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"
36 #include "radius/radius.h"
40 #include "vlan_init.h"
41 #include "ctrl_iface.h"
43 #include "eap_server/eap_sim_db.h"
44 #include "eap_server/eap.h"
45 #include "eap_server/tncs.h"
47 #include "l2_packet/l2_packet.h"
48 #include "wps_hostapd.h"
51 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
52 size_t identity_len, int phase2,
53 struct eap_user *user);
54 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
55 static int hostapd_setup_wpa(struct hostapd_data *hapd);
56 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
58 struct hapd_interfaces {
60 struct hostapd_iface **iface;
64 extern int wpa_debug_level;
65 extern int wpa_debug_show_keys;
66 extern int wpa_debug_timestamp;
69 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
70 int level, const char *txt, size_t len)
72 struct hostapd_data *hapd = ctx;
73 char *format, *module_str;
75 int conf_syslog_level, conf_stdout_level;
76 unsigned int conf_syslog, conf_stdout;
79 format = os_malloc(maxlen);
83 if (hapd && hapd->conf) {
84 conf_syslog_level = hapd->conf->logger_syslog_level;
85 conf_stdout_level = hapd->conf->logger_stdout_level;
86 conf_syslog = hapd->conf->logger_syslog;
87 conf_stdout = hapd->conf->logger_stdout;
89 conf_syslog_level = conf_stdout_level = 0;
90 conf_syslog = conf_stdout = (unsigned int) -1;
94 case HOSTAPD_MODULE_IEEE80211:
95 module_str = "IEEE 802.11";
97 case HOSTAPD_MODULE_IEEE8021X:
98 module_str = "IEEE 802.1X";
100 case HOSTAPD_MODULE_RADIUS:
101 module_str = "RADIUS";
103 case HOSTAPD_MODULE_WPA:
106 case HOSTAPD_MODULE_DRIVER:
107 module_str = "DRIVER";
109 case HOSTAPD_MODULE_IAPP:
112 case HOSTAPD_MODULE_MLME:
120 if (hapd && hapd->conf && addr)
121 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
122 hapd->conf->iface, MAC2STR(addr),
123 module_str ? " " : "", module_str, txt);
124 else if (hapd && hapd->conf)
125 os_snprintf(format, maxlen, "%s:%s%s %s",
126 hapd->conf->iface, module_str ? " " : "",
129 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
130 MAC2STR(addr), module_str ? " " : "",
133 os_snprintf(format, maxlen, "%s%s%s",
134 module_str, module_str ? ": " : "", txt);
136 if ((conf_stdout & module) && level >= conf_stdout_level) {
137 wpa_debug_print_timestamp();
138 printf("%s\n", format);
141 #ifndef CONFIG_NATIVE_WINDOWS
142 if ((conf_syslog & module) && level >= conf_syslog_level) {
145 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
146 case HOSTAPD_LEVEL_DEBUG:
147 priority = LOG_DEBUG;
149 case HOSTAPD_LEVEL_INFO:
152 case HOSTAPD_LEVEL_NOTICE:
153 priority = LOG_NOTICE;
155 case HOSTAPD_LEVEL_WARNING:
156 priority = LOG_WARNING;
162 syslog(priority, "%s", format);
164 #endif /* CONFIG_NATIVE_WINDOWS */
170 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
174 /* New Prism2.5/3 STA firmware versions seem to have issues with this
175 * broadcast deauth frame. This gets the firmware in odd state where
176 * nothing works correctly, so let's skip sending this for the hostap
179 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
180 os_memset(addr, 0xff, ETH_ALEN);
181 hostapd_sta_deauth(hapd, addr,
182 WLAN_REASON_PREV_AUTH_NOT_VALID);
188 * hostapd_prune_associations - Remove extraneous associations
189 * @hapd: Pointer to BSS data for the most recent association
190 * @sta: Pointer to the associated STA data
192 * This function looks through all radios and BSS's for previous
193 * (stale) associations of STA. If any are found they are removed.
195 static void hostapd_prune_associations(struct hostapd_data *hapd,
196 struct sta_info *sta)
198 struct sta_info *osta;
199 struct hostapd_data *ohapd;
201 struct hapd_interfaces *interfaces = eloop_get_user_data();
203 for (i = 0; i < interfaces->count; i++) {
204 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
205 ohapd = interfaces->iface[i]->bss[j];
208 osta = ap_get_sta(ohapd, sta->addr);
212 ap_sta_disassociate(ohapd, osta,
213 WLAN_REASON_UNSPECIFIED);
220 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
221 * @hapd: Pointer to BSS data
222 * @sta: Pointer to the associated STA data
223 * @reassoc: 1 to indicate this was a re-association; 0 = first association
225 * This function will be called whenever a station associates with the AP. It
226 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
227 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
228 * authentication and association) internally.
230 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
233 if (hapd->tkip_countermeasures) {
234 hostapd_sta_deauth(hapd, sta->addr,
235 WLAN_REASON_MICHAEL_MIC_FAILURE);
239 hostapd_prune_associations(hapd, sta);
241 /* IEEE 802.11F (IAPP) */
242 if (hapd->conf->ieee802_11f)
243 iapp_new_station(hapd->iapp, sta);
245 /* Start accounting here, if IEEE 802.1X and WPA are not used.
246 * IEEE 802.1X/WPA code will start accounting after the station has
247 * been authorized. */
248 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
249 accounting_sta_start(hapd, sta);
251 hostapd_wme_sta_config(hapd, sta);
253 /* Start IEEE 802.1X authentication process for new stations */
254 ieee802_1x_new_station(hapd, sta);
256 if (sta->auth_alg != WLAN_AUTH_FT &&
257 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
258 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
260 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
264 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
265 const u8 *buf, size_t len, int ack)
267 struct sta_info *sta;
269 sta = ap_get_sta(hapd, addr);
270 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
271 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
272 "activity poll", MAC2STR(sta->addr),
273 ack ? "ACKed" : "did not ACK");
275 sta->flags &= ~WLAN_STA_PENDING_POLL;
278 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
282 void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, const u8 *addr)
284 struct sta_info *sta;
286 sta = ap_get_sta(hapd, addr);
287 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
288 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
289 "STA " MACSTR, MAC2STR(addr));
290 if (sta && (sta->flags & WLAN_STA_AUTH))
291 hostapd_sta_disassoc(
293 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
297 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
302 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
303 const u8 *ie, size_t ielen)
305 struct sta_info *sta;
308 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
309 HOSTAPD_LEVEL_INFO, "associated");
311 sta = ap_get_sta(hapd, addr);
313 accounting_sta_stop(hapd, sta);
315 sta = ap_sta_add(hapd, addr);
319 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
321 if (hapd->conf->wpa) {
322 if (ie == NULL || ielen == 0) {
323 if (hapd->conf->wps_state) {
324 wpa_printf(MSG_DEBUG, "STA did not include "
325 "WPA/RSN IE in (Re)Association "
326 "Request - possible WPS use");
327 sta->flags |= WLAN_STA_MAYBE_WPS;
331 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
334 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
335 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
336 sta->flags |= WLAN_STA_WPS;
340 if (sta->wpa_sm == NULL)
341 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
343 if (sta->wpa_sm == NULL) {
344 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
348 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
350 if (res != WPA_IE_OK) {
351 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
352 "rejected? (res %u)", res);
353 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
359 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
360 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
361 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
363 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
365 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
371 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
373 struct sta_info *sta;
375 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
376 HOSTAPD_LEVEL_INFO, "disassociated");
378 sta = ap_get_sta(hapd, addr);
380 wpa_printf(MSG_DEBUG, "Disassociation notification for "
381 "unknown STA " MACSTR, MAC2STR(addr));
385 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
386 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
387 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
388 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
389 ap_free_sta(hapd, sta);
394 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
395 struct sta_info *sta, void *ctx)
397 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
403 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
405 struct hostapd_data *hapd = ctx;
406 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
407 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
409 #endif /* EAP_SERVER */
413 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
415 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
417 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
422 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
423 struct wpa_auth_config *wconf)
425 wconf->wpa = conf->wpa;
426 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
427 wconf->wpa_pairwise = conf->wpa_pairwise;
428 wconf->wpa_group = conf->wpa_group;
429 wconf->wpa_group_rekey = conf->wpa_group_rekey;
430 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
431 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
432 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
433 wconf->rsn_pairwise = conf->rsn_pairwise;
434 wconf->rsn_preauth = conf->rsn_preauth;
435 wconf->eapol_version = conf->eapol_version;
436 wconf->peerkey = conf->peerkey;
437 wconf->wme_enabled = conf->wme_enabled;
438 wconf->okc = conf->okc;
439 #ifdef CONFIG_IEEE80211W
440 wconf->ieee80211w = conf->ieee80211w;
441 #endif /* CONFIG_IEEE80211W */
442 #ifdef CONFIG_IEEE80211R
443 wconf->ssid_len = conf->ssid.ssid_len;
444 if (wconf->ssid_len > SSID_LEN)
445 wconf->ssid_len = SSID_LEN;
446 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
447 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
448 MOBILITY_DOMAIN_ID_LEN);
449 if (conf->nas_identifier &&
450 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
451 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
452 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
453 wconf->r0_key_holder_len);
455 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
456 wconf->r0_key_lifetime = conf->r0_key_lifetime;
457 wconf->reassociation_deadline = conf->reassociation_deadline;
458 wconf->r0kh_list = conf->r0kh_list;
459 wconf->r1kh_list = conf->r1kh_list;
460 wconf->pmk_r1_push = conf->pmk_r1_push;
461 #endif /* CONFIG_IEEE80211R */
465 int hostapd_reload_config(struct hostapd_iface *iface)
467 struct hostapd_data *hapd = iface->bss[0];
468 struct hostapd_config *newconf, *oldconf;
469 struct wpa_auth_config wpa_auth_conf;
471 newconf = hostapd_config_read(iface->config_fname);
476 * Deauthenticate all stations since the new configuration may not
477 * allow them to use the BSS anymore.
479 hostapd_flush_old_stations(hapd);
481 /* TODO: update dynamic data based on changed configuration
482 * items (e.g., open/close sockets, etc.) */
483 radius_client_flush(hapd->radius, 0);
485 oldconf = hapd->iconf;
486 hapd->iconf = newconf;
487 hapd->conf = &newconf->bss[0];
488 iface->conf = newconf;
490 if (hostapd_setup_wpa_psk(hapd->conf)) {
491 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
492 "after reloading configuration");
495 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
496 hostapd_setup_wpa(hapd);
497 else if (hapd->conf->wpa) {
498 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
499 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
500 } else if (hapd->wpa_auth) {
501 wpa_deinit(hapd->wpa_auth);
502 hapd->wpa_auth = NULL;
503 hostapd_set_privacy(hapd, 0);
504 hostapd_setup_encryption(hapd->conf->iface, hapd);
507 ieee802_11_set_beacon(hapd);
509 hostapd_config_free(oldconf);
511 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
517 #ifndef CONFIG_NATIVE_WINDOWS
519 * handle_reload - SIGHUP handler to reload configuration
521 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
523 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
526 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
529 for (i = 0; i < hapds->count; i++) {
530 if (hostapd_reload_config(hapds->iface[i]) < 0) {
531 wpa_printf(MSG_WARNING, "Failed to read new "
532 "configuration file - continuing with "
540 #ifdef HOSTAPD_DUMP_STATE
542 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
544 static void hostapd_dump_state(struct hostapd_data *hapd)
548 struct sta_info *sta;
552 if (!hapd->conf->dump_log_name) {
553 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
558 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
559 hapd->conf->dump_log_name);
560 f = fopen(hapd->conf->dump_log_name, "w");
562 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
563 "writing.", hapd->conf->dump_log_name);
568 fprintf(f, "hostapd state dump - %s", ctime(&now));
569 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
570 "num_sta_no_short_slot_time=%d\n"
571 "num_sta_no_short_preamble=%d\n",
572 hapd->num_sta, hapd->iface->num_sta_non_erp,
573 hapd->iface->num_sta_no_short_slot_time,
574 hapd->iface->num_sta_no_short_preamble);
576 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
577 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
580 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
581 " capability=0x%x listen_interval=%d\n",
584 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
585 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
586 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
587 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
588 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
589 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
591 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
593 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
594 "[SHORT_PREAMBLE]" : ""),
595 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
596 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
597 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
598 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
599 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
600 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
602 sta->listen_interval);
604 fprintf(f, " supported_rates=");
605 for (i = 0; i < sta->supported_rates_len; i++)
606 fprintf(f, "%02x ", sta->supported_rates[i]);
610 " timeout_next=%s\n",
611 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
612 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
615 ieee802_1x_dump_state(f, " ", sta);
618 buf = os_malloc(4096);
620 int count = radius_client_get_mib(hapd->radius, buf, 4096);
623 else if (count > 4095)
626 fprintf(f, "%s", buf);
628 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
631 else if (count > 4095)
634 fprintf(f, "%s", buf);
639 #endif /* HOSTAPD_DUMP_STATE */
642 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
644 #ifdef HOSTAPD_DUMP_STATE
645 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
648 for (i = 0; i < hapds->count; i++) {
649 struct hostapd_iface *hapd_iface = hapds->iface[i];
650 for (j = 0; j < hapd_iface->num_bss; j++)
651 hostapd_dump_state(hapd_iface->bss[j]);
653 #endif /* HOSTAPD_DUMP_STATE */
655 #endif /* CONFIG_NATIVE_WINDOWS */
657 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
662 for (i = 0; i < NUM_WEP_KEYS; i++) {
663 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
664 0, i == 0 ? 1 : 0)) {
665 wpa_printf(MSG_DEBUG, "Failed to clear default "
666 "encryption keys (ifname=%s keyidx=%d)",
670 #ifdef CONFIG_IEEE80211W
671 if (hapd->conf->ieee80211w) {
672 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
673 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
676 wpa_printf(MSG_DEBUG, "Failed to clear "
677 "default mgmt encryption keys "
678 "(ifname=%s keyidx=%d)", ifname, i);
682 #endif /* CONFIG_IEEE80211W */
686 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
688 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
693 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
696 struct hostapd_ssid *ssid = &hapd->conf->ssid;
699 if (ssid->wep.default_len &&
700 hostapd_set_encryption(hapd->conf->iface,
701 hapd, "WEP", NULL, idx,
704 idx == ssid->wep.idx)) {
705 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
709 if (ssid->dyn_vlan_keys) {
711 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
713 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
716 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
722 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
726 wpa_printf(MSG_WARNING, "Could not set "
727 "dynamic VLAN WEP encryption.");
737 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
738 * @hapd: Pointer to BSS data
740 * This function is used to free all per-BSS data structures and resources.
741 * This gets called in a loop for each BSS between calls to
742 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
743 * is deinitialized. Most of the modules that are initialized in
744 * hostapd_setup_bss() are deinitialized here.
746 static void hostapd_cleanup(struct hostapd_data *hapd)
748 hostapd_ctrl_iface_deinit(hapd);
750 os_free(hapd->default_wep_key);
751 hapd->default_wep_key = NULL;
752 iapp_deinit(hapd->iapp);
754 accounting_deinit(hapd);
755 rsn_preauth_iface_deinit(hapd);
756 if (hapd->wpa_auth) {
757 wpa_deinit(hapd->wpa_auth);
758 hapd->wpa_auth = NULL;
760 if (hostapd_set_privacy(hapd, 0)) {
761 wpa_printf(MSG_DEBUG, "Could not disable "
762 "PrivacyInvoked for interface %s",
766 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
767 wpa_printf(MSG_DEBUG, "Could not remove generic "
768 "information element from interface %s",
772 ieee802_1x_deinit(hapd);
774 hostapd_acl_deinit(hapd);
775 radius_client_deinit(hapd->radius);
777 radius_server_deinit(hapd->radius_srv);
778 hapd->radius_srv = NULL;
780 #ifdef CONFIG_IEEE80211R
781 l2_packet_deinit(hapd->l2);
782 #endif /* CONFIG_IEEE80211R */
784 hostapd_deinit_wps(hapd);
786 hostapd_wireless_event_deinit(hapd);
790 tls_deinit(hapd->ssl_ctx);
791 hapd->ssl_ctx = NULL;
793 #endif /* EAP_TLS_FUNCS */
796 if (hapd->eap_sim_db_priv) {
797 eap_sim_db_deinit(hapd->eap_sim_db_priv);
798 hapd->eap_sim_db_priv = NULL;
800 #endif /* EAP_SERVER */
802 if (hapd->interface_added &&
803 hostapd_bss_remove(hapd, hapd->conf->iface)) {
804 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
811 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
812 * @iface: Pointer to interface data
814 * This function is called before per-BSS data structures are deinitialized
815 * with hostapd_cleanup().
817 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
823 * hostapd_cleanup_iface - Complete per-interface cleanup
824 * @iface: Pointer to interface data
826 * This function is called after per-BSS data structures are deinitialized
827 * with hostapd_cleanup().
829 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
831 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
832 iface->hw_features = NULL;
833 os_free(iface->current_rates);
834 iface->current_rates = NULL;
835 ap_list_deinit(iface);
836 hostapd_config_free(iface->conf);
839 os_free(iface->config_fname);
845 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
849 hostapd_broadcast_wep_set(hapd);
851 if (hapd->conf->ssid.wep.default_len)
854 for (i = 0; i < 4; i++) {
855 if (hapd->conf->ssid.wep.key[i] &&
856 hostapd_set_encryption(iface, hapd, "WEP", NULL,
857 i, hapd->conf->ssid.wep.key[i],
858 hapd->conf->ssid.wep.len[i],
859 i == hapd->conf->ssid.wep.idx)) {
860 wpa_printf(MSG_WARNING, "Could not set WEP "
864 if (hapd->conf->ssid.wep.key[i] &&
865 i == hapd->conf->ssid.wep.idx)
866 hostapd_set_privacy(hapd, 1);
873 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
877 if (hostapd_drv_none(hapd))
880 wpa_printf(MSG_DEBUG, "Flushing old station entries");
881 if (hostapd_flush(hapd)) {
882 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
885 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
886 hostapd_deauth_all_stas(hapd);
892 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
893 logger_level level, const char *txt)
895 struct hostapd_data *hapd = ctx;
900 hlevel = HOSTAPD_LEVEL_WARNING;
903 hlevel = HOSTAPD_LEVEL_INFO;
907 hlevel = HOSTAPD_LEVEL_DEBUG;
911 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
915 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
918 struct hostapd_data *hapd = ctx;
919 struct sta_info *sta;
921 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
922 "STA " MACSTR " reason %d",
923 __func__, MAC2STR(addr), reason);
925 sta = ap_get_sta(hapd, addr);
926 hostapd_sta_deauth(hapd, addr, reason);
929 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
930 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
931 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
932 sta->timeout_next = STA_REMOVE;
936 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
938 struct hostapd_data *hapd = ctx;
939 ieee80211_michael_mic_failure(hapd, addr, 0);
943 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
944 wpa_eapol_variable var, int value)
946 struct hostapd_data *hapd = ctx;
947 struct sta_info *sta = ap_get_sta(hapd, addr);
951 case WPA_EAPOL_portEnabled:
952 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
954 case WPA_EAPOL_portValid:
955 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
957 case WPA_EAPOL_authorized:
958 ieee802_1x_set_sta_authorized(hapd, sta, value);
960 case WPA_EAPOL_portControl_Auto:
962 sta->eapol_sm->portControl = Auto;
964 case WPA_EAPOL_keyRun:
966 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
968 case WPA_EAPOL_keyAvailable:
970 sta->eapol_sm->eap_if->eapKeyAvailable =
971 value ? TRUE : FALSE;
973 case WPA_EAPOL_keyDone:
975 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
977 case WPA_EAPOL_inc_EapolFramesTx:
979 sta->eapol_sm->dot1xAuthEapolFramesTx++;
985 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
986 wpa_eapol_variable var)
988 struct hostapd_data *hapd = ctx;
989 struct sta_info *sta = ap_get_sta(hapd, addr);
990 if (sta == NULL || sta->eapol_sm == NULL)
993 case WPA_EAPOL_keyRun:
994 return sta->eapol_sm->keyRun;
995 case WPA_EAPOL_keyAvailable:
996 return sta->eapol_sm->eap_if->eapKeyAvailable;
1003 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
1006 struct hostapd_data *hapd = ctx;
1007 return hostapd_get_psk(hapd->conf, addr, prev_psk);
1011 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
1014 struct hostapd_data *hapd = ctx;
1017 struct sta_info *sta;
1019 sta = ap_get_sta(hapd, addr);
1023 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
1029 os_memcpy(msk, key, keylen);
1036 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
1037 const u8 *addr, int idx, u8 *key,
1040 struct hostapd_data *hapd = ctx;
1041 const char *ifname = hapd->conf->iface;
1044 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
1049 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
1054 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
1057 struct hostapd_data *hapd = ctx;
1058 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
1062 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
1065 struct hostapd_data *hapd = ctx;
1066 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
1071 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
1072 const u8 *data, size_t data_len,
1075 struct hostapd_data *hapd = ctx;
1076 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
1080 static int hostapd_wpa_auth_for_each_sta(
1081 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
1084 struct hostapd_data *hapd = ctx;
1085 struct sta_info *sta;
1087 for (sta = hapd->sta_list; sta; sta = sta->next) {
1088 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
1095 static int hostapd_wpa_auth_for_each_auth(
1096 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
1099 struct hostapd_data *ohapd;
1101 struct hapd_interfaces *interfaces = eloop_get_user_data();
1103 for (i = 0; i < interfaces->count; i++) {
1104 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
1105 ohapd = interfaces->iface[i]->bss[j];
1106 if (cb(ohapd->wpa_auth, cb_ctx))
1115 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
1116 const u8 *data, size_t data_len)
1118 struct hostapd_data *hapd = ctx;
1120 if (hapd->driver && hapd->driver->send_ether)
1121 return hapd->driver->send_ether(hapd->drv_priv, dst,
1122 hapd->own_addr, proto,
1124 if (hapd->l2 == NULL)
1126 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
1130 #ifdef CONFIG_IEEE80211R
1132 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1133 const u8 *data, size_t data_len)
1135 struct hostapd_data *hapd = ctx;
1137 struct ieee80211_mgmt *m;
1139 struct sta_info *sta;
1141 sta = ap_get_sta(hapd, dst);
1142 if (sta == NULL || sta->wpa_sm == NULL)
1145 m = os_zalloc(sizeof(*m) + data_len);
1148 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1149 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1150 WLAN_FC_STYPE_ACTION);
1151 os_memcpy(m->da, dst, ETH_ALEN);
1152 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1153 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1154 os_memcpy(&m->u, data, data_len);
1156 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1162 static struct wpa_state_machine *
1163 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1165 struct hostapd_data *hapd = ctx;
1166 struct sta_info *sta;
1168 sta = ap_sta_add(hapd, sta_addr);
1174 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1175 if (sta->wpa_sm == NULL) {
1176 ap_free_sta(hapd, sta);
1179 sta->auth_alg = WLAN_AUTH_FT;
1185 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1188 struct hostapd_data *hapd = ctx;
1189 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1192 #endif /* CONFIG_IEEE80211R */
1196 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1197 * @iface: Pointer to interface data
1198 * Returns: 0 on success, -1 on failure
1200 * This function is used to validate that the configured BSSIDs are valid.
1202 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1204 u8 mask[ETH_ALEN] = { 0 };
1205 struct hostapd_data *hapd = iface->bss[0];
1206 unsigned int i = iface->conf->num_bss, bits = 0, j;
1209 if (hostapd_drv_none(hapd))
1212 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1214 /* Determine the bits necessary to cover the number of BSSIDs. */
1215 for (i--; i; i >>= 1)
1218 /* Determine the bits necessary to any configured BSSIDs,
1219 if they are higher than the number of BSSIDs. */
1220 for (j = 0; j < iface->conf->num_bss; j++) {
1221 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1224 for (i = 0; i < ETH_ALEN; i++) {
1226 iface->conf->bss[j].bssid[i] ^
1231 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1237 while (mask[i] != 0) {
1249 os_memset(mask, 0xff, ETH_ALEN);
1251 for (i = 5; i > 5 - j; i--)
1257 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1258 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1260 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1265 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1266 MACSTR " for start address " MACSTR ".",
1267 MAC2STR(mask), MAC2STR(hapd->own_addr));
1271 for (i = 0; i < ETH_ALEN; i++) {
1272 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1273 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1274 " for start address " MACSTR ".",
1275 MAC2STR(mask), MAC2STR(hapd->own_addr));
1276 wpa_printf(MSG_ERROR, "Start address must be the "
1277 "first address in the block (i.e., addr "
1278 "AND mask == addr).");
1287 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1291 for (i = 0; i < conf->num_bss; i++) {
1292 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1301 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1303 struct wpa_auth_config _conf;
1304 struct wpa_auth_callbacks cb;
1308 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1309 os_memset(&cb, 0, sizeof(cb));
1311 cb.logger = hostapd_wpa_auth_logger;
1312 cb.disconnect = hostapd_wpa_auth_disconnect;
1313 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1314 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1315 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1316 cb.get_psk = hostapd_wpa_auth_get_psk;
1317 cb.get_msk = hostapd_wpa_auth_get_msk;
1318 cb.set_key = hostapd_wpa_auth_set_key;
1319 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1320 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1321 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1322 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1323 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1324 cb.send_ether = hostapd_wpa_auth_send_ether;
1325 #ifdef CONFIG_IEEE80211R
1326 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1327 cb.add_sta = hostapd_wpa_auth_add_sta;
1328 #endif /* CONFIG_IEEE80211R */
1329 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1330 if (hapd->wpa_auth == NULL) {
1331 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1335 if (hostapd_set_privacy(hapd, 1)) {
1336 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1337 "for interface %s", hapd->conf->iface);
1341 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1342 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1343 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1344 "the kernel driver.");
1348 if (rsn_preauth_iface_init(hapd)) {
1349 wpa_printf(MSG_ERROR, "Initialization of RSN "
1350 "pre-authentication failed.");
1359 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1360 struct hostapd_bss_config *conf)
1362 struct radius_server_conf srv;
1363 os_memset(&srv, 0, sizeof(srv));
1364 srv.client_file = conf->radius_server_clients;
1365 srv.auth_port = conf->radius_server_auth_port;
1366 srv.conf_ctx = conf;
1367 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1368 srv.ssl_ctx = hapd->ssl_ctx;
1369 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1370 srv.eap_fast_a_id = conf->eap_fast_a_id;
1371 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1372 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1373 srv.eap_fast_prov = conf->eap_fast_prov;
1374 srv.pac_key_lifetime = conf->pac_key_lifetime;
1375 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1376 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1377 srv.tnc = conf->tnc;
1378 srv.wps = hapd->wps;
1379 srv.ipv6 = conf->radius_server_ipv6;
1380 srv.get_eap_user = hostapd_radius_get_eap_user;
1381 srv.eap_req_id_text = conf->eap_req_id_text;
1382 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1384 hapd->radius_srv = radius_server_init(&srv);
1385 if (hapd->radius_srv == NULL) {
1386 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1395 * hostapd_setup_bss - Per-BSS setup (initialization)
1396 * @hapd: Pointer to BSS data
1397 * @first: Whether this BSS is the first BSS of an interface
1399 * This function is used to initialize all per-BSS data structures and
1400 * resources. This gets called in a loop for each BSS when an interface is
1401 * initialized. Most of the modules that are initialized here will be
1402 * deinitialized in hostapd_cleanup().
1404 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1406 struct hostapd_bss_config *conf = hapd->conf;
1407 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1408 int ssid_len, set_ssid;
1411 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1412 /* Allocate the next available BSSID. */
1414 inc_byte_array(hapd->own_addr, ETH_ALEN);
1415 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1417 /* Allocate the configured BSSID. */
1418 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1420 if (hostapd_mac_comp(hapd->own_addr,
1421 hapd->iface->bss[0]->own_addr) ==
1423 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1424 "BSSID set to the MAC address of "
1425 "the radio", hapd->conf->iface);
1430 hapd->interface_added = 1;
1431 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1433 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1434 MACSTR ")", MAC2STR(hapd->own_addr));
1440 * Fetch the SSID from the system and use it or,
1441 * if one was specified in the config file, verify they
1444 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1446 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1449 if (conf->ssid.ssid_set) {
1451 * If SSID is specified in the config file and it differs
1452 * from what is being used then force installation of the
1455 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1456 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1459 * No SSID in the config file; just use the one we got
1463 conf->ssid.ssid_len = ssid_len;
1464 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1465 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1468 if (!hostapd_drv_none(hapd)) {
1469 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1471 hapd->conf->iface, MAC2STR(hapd->own_addr),
1472 hapd->conf->ssid.ssid);
1475 if (hostapd_setup_wpa_psk(conf)) {
1476 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1480 /* Set flag for whether SSID is broadcast in beacons */
1481 if (hostapd_set_broadcast_ssid(hapd,
1482 !!hapd->conf->ignore_broadcast_ssid)) {
1483 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1488 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1489 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1494 /* Set SSID for the kernel driver (to be used in beacon and probe
1495 * response frames) */
1496 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1497 conf->ssid.ssid_len)) {
1498 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1502 if (wpa_debug_level == MSG_MSGDUMP)
1503 conf->radius->msg_dumps = 1;
1504 hapd->radius = radius_client_init(hapd, conf->radius);
1505 if (hapd->radius == NULL) {
1506 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1510 if (hostapd_acl_init(hapd)) {
1511 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1514 if (hostapd_init_wps(hapd, conf))
1517 if (ieee802_1x_init(hapd)) {
1518 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1522 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1525 if (accounting_init(hapd)) {
1526 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1530 if (hapd->conf->ieee802_11f &&
1531 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1532 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1537 if (hostapd_ctrl_iface_init(hapd)) {
1538 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1542 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1543 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1547 #ifdef CONFIG_IEEE80211R
1548 if (!hostapd_drv_none(hapd)) {
1549 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1550 hostapd_rrb_receive, hapd, 0);
1551 if (hapd->l2 == NULL &&
1552 (hapd->driver == NULL ||
1553 hapd->driver->send_ether == NULL)) {
1554 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1559 #endif /* CONFIG_IEEE80211R */
1561 ieee802_11_set_beacon(hapd);
1563 if (conf->radius_server_clients &&
1564 hostapd_setup_radius_srv(hapd, conf))
1571 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1573 struct hostapd_data *hapd = iface->bss[0];
1575 struct hostapd_tx_queue_params *p;
1577 for (i = 0; i < NUM_TX_QUEUES; i++) {
1578 p = &iface->conf->tx_queue[i];
1583 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1584 p->cwmax, p->burst)) {
1585 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1586 "parameters for queue %d.", i);
1587 /* Continue anyway */
1593 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1594 size_t identity_len, int phase2,
1595 struct eap_user *user)
1597 const struct hostapd_eap_user *eap_user;
1600 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1601 if (eap_user == NULL)
1607 os_memset(user, 0, sizeof(*user));
1608 count = EAP_USER_MAX_METHODS;
1609 if (count > EAP_MAX_METHODS)
1610 count = EAP_MAX_METHODS;
1611 for (i = 0; i < count; i++) {
1612 user->methods[i].vendor = eap_user->methods[i].vendor;
1613 user->methods[i].method = eap_user->methods[i].method;
1616 if (eap_user->password) {
1617 user->password = os_malloc(eap_user->password_len);
1618 if (user->password == NULL)
1620 os_memcpy(user->password, eap_user->password,
1621 eap_user->password_len);
1622 user->password_len = eap_user->password_len;
1623 user->password_hash = eap_user->password_hash;
1625 user->force_version = eap_user->force_version;
1626 user->ttls_auth = eap_user->ttls_auth;
1632 static int setup_interface(struct hostapd_iface *iface)
1634 struct hostapd_data *hapd = iface->bss[0];
1635 struct hostapd_bss_config *conf = hapd->conf;
1638 u8 *b = conf->bssid;
1645 * Initialize the driver interface and make sure that all BSSes get
1646 * configured with a pointer to this driver interface.
1648 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1649 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1651 hapd->drv_priv = hostapd_driver_init(hapd);
1654 if (hapd->drv_priv == NULL) {
1655 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1656 hapd->driver ? hapd->driver->name : "Unknown");
1657 hapd->driver = NULL;
1660 for (i = 0; i < iface->num_bss; i++) {
1661 iface->bss[i]->driver = hapd->driver;
1662 iface->bss[i]->drv_priv = hapd->drv_priv;
1665 if (hostapd_validate_bssid_configuration(iface))
1668 #ifdef CONFIG_IEEE80211N
1669 SET_2BIT_LE16(&iface->ht_op_mode,
1670 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1672 #endif /* CONFIG_IEEE80211N */
1674 os_memcpy(country, hapd->iconf->country, 3);
1676 if (hostapd_set_country(hapd, country) < 0) {
1677 wpa_printf(MSG_ERROR, "Failed to set country code");
1681 if (hapd->iconf->ieee80211d &&
1682 hostapd_set_ieee80211d(hapd, 1) < 0) {
1683 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1684 hapd->iconf->ieee80211d);
1688 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1689 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1690 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1695 /* TODO: merge with hostapd_driver_init() ? */
1696 if (hostapd_wireless_event_init(hapd) < 0)
1699 if (hostapd_get_hw_features(iface)) {
1700 /* Not all drivers support this yet, so continue without hw
1703 int ret = hostapd_select_hw_mode(iface);
1705 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1706 "channel. (%d)", ret);
1711 hostapd_flush_old_stations(hapd);
1712 hostapd_set_privacy(hapd, 0);
1714 if (hapd->iconf->channel) {
1715 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1716 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1717 "Frequency: %d MHz",
1718 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1719 hapd->iconf->channel, freq);
1721 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1722 hapd->iconf->ieee80211n,
1723 hapd->iconf->secondary_channel)) {
1724 wpa_printf(MSG_ERROR, "Could not set channel for "
1730 hostapd_broadcast_wep_clear(hapd);
1731 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1734 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1735 ieee802_11_set_beacon(hapd);
1737 if (hapd->iconf->rts_threshold > -1 &&
1738 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1739 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1744 if (hapd->iconf->fragm_threshold > -1 &&
1745 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1746 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1747 "for kernel driver");
1751 prev_addr = hapd->own_addr;
1753 for (j = 0; j < iface->num_bss; j++) {
1754 hapd = iface->bss[j];
1756 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1757 if (hostapd_setup_bss(hapd, j == 0))
1759 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1760 prev_addr = hapd->own_addr;
1763 hostapd_tx_queue_params(iface);
1765 ap_list_init(iface);
1767 if (hostapd_driver_commit(hapd) < 0) {
1768 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1769 "configuration", __func__);
1778 * hostapd_setup_interface - Setup of an interface
1779 * @iface: Pointer to interface data.
1780 * Returns: 0 on success, -1 on failure
1782 * Initializes the driver interface, validates the configuration,
1783 * and sets driver parameters based on the configuration.
1784 * Flushes old stations, sets the channel, encryption,
1785 * beacons, and WDS links based on the configuration.
1787 static int hostapd_setup_interface(struct hostapd_iface *iface)
1791 ret = setup_interface(iface);
1793 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1794 iface->bss[0]->conf->iface);
1797 } else if (!hostapd_drv_none(iface->bss[0])) {
1798 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1799 iface->bss[0]->conf->iface);
1806 static void show_version(void)
1809 "hostapd v" VERSION_STR "\n"
1810 "User space daemon for IEEE 802.11 AP management,\n"
1811 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1812 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1813 "and contributors\n");
1817 static void usage(void)
1822 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1823 "<configuration file(s)>\n"
1826 " -h show this usage\n"
1827 " -d show more debug messages (-dd for even more)\n"
1828 " -B run daemon in the background\n"
1830 " -K include key data in debug messages\n"
1831 " -t include timestamps in some debug messages\n"
1832 " -v show hostapd version\n");
1839 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1840 * @hapd_iface: Pointer to interface data
1841 * @conf: Pointer to per-interface configuration
1842 * @bss: Pointer to per-BSS configuration for this BSS
1843 * Returns: Pointer to allocated BSS data
1845 * This function is used to allocate per-BSS data structure. This data will be
1846 * freed after hostapd_cleanup() is called for it during interface
1849 static struct hostapd_data *
1850 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1851 struct hostapd_config *conf,
1852 struct hostapd_bss_config *bss)
1854 struct hostapd_data *hapd;
1856 hapd = os_zalloc(sizeof(*hapd));
1862 hapd->iface = hapd_iface;
1864 if (hapd->conf->individual_wep_key_len > 0) {
1865 /* use key0 in individual key and key1 in broadcast key */
1866 hapd->default_wep_key_idx = 1;
1869 #ifdef EAP_TLS_FUNCS
1870 if (hapd->conf->eap_server &&
1871 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1872 hapd->conf->dh_file)) {
1873 struct tls_connection_params params;
1875 hapd->ssl_ctx = tls_init(NULL);
1876 if (hapd->ssl_ctx == NULL) {
1877 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1881 os_memset(¶ms, 0, sizeof(params));
1882 params.ca_cert = hapd->conf->ca_cert;
1883 params.client_cert = hapd->conf->server_cert;
1884 params.private_key = hapd->conf->private_key;
1885 params.private_key_passwd = hapd->conf->private_key_passwd;
1886 params.dh_file = hapd->conf->dh_file;
1888 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1889 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1893 if (tls_global_set_verify(hapd->ssl_ctx,
1894 hapd->conf->check_crl)) {
1895 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1899 #endif /* EAP_TLS_FUNCS */
1902 if (hapd->conf->eap_sim_db) {
1903 hapd->eap_sim_db_priv =
1904 eap_sim_db_init(hapd->conf->eap_sim_db,
1905 hostapd_sim_db_cb, hapd);
1906 if (hapd->eap_sim_db_priv == NULL) {
1907 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1908 "database interface");
1912 #endif /* EAP_SERVER */
1914 hapd->driver = hapd->iconf->driver;
1918 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1921 /* TODO: cleanup allocated resources(?) */
1928 * hostapd_init - Allocate and initialize per-interface data
1929 * @config_file: Path to the configuration file
1930 * Returns: Pointer to the allocated interface data or %NULL on failure
1932 * This function is used to allocate main data structures for per-interface
1933 * data. The allocated data buffer will be freed by calling
1934 * hostapd_cleanup_iface().
1936 static struct hostapd_iface * hostapd_init(const char *config_file)
1938 struct hostapd_iface *hapd_iface = NULL;
1939 struct hostapd_config *conf = NULL;
1940 struct hostapd_data *hapd;
1943 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1944 if (hapd_iface == NULL)
1947 hapd_iface->config_fname = os_strdup(config_file);
1948 if (hapd_iface->config_fname == NULL)
1951 conf = hostapd_config_read(hapd_iface->config_fname);
1954 hapd_iface->conf = conf;
1956 hapd_iface->num_bss = conf->num_bss;
1957 hapd_iface->bss = os_zalloc(conf->num_bss *
1958 sizeof(struct hostapd_data *));
1959 if (hapd_iface->bss == NULL)
1962 for (i = 0; i < conf->num_bss; i++) {
1963 hapd = hapd_iface->bss[i] =
1964 hostapd_alloc_bss_data(hapd_iface, conf,
1974 hostapd_config_free(conf);
1976 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1977 hapd = hapd_iface->bss[i];
1978 if (hapd && hapd->ssl_ctx)
1979 tls_deinit(hapd->ssl_ctx);
1982 os_free(hapd_iface->config_fname);
1983 os_free(hapd_iface->bss);
1984 os_free(hapd_iface);
1990 int main(int argc, char *argv[])
1992 struct hapd_interfaces interfaces;
1995 int c, debug = 0, daemonize = 0, tnc = 0;
1996 const char *pid_file = NULL;
1998 hostapd_logger_register_cb(hostapd_logger_cb);
2001 c = getopt(argc, argv, "BdhKP:tv");
2010 if (wpa_debug_level > 0)
2017 wpa_debug_show_keys++;
2023 wpa_debug_timestamp++;
2039 if (eap_server_register_methods()) {
2040 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2044 interfaces.count = argc - optind;
2046 interfaces.iface = os_malloc(interfaces.count *
2047 sizeof(struct hostapd_iface *));
2048 if (interfaces.iface == NULL) {
2049 wpa_printf(MSG_ERROR, "malloc failed\n");
2053 if (eloop_init(&interfaces)) {
2054 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2058 #ifndef CONFIG_NATIVE_WINDOWS
2059 eloop_register_signal(SIGHUP, handle_reload, NULL);
2060 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
2061 #endif /* CONFIG_NATIVE_WINDOWS */
2062 eloop_register_signal_terminate(handle_term, NULL);
2064 /* Initialize interfaces */
2065 for (i = 0; i < interfaces.count; i++) {
2066 wpa_printf(MSG_ERROR, "Configuration file: %s",
2068 interfaces.iface[i] = hostapd_init(argv[optind + i]);
2069 if (!interfaces.iface[i])
2071 for (k = 0; k < debug; k++) {
2072 if (interfaces.iface[i]->bss[0]->conf->
2073 logger_stdout_level > 0)
2074 interfaces.iface[i]->bss[0]->conf->
2075 logger_stdout_level--;
2078 ret = hostapd_setup_interface(interfaces.iface[i]);
2082 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
2083 if (interfaces.iface[i]->bss[0]->conf->tnc)
2089 if (tnc && tncs_global_init() < 0) {
2090 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
2093 #endif /* EAP_TNC */
2095 if (daemonize && os_daemonize(pid_file)) {
2100 #ifndef CONFIG_NATIVE_WINDOWS
2101 openlog("hostapd", 0, LOG_DAEMON);
2102 #endif /* CONFIG_NATIVE_WINDOWS */
2106 /* Disconnect associated stations from all interfaces and BSSes */
2107 for (i = 0; i < interfaces.count; i++) {
2108 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2109 struct hostapd_data *hapd =
2110 interfaces.iface[i]->bss[j];
2111 hostapd_free_stas(hapd);
2112 hostapd_flush_old_stations(hapd);
2119 /* Deinitialize all interfaces */
2120 for (i = 0; i < interfaces.count; i++) {
2121 if (!interfaces.iface[i])
2123 hostapd_cleanup_iface_pre(interfaces.iface[i]);
2124 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2125 struct hostapd_data *hapd =
2126 interfaces.iface[i]->bss[j];
2127 hostapd_cleanup(hapd);
2128 if (j == interfaces.iface[i]->num_bss - 1 &&
2130 hostapd_driver_deinit(hapd);
2132 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2133 os_free(interfaces.iface[i]->bss[j]);
2134 hostapd_cleanup_iface(interfaces.iface[i]);
2136 os_free(interfaces.iface);
2139 tncs_global_deinit();
2140 #endif /* EAP_TNC */
2144 #ifndef CONFIG_NATIVE_WINDOWS
2146 #endif /* CONFIG_NATIVE_WINDOWS */
2148 eap_server_unregister_methods();
2150 os_daemonize_terminate(pid_file);