2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
25 #include "hw_features.h"
26 #include "accounting.h"
30 #include "ieee802_11_auth.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
39 #include "vlan_init.h"
40 #include "ctrl_iface.h"
42 #include "eap_server/eap_sim_db.h"
43 #include "eap_server/eap.h"
44 #include "eap_server/tncs.h"
46 #include "l2_packet/l2_packet.h"
47 #include "wps_hostapd.h"
50 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
51 size_t identity_len, int phase2,
52 struct eap_user *user);
53 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
54 static int hostapd_setup_wpa(struct hostapd_data *hapd);
55 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
57 struct hapd_interfaces {
59 struct hostapd_iface **iface;
63 extern int wpa_debug_level;
64 extern int wpa_debug_show_keys;
65 extern int wpa_debug_timestamp;
68 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
69 int level, const char *txt, size_t len)
71 struct hostapd_data *hapd = ctx;
72 char *format, *module_str;
74 int conf_syslog_level, conf_stdout_level;
75 unsigned int conf_syslog, conf_stdout;
78 format = os_malloc(maxlen);
82 if (hapd && hapd->conf) {
83 conf_syslog_level = hapd->conf->logger_syslog_level;
84 conf_stdout_level = hapd->conf->logger_stdout_level;
85 conf_syslog = hapd->conf->logger_syslog;
86 conf_stdout = hapd->conf->logger_stdout;
88 conf_syslog_level = conf_stdout_level = 0;
89 conf_syslog = conf_stdout = (unsigned int) -1;
93 case HOSTAPD_MODULE_IEEE80211:
94 module_str = "IEEE 802.11";
96 case HOSTAPD_MODULE_IEEE8021X:
97 module_str = "IEEE 802.1X";
99 case HOSTAPD_MODULE_RADIUS:
100 module_str = "RADIUS";
102 case HOSTAPD_MODULE_WPA:
105 case HOSTAPD_MODULE_DRIVER:
106 module_str = "DRIVER";
108 case HOSTAPD_MODULE_IAPP:
111 case HOSTAPD_MODULE_MLME:
119 if (hapd && hapd->conf && addr)
120 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
121 hapd->conf->iface, MAC2STR(addr),
122 module_str ? " " : "", module_str, txt);
123 else if (hapd && hapd->conf)
124 os_snprintf(format, maxlen, "%s:%s%s %s",
125 hapd->conf->iface, module_str ? " " : "",
128 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
129 MAC2STR(addr), module_str ? " " : "",
132 os_snprintf(format, maxlen, "%s%s%s",
133 module_str, module_str ? ": " : "", txt);
135 if ((conf_stdout & module) && level >= conf_stdout_level) {
136 wpa_debug_print_timestamp();
137 printf("%s\n", format);
140 #ifndef CONFIG_NATIVE_WINDOWS
141 if ((conf_syslog & module) && level >= conf_syslog_level) {
144 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
145 case HOSTAPD_LEVEL_DEBUG:
146 priority = LOG_DEBUG;
148 case HOSTAPD_LEVEL_INFO:
151 case HOSTAPD_LEVEL_NOTICE:
152 priority = LOG_NOTICE;
154 case HOSTAPD_LEVEL_WARNING:
155 priority = LOG_WARNING;
161 syslog(priority, "%s", format);
163 #endif /* CONFIG_NATIVE_WINDOWS */
169 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
173 /* New Prism2.5/3 STA firmware versions seem to have issues with this
174 * broadcast deauth frame. This gets the firmware in odd state where
175 * nothing works correctly, so let's skip sending this for the hostap
178 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
179 os_memset(addr, 0xff, ETH_ALEN);
180 hostapd_sta_deauth(hapd, addr,
181 WLAN_REASON_PREV_AUTH_NOT_VALID);
187 * hostapd_prune_associations - Remove extraneous associations
188 * @hapd: Pointer to BSS data for the most recent association
189 * @sta: Pointer to the associated STA data
191 * This function looks through all radios and BSS's for previous
192 * (stale) associations of STA. If any are found they are removed.
194 static void hostapd_prune_associations(struct hostapd_data *hapd,
195 struct sta_info *sta)
197 struct sta_info *osta;
198 struct hostapd_data *ohapd;
200 struct hapd_interfaces *interfaces = eloop_get_user_data();
202 for (i = 0; i < interfaces->count; i++) {
203 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
204 ohapd = interfaces->iface[i]->bss[j];
207 osta = ap_get_sta(ohapd, sta->addr);
211 ap_sta_disassociate(ohapd, osta,
212 WLAN_REASON_UNSPECIFIED);
219 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
220 * @hapd: Pointer to BSS data
221 * @sta: Pointer to the associated STA data
222 * @reassoc: 1 to indicate this was a re-association; 0 = first association
224 * This function will be called whenever a station associates with the AP. It
225 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
226 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
227 * authentication and association) internally.
229 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
232 if (hapd->tkip_countermeasures) {
233 hostapd_sta_deauth(hapd, sta->addr,
234 WLAN_REASON_MICHAEL_MIC_FAILURE);
238 hostapd_prune_associations(hapd, sta);
240 /* IEEE 802.11F (IAPP) */
241 if (hapd->conf->ieee802_11f)
242 iapp_new_station(hapd->iapp, sta);
244 /* Start accounting here, if IEEE 802.1X and WPA are not used.
245 * IEEE 802.1X/WPA code will start accounting after the station has
246 * been authorized. */
247 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
248 accounting_sta_start(hapd, sta);
250 hostapd_wme_sta_config(hapd, sta);
252 /* Start IEEE 802.1X authentication process for new stations */
253 ieee802_1x_new_station(hapd, sta);
255 if (sta->auth_alg != WLAN_AUTH_FT &&
256 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
257 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
259 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
263 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
264 const u8 *buf, size_t len, int ack)
266 struct sta_info *sta;
268 sta = ap_get_sta(hapd, addr);
269 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
270 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
271 "activity poll", MAC2STR(sta->addr),
272 ack ? "ACKed" : "did not ACK");
274 sta->flags &= ~WLAN_STA_PENDING_POLL;
277 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
282 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
283 struct sta_info *sta, void *ctx)
285 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
291 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
293 struct hostapd_data *hapd = ctx;
294 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
295 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
297 #endif /* EAP_SERVER */
301 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
303 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
305 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
310 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
311 struct wpa_auth_config *wconf)
313 wconf->wpa = conf->wpa;
314 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
315 wconf->wpa_pairwise = conf->wpa_pairwise;
316 wconf->wpa_group = conf->wpa_group;
317 wconf->wpa_group_rekey = conf->wpa_group_rekey;
318 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
319 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
320 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
321 wconf->rsn_pairwise = conf->rsn_pairwise;
322 wconf->rsn_preauth = conf->rsn_preauth;
323 wconf->eapol_version = conf->eapol_version;
324 wconf->peerkey = conf->peerkey;
325 wconf->wme_enabled = conf->wme_enabled;
326 wconf->okc = conf->okc;
327 #ifdef CONFIG_IEEE80211W
328 wconf->ieee80211w = conf->ieee80211w;
329 #endif /* CONFIG_IEEE80211W */
330 #ifdef CONFIG_IEEE80211R
331 wconf->ssid_len = conf->ssid.ssid_len;
332 if (wconf->ssid_len > SSID_LEN)
333 wconf->ssid_len = SSID_LEN;
334 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
335 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
336 MOBILITY_DOMAIN_ID_LEN);
337 if (conf->nas_identifier &&
338 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
339 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
340 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
341 wconf->r0_key_holder_len);
343 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
344 wconf->r0_key_lifetime = conf->r0_key_lifetime;
345 wconf->reassociation_deadline = conf->reassociation_deadline;
346 wconf->r0kh_list = conf->r0kh_list;
347 wconf->r1kh_list = conf->r1kh_list;
348 wconf->pmk_r1_push = conf->pmk_r1_push;
349 #endif /* CONFIG_IEEE80211R */
353 int hostapd_reload_config(struct hostapd_iface *iface)
355 struct hostapd_data *hapd = iface->bss[0];
356 struct hostapd_config *newconf, *oldconf;
357 struct wpa_auth_config wpa_auth_conf;
359 newconf = hostapd_config_read(iface->config_fname);
364 * Deauthenticate all stations since the new configuration may not
365 * allow them to use the BSS anymore.
367 hostapd_flush_old_stations(hapd);
369 /* TODO: update dynamic data based on changed configuration
370 * items (e.g., open/close sockets, etc.) */
371 radius_client_flush(hapd->radius, 0);
373 oldconf = hapd->iconf;
374 hapd->iconf = newconf;
375 hapd->conf = &newconf->bss[0];
376 iface->conf = newconf;
378 if (hostapd_setup_wpa_psk(hapd->conf)) {
379 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
380 "after reloading configuration");
383 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
384 hostapd_setup_wpa(hapd);
385 else if (hapd->conf->wpa) {
386 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
387 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
388 } else if (hapd->wpa_auth) {
389 wpa_deinit(hapd->wpa_auth);
390 hapd->wpa_auth = NULL;
391 hostapd_set_privacy(hapd, 0);
392 hostapd_setup_encryption(hapd->conf->iface, hapd);
395 ieee802_11_set_beacon(hapd);
397 hostapd_config_free(oldconf);
399 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
405 #ifndef CONFIG_NATIVE_WINDOWS
407 * handle_reload - SIGHUP handler to reload configuration
409 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
411 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
414 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
417 for (i = 0; i < hapds->count; i++) {
418 if (hostapd_reload_config(hapds->iface[i]) < 0) {
419 wpa_printf(MSG_WARNING, "Failed to read new "
420 "configuration file - continuing with "
428 #ifdef HOSTAPD_DUMP_STATE
430 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
432 static void hostapd_dump_state(struct hostapd_data *hapd)
436 struct sta_info *sta;
440 if (!hapd->conf->dump_log_name) {
441 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
446 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
447 hapd->conf->dump_log_name);
448 f = fopen(hapd->conf->dump_log_name, "w");
450 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
451 "writing.", hapd->conf->dump_log_name);
456 fprintf(f, "hostapd state dump - %s", ctime(&now));
457 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
458 "num_sta_no_short_slot_time=%d\n"
459 "num_sta_no_short_preamble=%d\n",
460 hapd->num_sta, hapd->iface->num_sta_non_erp,
461 hapd->iface->num_sta_no_short_slot_time,
462 hapd->iface->num_sta_no_short_preamble);
464 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
465 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
468 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
469 " capability=0x%x listen_interval=%d\n",
472 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
473 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
474 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
475 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
476 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
477 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
479 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
481 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
482 "[SHORT_PREAMBLE]" : ""),
483 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
484 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
485 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
486 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
487 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
488 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
490 sta->listen_interval);
492 fprintf(f, " supported_rates=");
493 for (i = 0; i < sta->supported_rates_len; i++)
494 fprintf(f, "%02x ", sta->supported_rates[i]);
498 " timeout_next=%s\n",
499 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
500 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
503 ieee802_1x_dump_state(f, " ", sta);
506 buf = os_malloc(4096);
508 int count = radius_client_get_mib(hapd->radius, buf, 4096);
511 else if (count > 4095)
514 fprintf(f, "%s", buf);
516 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
519 else if (count > 4095)
522 fprintf(f, "%s", buf);
527 #endif /* HOSTAPD_DUMP_STATE */
530 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
532 #ifdef HOSTAPD_DUMP_STATE
533 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
536 for (i = 0; i < hapds->count; i++) {
537 struct hostapd_iface *hapd_iface = hapds->iface[i];
538 for (j = 0; j < hapd_iface->num_bss; j++)
539 hostapd_dump_state(hapd_iface->bss[j]);
541 #endif /* HOSTAPD_DUMP_STATE */
543 #endif /* CONFIG_NATIVE_WINDOWS */
545 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
550 for (i = 0; i < NUM_WEP_KEYS; i++) {
551 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
552 0, i == 0 ? 1 : 0)) {
553 wpa_printf(MSG_DEBUG, "Failed to clear default "
554 "encryption keys (ifname=%s keyidx=%d)",
558 #ifdef CONFIG_IEEE80211W
559 if (hapd->conf->ieee80211w) {
560 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
561 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
564 wpa_printf(MSG_DEBUG, "Failed to clear "
565 "default mgmt encryption keys "
566 "(ifname=%s keyidx=%d)", ifname, i);
570 #endif /* CONFIG_IEEE80211W */
574 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
576 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
581 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
584 struct hostapd_ssid *ssid = &hapd->conf->ssid;
587 if (ssid->wep.default_len &&
588 hostapd_set_encryption(hapd->conf->iface,
589 hapd, "WEP", NULL, idx,
592 idx == ssid->wep.idx)) {
593 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
597 if (ssid->dyn_vlan_keys) {
599 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
601 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
604 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
610 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
614 wpa_printf(MSG_WARNING, "Could not set "
615 "dynamic VLAN WEP encryption.");
625 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
626 * @hapd: Pointer to BSS data
628 * This function is used to free all per-BSS data structures and resources.
629 * This gets called in a loop for each BSS between calls to
630 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
631 * is deinitialized. Most of the modules that are initialized in
632 * hostapd_setup_bss() are deinitialized here.
634 static void hostapd_cleanup(struct hostapd_data *hapd)
636 hostapd_ctrl_iface_deinit(hapd);
638 os_free(hapd->default_wep_key);
639 hapd->default_wep_key = NULL;
640 iapp_deinit(hapd->iapp);
642 accounting_deinit(hapd);
643 rsn_preauth_iface_deinit(hapd);
644 if (hapd->wpa_auth) {
645 wpa_deinit(hapd->wpa_auth);
646 hapd->wpa_auth = NULL;
648 if (hostapd_set_privacy(hapd, 0)) {
649 wpa_printf(MSG_DEBUG, "Could not disable "
650 "PrivacyInvoked for interface %s",
654 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
655 wpa_printf(MSG_DEBUG, "Could not remove generic "
656 "information element from interface %s",
660 ieee802_1x_deinit(hapd);
662 hostapd_acl_deinit(hapd);
663 radius_client_deinit(hapd->radius);
665 radius_server_deinit(hapd->radius_srv);
666 hapd->radius_srv = NULL;
668 #ifdef CONFIG_IEEE80211R
669 l2_packet_deinit(hapd->l2);
670 #endif /* CONFIG_IEEE80211R */
672 hostapd_deinit_wps(hapd);
674 hostapd_wireless_event_deinit(hapd);
678 tls_deinit(hapd->ssl_ctx);
679 hapd->ssl_ctx = NULL;
681 #endif /* EAP_TLS_FUNCS */
684 if (hapd->eap_sim_db_priv) {
685 eap_sim_db_deinit(hapd->eap_sim_db_priv);
686 hapd->eap_sim_db_priv = NULL;
688 #endif /* EAP_SERVER */
690 if (hapd->interface_added &&
691 hostapd_bss_remove(hapd, hapd->conf->iface)) {
692 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
699 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
700 * @iface: Pointer to interface data
702 * This function is called before per-BSS data structures are deinitialized
703 * with hostapd_cleanup().
705 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
711 * hostapd_cleanup_iface - Complete per-interface cleanup
712 * @iface: Pointer to interface data
714 * This function is called after per-BSS data structures are deinitialized
715 * with hostapd_cleanup().
717 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
719 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
720 iface->hw_features = NULL;
721 os_free(iface->current_rates);
722 iface->current_rates = NULL;
723 ap_list_deinit(iface);
724 hostapd_config_free(iface->conf);
727 os_free(iface->config_fname);
733 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
737 hostapd_broadcast_wep_set(hapd);
739 if (hapd->conf->ssid.wep.default_len)
742 for (i = 0; i < 4; i++) {
743 if (hapd->conf->ssid.wep.key[i] &&
744 hostapd_set_encryption(iface, hapd, "WEP", NULL,
745 i, hapd->conf->ssid.wep.key[i],
746 hapd->conf->ssid.wep.len[i],
747 i == hapd->conf->ssid.wep.idx)) {
748 wpa_printf(MSG_WARNING, "Could not set WEP "
752 if (hapd->conf->ssid.wep.key[i] &&
753 i == hapd->conf->ssid.wep.idx)
754 hostapd_set_privacy(hapd, 1);
761 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
765 if (hostapd_drv_none(hapd))
768 wpa_printf(MSG_DEBUG, "Flushing old station entries");
769 if (hostapd_flush(hapd)) {
770 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
773 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
774 hostapd_deauth_all_stas(hapd);
780 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
781 logger_level level, const char *txt)
783 struct hostapd_data *hapd = ctx;
788 hlevel = HOSTAPD_LEVEL_WARNING;
791 hlevel = HOSTAPD_LEVEL_INFO;
795 hlevel = HOSTAPD_LEVEL_DEBUG;
799 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
803 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
806 struct hostapd_data *hapd = ctx;
807 struct sta_info *sta;
809 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
810 "STA " MACSTR " reason %d",
811 __func__, MAC2STR(addr), reason);
813 sta = ap_get_sta(hapd, addr);
814 hostapd_sta_deauth(hapd, addr, reason);
817 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
818 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
819 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
820 sta->timeout_next = STA_REMOVE;
824 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
826 struct hostapd_data *hapd = ctx;
827 ieee80211_michael_mic_failure(hapd, addr, 0);
831 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
832 wpa_eapol_variable var, int value)
834 struct hostapd_data *hapd = ctx;
835 struct sta_info *sta = ap_get_sta(hapd, addr);
839 case WPA_EAPOL_portEnabled:
840 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
842 case WPA_EAPOL_portValid:
843 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
845 case WPA_EAPOL_authorized:
846 ieee802_1x_set_sta_authorized(hapd, sta, value);
848 case WPA_EAPOL_portControl_Auto:
850 sta->eapol_sm->portControl = Auto;
852 case WPA_EAPOL_keyRun:
854 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
856 case WPA_EAPOL_keyAvailable:
858 sta->eapol_sm->eap_if->eapKeyAvailable =
859 value ? TRUE : FALSE;
861 case WPA_EAPOL_keyDone:
863 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
865 case WPA_EAPOL_inc_EapolFramesTx:
867 sta->eapol_sm->dot1xAuthEapolFramesTx++;
873 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
874 wpa_eapol_variable var)
876 struct hostapd_data *hapd = ctx;
877 struct sta_info *sta = ap_get_sta(hapd, addr);
878 if (sta == NULL || sta->eapol_sm == NULL)
881 case WPA_EAPOL_keyRun:
882 return sta->eapol_sm->keyRun;
883 case WPA_EAPOL_keyAvailable:
884 return sta->eapol_sm->eap_if->eapKeyAvailable;
891 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
894 struct hostapd_data *hapd = ctx;
895 return hostapd_get_psk(hapd->conf, addr, prev_psk);
899 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
902 struct hostapd_data *hapd = ctx;
905 struct sta_info *sta;
907 sta = ap_get_sta(hapd, addr);
911 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
917 os_memcpy(msk, key, keylen);
924 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
925 const u8 *addr, int idx, u8 *key,
928 struct hostapd_data *hapd = ctx;
929 const char *ifname = hapd->conf->iface;
932 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
937 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
942 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
945 struct hostapd_data *hapd = ctx;
946 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
950 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
953 struct hostapd_data *hapd = ctx;
954 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
959 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
960 const u8 *data, size_t data_len,
963 struct hostapd_data *hapd = ctx;
964 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
968 static int hostapd_wpa_auth_for_each_sta(
969 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
972 struct hostapd_data *hapd = ctx;
973 struct sta_info *sta;
975 for (sta = hapd->sta_list; sta; sta = sta->next) {
976 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
983 static int hostapd_wpa_auth_for_each_auth(
984 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
987 struct hostapd_data *ohapd;
989 struct hapd_interfaces *interfaces = eloop_get_user_data();
991 for (i = 0; i < interfaces->count; i++) {
992 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
993 ohapd = interfaces->iface[i]->bss[j];
994 if (cb(ohapd->wpa_auth, cb_ctx))
1003 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
1004 const u8 *data, size_t data_len)
1006 struct hostapd_data *hapd = ctx;
1008 if (hapd->driver && hapd->driver->send_ether)
1009 return hapd->driver->send_ether(hapd->drv_priv, dst,
1010 hapd->own_addr, proto,
1012 if (hapd->l2 == NULL)
1014 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
1018 #ifdef CONFIG_IEEE80211R
1020 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1021 const u8 *data, size_t data_len)
1023 struct hostapd_data *hapd = ctx;
1025 struct ieee80211_mgmt *m;
1027 struct sta_info *sta;
1029 sta = ap_get_sta(hapd, dst);
1030 if (sta == NULL || sta->wpa_sm == NULL)
1033 m = os_zalloc(sizeof(*m) + data_len);
1036 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1037 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1038 WLAN_FC_STYPE_ACTION);
1039 os_memcpy(m->da, dst, ETH_ALEN);
1040 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1041 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1042 os_memcpy(&m->u, data, data_len);
1044 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1050 static struct wpa_state_machine *
1051 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1053 struct hostapd_data *hapd = ctx;
1054 struct sta_info *sta;
1056 sta = ap_sta_add(hapd, sta_addr);
1062 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1063 if (sta->wpa_sm == NULL) {
1064 ap_free_sta(hapd, sta);
1067 sta->auth_alg = WLAN_AUTH_FT;
1073 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1076 struct hostapd_data *hapd = ctx;
1077 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1080 #endif /* CONFIG_IEEE80211R */
1084 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1085 * @iface: Pointer to interface data
1086 * Returns: 0 on success, -1 on failure
1088 * This function is used to validate that the configured BSSIDs are valid.
1090 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1092 u8 mask[ETH_ALEN] = { 0 };
1093 struct hostapd_data *hapd = iface->bss[0];
1094 unsigned int i = iface->conf->num_bss, bits = 0, j;
1097 if (hostapd_drv_none(hapd))
1100 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1102 /* Determine the bits necessary to cover the number of BSSIDs. */
1103 for (i--; i; i >>= 1)
1106 /* Determine the bits necessary to any configured BSSIDs,
1107 if they are higher than the number of BSSIDs. */
1108 for (j = 0; j < iface->conf->num_bss; j++) {
1109 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1112 for (i = 0; i < ETH_ALEN; i++) {
1114 iface->conf->bss[j].bssid[i] ^
1119 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1125 while (mask[i] != 0) {
1137 os_memset(mask, 0xff, ETH_ALEN);
1139 for (i = 5; i > 5 - j; i--)
1145 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1146 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1148 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1153 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1154 MACSTR " for start address " MACSTR ".",
1155 MAC2STR(mask), MAC2STR(hapd->own_addr));
1159 for (i = 0; i < ETH_ALEN; i++) {
1160 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1161 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1162 " for start address " MACSTR ".",
1163 MAC2STR(mask), MAC2STR(hapd->own_addr));
1164 wpa_printf(MSG_ERROR, "Start address must be the "
1165 "first address in the block (i.e., addr "
1166 "AND mask == addr).");
1175 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1179 for (i = 0; i < conf->num_bss; i++) {
1180 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1189 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1191 struct wpa_auth_config _conf;
1192 struct wpa_auth_callbacks cb;
1196 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1197 os_memset(&cb, 0, sizeof(cb));
1199 cb.logger = hostapd_wpa_auth_logger;
1200 cb.disconnect = hostapd_wpa_auth_disconnect;
1201 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1202 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1203 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1204 cb.get_psk = hostapd_wpa_auth_get_psk;
1205 cb.get_msk = hostapd_wpa_auth_get_msk;
1206 cb.set_key = hostapd_wpa_auth_set_key;
1207 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1208 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1209 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1210 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1211 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1212 cb.send_ether = hostapd_wpa_auth_send_ether;
1213 #ifdef CONFIG_IEEE80211R
1214 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1215 cb.add_sta = hostapd_wpa_auth_add_sta;
1216 #endif /* CONFIG_IEEE80211R */
1217 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1218 if (hapd->wpa_auth == NULL) {
1219 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1223 if (hostapd_set_privacy(hapd, 1)) {
1224 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1225 "for interface %s", hapd->conf->iface);
1229 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1230 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1231 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1232 "the kernel driver.");
1236 if (rsn_preauth_iface_init(hapd)) {
1237 wpa_printf(MSG_ERROR, "Initialization of RSN "
1238 "pre-authentication failed.");
1247 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1248 struct hostapd_bss_config *conf)
1250 struct radius_server_conf srv;
1251 os_memset(&srv, 0, sizeof(srv));
1252 srv.client_file = conf->radius_server_clients;
1253 srv.auth_port = conf->radius_server_auth_port;
1254 srv.conf_ctx = conf;
1255 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1256 srv.ssl_ctx = hapd->ssl_ctx;
1257 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1258 srv.eap_fast_a_id = conf->eap_fast_a_id;
1259 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1260 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1261 srv.eap_fast_prov = conf->eap_fast_prov;
1262 srv.pac_key_lifetime = conf->pac_key_lifetime;
1263 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1264 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1265 srv.tnc = conf->tnc;
1266 srv.wps = hapd->wps;
1267 srv.ipv6 = conf->radius_server_ipv6;
1268 srv.get_eap_user = hostapd_radius_get_eap_user;
1269 srv.eap_req_id_text = conf->eap_req_id_text;
1270 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1272 hapd->radius_srv = radius_server_init(&srv);
1273 if (hapd->radius_srv == NULL) {
1274 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1283 * hostapd_setup_bss - Per-BSS setup (initialization)
1284 * @hapd: Pointer to BSS data
1285 * @first: Whether this BSS is the first BSS of an interface
1287 * This function is used to initialize all per-BSS data structures and
1288 * resources. This gets called in a loop for each BSS when an interface is
1289 * initialized. Most of the modules that are initialized here will be
1290 * deinitialized in hostapd_cleanup().
1292 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1294 struct hostapd_bss_config *conf = hapd->conf;
1295 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1296 int ssid_len, set_ssid;
1299 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1300 /* Allocate the next available BSSID. */
1302 inc_byte_array(hapd->own_addr, ETH_ALEN);
1303 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1305 /* Allocate the configured BSSID. */
1306 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1308 if (hostapd_mac_comp(hapd->own_addr,
1309 hapd->iface->bss[0]->own_addr) ==
1311 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1312 "BSSID set to the MAC address of "
1313 "the radio", hapd->conf->iface);
1318 hapd->interface_added = 1;
1319 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1321 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1322 MACSTR ")", MAC2STR(hapd->own_addr));
1328 * Fetch the SSID from the system and use it or,
1329 * if one was specified in the config file, verify they
1332 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1334 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1337 if (conf->ssid.ssid_set) {
1339 * If SSID is specified in the config file and it differs
1340 * from what is being used then force installation of the
1343 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1344 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1347 * No SSID in the config file; just use the one we got
1351 conf->ssid.ssid_len = ssid_len;
1352 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1353 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1356 if (!hostapd_drv_none(hapd)) {
1357 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1359 hapd->conf->iface, MAC2STR(hapd->own_addr),
1360 hapd->conf->ssid.ssid);
1363 if (hostapd_setup_wpa_psk(conf)) {
1364 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1368 /* Set flag for whether SSID is broadcast in beacons */
1369 if (hostapd_set_broadcast_ssid(hapd,
1370 !!hapd->conf->ignore_broadcast_ssid)) {
1371 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1376 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1377 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1382 /* Set SSID for the kernel driver (to be used in beacon and probe
1383 * response frames) */
1384 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1385 conf->ssid.ssid_len)) {
1386 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1390 if (wpa_debug_level == MSG_MSGDUMP)
1391 conf->radius->msg_dumps = 1;
1392 hapd->radius = radius_client_init(hapd, conf->radius);
1393 if (hapd->radius == NULL) {
1394 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1398 if (hostapd_acl_init(hapd)) {
1399 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1402 if (hostapd_init_wps(hapd, conf))
1405 if (ieee802_1x_init(hapd)) {
1406 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1410 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1413 if (accounting_init(hapd)) {
1414 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1418 if (hapd->conf->ieee802_11f &&
1419 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1420 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1425 if (hostapd_ctrl_iface_init(hapd)) {
1426 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1430 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1431 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1435 #ifdef CONFIG_IEEE80211R
1436 if (!hostapd_drv_none(hapd)) {
1437 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1438 hostapd_rrb_receive, hapd, 0);
1439 if (hapd->l2 == NULL &&
1440 (hapd->driver == NULL ||
1441 hapd->driver->send_ether == NULL)) {
1442 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1447 #endif /* CONFIG_IEEE80211R */
1449 ieee802_11_set_beacon(hapd);
1451 if (conf->radius_server_clients &&
1452 hostapd_setup_radius_srv(hapd, conf))
1459 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1461 struct hostapd_data *hapd = iface->bss[0];
1463 struct hostapd_tx_queue_params *p;
1465 for (i = 0; i < NUM_TX_QUEUES; i++) {
1466 p = &iface->conf->tx_queue[i];
1471 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1472 p->cwmax, p->burst)) {
1473 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1474 "parameters for queue %d.", i);
1475 /* Continue anyway */
1481 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1482 size_t identity_len, int phase2,
1483 struct eap_user *user)
1485 const struct hostapd_eap_user *eap_user;
1488 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1489 if (eap_user == NULL)
1495 os_memset(user, 0, sizeof(*user));
1496 count = EAP_USER_MAX_METHODS;
1497 if (count > EAP_MAX_METHODS)
1498 count = EAP_MAX_METHODS;
1499 for (i = 0; i < count; i++) {
1500 user->methods[i].vendor = eap_user->methods[i].vendor;
1501 user->methods[i].method = eap_user->methods[i].method;
1504 if (eap_user->password) {
1505 user->password = os_malloc(eap_user->password_len);
1506 if (user->password == NULL)
1508 os_memcpy(user->password, eap_user->password,
1509 eap_user->password_len);
1510 user->password_len = eap_user->password_len;
1511 user->password_hash = eap_user->password_hash;
1513 user->force_version = eap_user->force_version;
1514 user->ttls_auth = eap_user->ttls_auth;
1520 static int setup_interface(struct hostapd_iface *iface)
1522 struct hostapd_data *hapd = iface->bss[0];
1523 struct hostapd_bss_config *conf = hapd->conf;
1526 u8 *b = conf->bssid;
1533 * Initialize the driver interface and make sure that all BSSes get
1534 * configured with a pointer to this driver interface.
1536 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1537 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1539 hapd->drv_priv = hostapd_driver_init(hapd);
1542 if (hapd->drv_priv == NULL) {
1543 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1544 hapd->driver ? hapd->driver->name : "Unknown");
1545 hapd->driver = NULL;
1548 for (i = 0; i < iface->num_bss; i++) {
1549 iface->bss[i]->driver = hapd->driver;
1550 iface->bss[i]->drv_priv = hapd->drv_priv;
1553 if (hostapd_validate_bssid_configuration(iface))
1556 #ifdef CONFIG_IEEE80211N
1557 SET_2BIT_LE16(&iface->ht_op_mode,
1558 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1560 #endif /* CONFIG_IEEE80211N */
1562 os_memcpy(country, hapd->iconf->country, 3);
1564 if (hostapd_set_country(hapd, country) < 0) {
1565 wpa_printf(MSG_ERROR, "Failed to set country code");
1569 if (hapd->iconf->ieee80211d &&
1570 hostapd_set_ieee80211d(hapd, 1) < 0) {
1571 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1572 hapd->iconf->ieee80211d);
1576 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1577 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1578 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1583 /* TODO: merge with hostapd_driver_init() ? */
1584 if (hostapd_wireless_event_init(hapd) < 0)
1587 if (hostapd_get_hw_features(iface)) {
1588 /* Not all drivers support this yet, so continue without hw
1591 int ret = hostapd_select_hw_mode(iface);
1593 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1594 "channel. (%d)", ret);
1599 hostapd_flush_old_stations(hapd);
1600 hostapd_set_privacy(hapd, 0);
1602 if (hapd->iconf->channel) {
1603 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1604 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1605 "Frequency: %d MHz",
1606 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1607 hapd->iconf->channel, freq);
1609 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1610 hapd->iconf->ieee80211n,
1611 hapd->iconf->secondary_channel)) {
1612 wpa_printf(MSG_ERROR, "Could not set channel for "
1618 hostapd_broadcast_wep_clear(hapd);
1619 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1622 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1623 ieee802_11_set_beacon(hapd);
1625 if (hapd->iconf->rts_threshold > -1 &&
1626 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1627 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1632 if (hapd->iconf->fragm_threshold > -1 &&
1633 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1634 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1635 "for kernel driver");
1639 prev_addr = hapd->own_addr;
1641 for (j = 0; j < iface->num_bss; j++) {
1642 hapd = iface->bss[j];
1644 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1645 if (hostapd_setup_bss(hapd, j == 0))
1647 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1648 prev_addr = hapd->own_addr;
1651 hostapd_tx_queue_params(iface);
1653 ap_list_init(iface);
1655 if (hostapd_driver_commit(hapd) < 0) {
1656 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1657 "configuration", __func__);
1666 * hostapd_setup_interface - Setup of an interface
1667 * @iface: Pointer to interface data.
1668 * Returns: 0 on success, -1 on failure
1670 * Initializes the driver interface, validates the configuration,
1671 * and sets driver parameters based on the configuration.
1672 * Flushes old stations, sets the channel, encryption,
1673 * beacons, and WDS links based on the configuration.
1675 static int hostapd_setup_interface(struct hostapd_iface *iface)
1679 ret = setup_interface(iface);
1681 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1682 iface->bss[0]->conf->iface);
1685 } else if (!hostapd_drv_none(iface->bss[0])) {
1686 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1687 iface->bss[0]->conf->iface);
1694 static void show_version(void)
1697 "hostapd v" VERSION_STR "\n"
1698 "User space daemon for IEEE 802.11 AP management,\n"
1699 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1700 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1701 "and contributors\n");
1705 static void usage(void)
1710 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1711 "<configuration file(s)>\n"
1714 " -h show this usage\n"
1715 " -d show more debug messages (-dd for even more)\n"
1716 " -B run daemon in the background\n"
1718 " -K include key data in debug messages\n"
1719 " -t include timestamps in some debug messages\n"
1720 " -v show hostapd version\n");
1727 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1728 * @hapd_iface: Pointer to interface data
1729 * @conf: Pointer to per-interface configuration
1730 * @bss: Pointer to per-BSS configuration for this BSS
1731 * Returns: Pointer to allocated BSS data
1733 * This function is used to allocate per-BSS data structure. This data will be
1734 * freed after hostapd_cleanup() is called for it during interface
1737 static struct hostapd_data *
1738 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1739 struct hostapd_config *conf,
1740 struct hostapd_bss_config *bss)
1742 struct hostapd_data *hapd;
1744 hapd = os_zalloc(sizeof(*hapd));
1750 hapd->iface = hapd_iface;
1752 if (hapd->conf->individual_wep_key_len > 0) {
1753 /* use key0 in individual key and key1 in broadcast key */
1754 hapd->default_wep_key_idx = 1;
1757 #ifdef EAP_TLS_FUNCS
1758 if (hapd->conf->eap_server &&
1759 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1760 hapd->conf->dh_file)) {
1761 struct tls_connection_params params;
1763 hapd->ssl_ctx = tls_init(NULL);
1764 if (hapd->ssl_ctx == NULL) {
1765 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1769 os_memset(¶ms, 0, sizeof(params));
1770 params.ca_cert = hapd->conf->ca_cert;
1771 params.client_cert = hapd->conf->server_cert;
1772 params.private_key = hapd->conf->private_key;
1773 params.private_key_passwd = hapd->conf->private_key_passwd;
1774 params.dh_file = hapd->conf->dh_file;
1776 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1777 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1781 if (tls_global_set_verify(hapd->ssl_ctx,
1782 hapd->conf->check_crl)) {
1783 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1787 #endif /* EAP_TLS_FUNCS */
1790 if (hapd->conf->eap_sim_db) {
1791 hapd->eap_sim_db_priv =
1792 eap_sim_db_init(hapd->conf->eap_sim_db,
1793 hostapd_sim_db_cb, hapd);
1794 if (hapd->eap_sim_db_priv == NULL) {
1795 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1796 "database interface");
1800 #endif /* EAP_SERVER */
1802 hapd->driver = hapd->iconf->driver;
1806 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1809 /* TODO: cleanup allocated resources(?) */
1816 * hostapd_init - Allocate and initialize per-interface data
1817 * @config_file: Path to the configuration file
1818 * Returns: Pointer to the allocated interface data or %NULL on failure
1820 * This function is used to allocate main data structures for per-interface
1821 * data. The allocated data buffer will be freed by calling
1822 * hostapd_cleanup_iface().
1824 static struct hostapd_iface * hostapd_init(const char *config_file)
1826 struct hostapd_iface *hapd_iface = NULL;
1827 struct hostapd_config *conf = NULL;
1828 struct hostapd_data *hapd;
1831 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1832 if (hapd_iface == NULL)
1835 hapd_iface->config_fname = os_strdup(config_file);
1836 if (hapd_iface->config_fname == NULL)
1839 conf = hostapd_config_read(hapd_iface->config_fname);
1842 hapd_iface->conf = conf;
1844 hapd_iface->num_bss = conf->num_bss;
1845 hapd_iface->bss = os_zalloc(conf->num_bss *
1846 sizeof(struct hostapd_data *));
1847 if (hapd_iface->bss == NULL)
1850 for (i = 0; i < conf->num_bss; i++) {
1851 hapd = hapd_iface->bss[i] =
1852 hostapd_alloc_bss_data(hapd_iface, conf,
1862 hostapd_config_free(conf);
1864 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1865 hapd = hapd_iface->bss[i];
1866 if (hapd && hapd->ssl_ctx)
1867 tls_deinit(hapd->ssl_ctx);
1870 os_free(hapd_iface->config_fname);
1871 os_free(hapd_iface->bss);
1872 os_free(hapd_iface);
1878 int main(int argc, char *argv[])
1880 struct hapd_interfaces interfaces;
1883 int c, debug = 0, daemonize = 0, tnc = 0;
1884 const char *pid_file = NULL;
1886 hostapd_logger_register_cb(hostapd_logger_cb);
1889 c = getopt(argc, argv, "BdhKP:tv");
1898 if (wpa_debug_level > 0)
1905 wpa_debug_show_keys++;
1911 wpa_debug_timestamp++;
1927 if (eap_server_register_methods()) {
1928 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1932 interfaces.count = argc - optind;
1934 interfaces.iface = os_malloc(interfaces.count *
1935 sizeof(struct hostapd_iface *));
1936 if (interfaces.iface == NULL) {
1937 wpa_printf(MSG_ERROR, "malloc failed\n");
1941 if (eloop_init(&interfaces)) {
1942 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1946 #ifndef CONFIG_NATIVE_WINDOWS
1947 eloop_register_signal(SIGHUP, handle_reload, NULL);
1948 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1949 #endif /* CONFIG_NATIVE_WINDOWS */
1950 eloop_register_signal_terminate(handle_term, NULL);
1952 /* Initialize interfaces */
1953 for (i = 0; i < interfaces.count; i++) {
1954 wpa_printf(MSG_ERROR, "Configuration file: %s",
1956 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1957 if (!interfaces.iface[i])
1959 for (k = 0; k < debug; k++) {
1960 if (interfaces.iface[i]->bss[0]->conf->
1961 logger_stdout_level > 0)
1962 interfaces.iface[i]->bss[0]->conf->
1963 logger_stdout_level--;
1966 ret = hostapd_setup_interface(interfaces.iface[i]);
1970 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1971 if (interfaces.iface[i]->bss[0]->conf->tnc)
1977 if (tnc && tncs_global_init() < 0) {
1978 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1981 #endif /* EAP_TNC */
1983 if (daemonize && os_daemonize(pid_file)) {
1988 #ifndef CONFIG_NATIVE_WINDOWS
1989 openlog("hostapd", 0, LOG_DAEMON);
1990 #endif /* CONFIG_NATIVE_WINDOWS */
1994 /* Disconnect associated stations from all interfaces and BSSes */
1995 for (i = 0; i < interfaces.count; i++) {
1996 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1997 struct hostapd_data *hapd =
1998 interfaces.iface[i]->bss[j];
1999 hostapd_free_stas(hapd);
2000 hostapd_flush_old_stations(hapd);
2007 /* Deinitialize all interfaces */
2008 for (i = 0; i < interfaces.count; i++) {
2009 if (!interfaces.iface[i])
2011 hostapd_cleanup_iface_pre(interfaces.iface[i]);
2012 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2013 struct hostapd_data *hapd =
2014 interfaces.iface[i]->bss[j];
2015 hostapd_cleanup(hapd);
2016 if (j == interfaces.iface[i]->num_bss - 1 &&
2018 hostapd_driver_deinit(hapd);
2020 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2021 os_free(interfaces.iface[i]->bss[j]);
2022 hostapd_cleanup_iface(interfaces.iface[i]);
2024 os_free(interfaces.iface);
2027 tncs_global_deinit();
2028 #endif /* EAP_TNC */
2032 #ifndef CONFIG_NATIVE_WINDOWS
2034 #endif /* CONFIG_NATIVE_WINDOWS */
2036 eap_server_unregister_methods();
2038 os_daemonize_terminate(pid_file);