2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2008, 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"
45 #include "l2_packet/l2_packet.h"
48 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
49 size_t identity_len, int phase2,
50 struct eap_user *user);
52 struct hapd_interfaces {
54 struct hostapd_iface **iface;
57 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
60 extern int wpa_debug_level;
61 extern int wpa_debug_show_keys;
62 extern int wpa_debug_timestamp;
65 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
66 int level, const char *txt, size_t len)
68 struct hostapd_data *hapd = ctx;
69 char *format, *module_str;
71 int conf_syslog_level, conf_stdout_level;
72 unsigned int conf_syslog, conf_stdout;
75 format = os_malloc(maxlen);
79 if (hapd && hapd->conf) {
80 conf_syslog_level = hapd->conf->logger_syslog_level;
81 conf_stdout_level = hapd->conf->logger_stdout_level;
82 conf_syslog = hapd->conf->logger_syslog;
83 conf_stdout = hapd->conf->logger_stdout;
85 conf_syslog_level = conf_stdout_level = 0;
86 conf_syslog = conf_stdout = (unsigned int) -1;
90 case HOSTAPD_MODULE_IEEE80211:
91 module_str = "IEEE 802.11";
93 case HOSTAPD_MODULE_IEEE8021X:
94 module_str = "IEEE 802.1X";
96 case HOSTAPD_MODULE_RADIUS:
97 module_str = "RADIUS";
99 case HOSTAPD_MODULE_WPA:
102 case HOSTAPD_MODULE_DRIVER:
103 module_str = "DRIVER";
105 case HOSTAPD_MODULE_IAPP:
108 case HOSTAPD_MODULE_MLME:
116 if (hapd && hapd->conf && addr)
117 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
118 hapd->conf->iface, MAC2STR(addr),
119 module_str ? " " : "", module_str, txt);
120 else if (hapd && hapd->conf)
121 os_snprintf(format, maxlen, "%s:%s%s %s",
122 hapd->conf->iface, module_str ? " " : "",
125 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
126 MAC2STR(addr), module_str ? " " : "",
129 os_snprintf(format, maxlen, "%s%s%s",
130 module_str, module_str ? ": " : "", txt);
132 if ((conf_stdout & module) && level >= conf_stdout_level) {
133 wpa_debug_print_timestamp();
134 printf("%s\n", format);
137 #ifndef CONFIG_NATIVE_WINDOWS
138 if ((conf_syslog & module) && level >= conf_syslog_level) {
141 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
142 case HOSTAPD_LEVEL_DEBUG:
143 priority = LOG_DEBUG;
145 case HOSTAPD_LEVEL_INFO:
148 case HOSTAPD_LEVEL_NOTICE:
149 priority = LOG_NOTICE;
151 case HOSTAPD_LEVEL_WARNING:
152 priority = LOG_WARNING;
158 syslog(priority, "%s", format);
160 #endif /* CONFIG_NATIVE_WINDOWS */
166 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
171 os_memset(addr, 0xff, ETH_ALEN);
172 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
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 a while
177 * until the issue has been resolved. */
183 * hostapd_prune_associations - Remove extraneous associations
184 * @hapd: Pointer to BSS data for the most recent association
185 * @sta: Pointer to the associated STA data
187 * This function looks through all radios and BSS's for previous
188 * (stale) associations of STA. If any are found they are removed.
190 static void hostapd_prune_associations(struct hostapd_data *hapd,
191 struct sta_info *sta)
193 struct sta_info *osta;
194 struct hostapd_data *ohapd;
196 struct hapd_interfaces *interfaces = eloop_get_user_data();
198 for (i = 0; i < interfaces->count; i++) {
199 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
200 ohapd = interfaces->iface[i]->bss[j];
203 osta = ap_get_sta(ohapd, sta->addr);
207 ap_sta_disassociate(ohapd, osta,
208 WLAN_REASON_UNSPECIFIED);
215 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
216 * @hapd: Pointer to BSS data
217 * @sta: Pointer to the associated STA data
218 * @reassoc: 1 to indicate this was a re-association; 0 = first association
220 * This function will be called whenever a station associates with the AP. It
221 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
222 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
223 * authentication and association) internally.
225 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
228 if (hapd->tkip_countermeasures) {
229 hostapd_sta_deauth(hapd, sta->addr,
230 WLAN_REASON_MICHAEL_MIC_FAILURE);
234 hostapd_prune_associations(hapd, sta);
236 /* IEEE 802.11F (IAPP) */
237 if (hapd->conf->ieee802_11f)
238 iapp_new_station(hapd->iapp, sta);
240 /* Start accounting here, if IEEE 802.1X and WPA are not used.
241 * IEEE 802.1X/WPA code will start accounting after the station has
242 * been authorized. */
243 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
244 accounting_sta_start(hapd, sta);
246 hostapd_wme_sta_config(hapd, sta);
248 /* Start IEEE 802.1X authentication process for new stations */
249 ieee802_1x_new_station(hapd, sta);
251 if (sta->auth_alg != WLAN_AUTH_FT)
252 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
254 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
259 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
260 struct sta_info *sta, void *ctx)
262 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
268 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
270 struct hostapd_data *hapd = ctx;
271 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
272 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
274 #endif /* EAP_SERVER */
277 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
279 printf("Signal %d received - terminating\n", sig);
284 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
285 struct wpa_auth_config *wconf)
287 wconf->wpa = conf->wpa;
288 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
289 wconf->wpa_pairwise = conf->wpa_pairwise;
290 wconf->wpa_group = conf->wpa_group;
291 wconf->wpa_group_rekey = conf->wpa_group_rekey;
292 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
293 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
294 wconf->rsn_pairwise = conf->rsn_pairwise;
295 wconf->rsn_preauth = conf->rsn_preauth;
296 wconf->eapol_version = conf->eapol_version;
297 wconf->peerkey = conf->peerkey;
298 wconf->wme_enabled = conf->wme_enabled;
299 #ifdef CONFIG_IEEE80211W
300 wconf->ieee80211w = conf->ieee80211w;
301 #endif /* CONFIG_IEEE80211W */
302 #ifdef CONFIG_IEEE80211R
303 wconf->ssid_len = conf->ssid.ssid_len;
304 if (wconf->ssid_len > SSID_LEN)
305 wconf->ssid_len = SSID_LEN;
306 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
307 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
308 MOBILITY_DOMAIN_ID_LEN);
309 if (conf->nas_identifier &&
310 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
311 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
312 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
313 wconf->r0_key_holder_len);
315 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
316 wconf->r0_key_lifetime = conf->r0_key_lifetime;
317 wconf->reassociation_deadline = conf->reassociation_deadline;
318 wconf->r0kh_list = conf->r0kh_list;
319 wconf->r1kh_list = conf->r1kh_list;
320 wconf->pmk_r1_push = conf->pmk_r1_push;
321 #endif /* CONFIG_IEEE80211R */
325 #ifndef CONFIG_NATIVE_WINDOWS
326 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
328 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
329 struct hostapd_config *newconf;
331 struct wpa_auth_config wpa_auth_conf;
333 printf("Signal %d received - reloading configuration\n", sig);
335 for (i = 0; i < hapds->count; i++) {
336 struct hostapd_data *hapd = hapds->iface[i]->bss[0];
337 newconf = hostapd_config_read(hapds->iface[i]->config_fname);
338 if (newconf == NULL) {
339 printf("Failed to read new configuration file - "
340 "continuing with old.\n");
343 /* TODO: update dynamic data based on changed configuration
344 * items (e.g., open/close sockets, remove stations added to
345 * deny list, etc.) */
346 radius_client_flush(hapd->radius, 0);
347 hostapd_config_free(hapd->iconf);
349 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
350 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
352 hapd->iconf = newconf;
353 hapd->conf = &newconf->bss[0];
354 hapds->iface[i]->conf = newconf;
356 if (hostapd_setup_wpa_psk(hapd->conf)) {
357 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
358 "after reloading configuration");
364 #ifdef HOSTAPD_DUMP_STATE
365 static void hostapd_dump_state(struct hostapd_data *hapd)
369 struct sta_info *sta;
373 if (!hapd->conf->dump_log_name) {
374 printf("Dump file not defined - ignoring dump request\n");
378 printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
379 f = fopen(hapd->conf->dump_log_name, "w");
381 printf("Could not open dump file '%s' for writing.\n",
382 hapd->conf->dump_log_name);
387 fprintf(f, "hostapd state dump - %s", ctime(&now));
388 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
389 "num_sta_no_short_slot_time=%d\n"
390 "num_sta_no_short_preamble=%d\n",
391 hapd->num_sta, hapd->iface->num_sta_non_erp,
392 hapd->iface->num_sta_no_short_slot_time,
393 hapd->iface->num_sta_no_short_preamble);
395 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
396 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
399 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s\n"
400 " capability=0x%x listen_interval=%d\n",
403 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
404 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
405 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
406 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
407 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
408 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
410 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
412 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
413 "[SHORT_PREAMBLE]" : ""),
414 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
415 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
417 sta->listen_interval);
419 fprintf(f, " supported_rates=");
420 for (i = 0; i < sta->supported_rates_len; i++)
421 fprintf(f, "%02x ", sta->supported_rates[i]);
425 " timeout_next=%s\n",
426 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
427 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
430 ieee802_1x_dump_state(f, " ", sta);
433 buf = os_malloc(4096);
435 int count = radius_client_get_mib(hapd->radius, buf, 4096);
438 else if (count > 4095)
441 fprintf(f, "%s", buf);
443 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
446 else if (count > 4095)
449 fprintf(f, "%s", buf);
454 #endif /* HOSTAPD_DUMP_STATE */
457 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
459 #ifdef HOSTAPD_DUMP_STATE
460 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
463 for (i = 0; i < hapds->count; i++) {
464 struct hostapd_iface *hapd_iface = hapds->iface[i];
465 for (j = 0; j < hapd_iface->num_bss; j++)
466 hostapd_dump_state(hapd_iface->bss[j]);
468 #endif /* HOSTAPD_DUMP_STATE */
470 #endif /* CONFIG_NATIVE_WINDOWS */
472 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
477 for (i = 0; i < NUM_WEP_KEYS; i++) {
478 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
479 0, i == 0 ? 1 : 0)) {
480 printf("Failed to clear default encryption keys "
481 "(ifname=%s keyidx=%d)\n", ifname, i);
487 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
489 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
494 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
497 struct hostapd_ssid *ssid = &hapd->conf->ssid;
500 if (ssid->wep.default_len &&
501 hostapd_set_encryption(hapd->conf->iface,
502 hapd, "WEP", NULL, idx,
505 idx == ssid->wep.idx)) {
506 printf("Could not set WEP encryption.\n");
510 if (ssid->dyn_vlan_keys) {
512 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
514 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
517 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
523 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
527 printf("Could not set dynamic VLAN WEP "
538 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
539 * @hapd: Pointer to BSS data
541 * This function is used to free all per-BSS data structures and resources.
542 * This gets called in a loop for each BSS between calls to
543 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
544 * is deinitialized. Most of the modules that are initialized in
545 * hostapd_setup_bss() are deinitialized here.
547 static void hostapd_cleanup(struct hostapd_data *hapd)
549 hostapd_ctrl_iface_deinit(hapd);
551 os_free(hapd->default_wep_key);
552 hapd->default_wep_key = NULL;
553 iapp_deinit(hapd->iapp);
555 accounting_deinit(hapd);
556 rsn_preauth_iface_deinit(hapd);
557 if (hapd->wpa_auth) {
558 wpa_deinit(hapd->wpa_auth);
559 hapd->wpa_auth = NULL;
561 if (hostapd_set_privacy(hapd, 0)) {
562 wpa_printf(MSG_DEBUG, "Could not disable "
563 "PrivacyInvoked for interface %s",
567 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
568 wpa_printf(MSG_DEBUG, "Could not remove generic "
569 "information element from interface %s",
573 ieee802_1x_deinit(hapd);
575 hostapd_acl_deinit(hapd);
576 radius_client_deinit(hapd->radius);
578 radius_server_deinit(hapd->radius_srv);
579 hapd->radius_srv = NULL;
581 #ifdef CONFIG_IEEE80211R
582 l2_packet_deinit(hapd->l2);
583 #endif /* CONFIG_IEEE80211R */
585 hostapd_wireless_event_deinit(hapd);
589 tls_deinit(hapd->ssl_ctx);
590 hapd->ssl_ctx = NULL;
592 #endif /* EAP_TLS_FUNCS */
595 if (hapd->eap_sim_db_priv) {
596 eap_sim_db_deinit(hapd->eap_sim_db_priv);
597 hapd->eap_sim_db_priv = NULL;
599 #endif /* EAP_SERVER */
601 if (hapd->interface_added &&
602 hostapd_bss_remove(hapd, hapd->conf->iface)) {
603 printf("Failed to remove BSS interface %s\n",
610 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
611 * @iface: Pointer to interface data
613 * This function is called before per-BSS data structures are deinitialized
614 * with hostapd_cleanup().
616 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
622 * hostapd_cleanup_iface - Complete per-interface cleanup
623 * @iface: Pointer to interface data
625 * This function is called after per-BSS data structures are deinitialized
626 * with hostapd_cleanup().
628 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
630 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
631 iface->hw_features = NULL;
632 os_free(iface->current_rates);
633 iface->current_rates = NULL;
634 ap_list_deinit(iface);
635 hostapd_config_free(iface->conf);
638 os_free(iface->config_fname);
644 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
648 hostapd_broadcast_wep_set(hapd);
650 if (hapd->conf->ssid.wep.default_len)
653 for (i = 0; i < 4; i++) {
654 if (hapd->conf->ssid.wep.key[i] &&
655 hostapd_set_encryption(iface, hapd, "WEP", NULL,
656 i, hapd->conf->ssid.wep.key[i],
657 hapd->conf->ssid.wep.len[i],
658 i == hapd->conf->ssid.wep.idx)) {
659 printf("Could not set WEP encryption.\n");
662 if (hapd->conf->ssid.wep.key[i] &&
663 i == hapd->conf->ssid.wep.idx)
664 hostapd_set_privacy(hapd, 1);
671 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
675 wpa_printf(MSG_DEBUG, "Flushing old station entries");
676 if (hostapd_flush(hapd)) {
677 printf("Could not connect to kernel driver.\n");
680 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
681 hostapd_deauth_all_stas(hapd);
687 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
688 logger_level level, const char *txt)
690 struct hostapd_data *hapd = ctx;
695 hlevel = HOSTAPD_LEVEL_WARNING;
698 hlevel = HOSTAPD_LEVEL_INFO;
702 hlevel = HOSTAPD_LEVEL_DEBUG;
706 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
710 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
713 struct hostapd_data *hapd = ctx;
714 struct sta_info *sta;
716 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
717 "STA " MACSTR " reason %d",
718 __func__, MAC2STR(addr), reason);
720 sta = ap_get_sta(hapd, addr);
721 hostapd_sta_deauth(hapd, addr, reason);
724 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
725 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
726 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
727 sta->timeout_next = STA_REMOVE;
731 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
733 struct hostapd_data *hapd = ctx;
734 ieee80211_michael_mic_failure(hapd, addr, 0);
738 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
739 wpa_eapol_variable var, int value)
741 struct hostapd_data *hapd = ctx;
742 struct sta_info *sta = ap_get_sta(hapd, addr);
746 case WPA_EAPOL_portEnabled:
747 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
749 case WPA_EAPOL_portValid:
750 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
752 case WPA_EAPOL_authorized:
753 ieee802_1x_set_sta_authorized(hapd, sta, value);
755 case WPA_EAPOL_portControl_Auto:
757 sta->eapol_sm->portControl = Auto;
759 case WPA_EAPOL_keyRun:
761 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
763 case WPA_EAPOL_keyAvailable:
765 sta->eapol_sm->eap_if->eapKeyAvailable =
766 value ? TRUE : FALSE;
768 case WPA_EAPOL_keyDone:
770 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
772 case WPA_EAPOL_inc_EapolFramesTx:
774 sta->eapol_sm->dot1xAuthEapolFramesTx++;
780 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
781 wpa_eapol_variable var)
783 struct hostapd_data *hapd = ctx;
784 struct sta_info *sta = ap_get_sta(hapd, addr);
785 if (sta == NULL || sta->eapol_sm == NULL)
788 case WPA_EAPOL_keyRun:
789 return sta->eapol_sm->keyRun;
790 case WPA_EAPOL_keyAvailable:
791 return sta->eapol_sm->eap_if->eapKeyAvailable;
798 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
801 struct hostapd_data *hapd = ctx;
802 return hostapd_get_psk(hapd->conf, addr, prev_psk);
806 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
809 struct hostapd_data *hapd = ctx;
812 struct sta_info *sta;
814 sta = ap_get_sta(hapd, addr);
818 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
824 os_memcpy(msk, key, keylen);
831 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
832 const u8 *addr, int idx, u8 *key,
835 struct hostapd_data *hapd = ctx;
836 const char *ifname = hapd->conf->iface;
839 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
844 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
849 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
852 struct hostapd_data *hapd = ctx;
853 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
857 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
860 struct hostapd_data *hapd = ctx;
861 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
866 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
867 const u8 *data, size_t data_len,
870 struct hostapd_data *hapd = ctx;
871 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
875 static int hostapd_wpa_auth_for_each_sta(
876 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
879 struct hostapd_data *hapd = ctx;
880 struct sta_info *sta;
882 for (sta = hapd->sta_list; sta; sta = sta->next) {
883 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
890 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
891 const u8 *data, size_t data_len)
893 struct hostapd_data *hapd = ctx;
895 if (hapd->driver && hapd->driver->send_ether)
896 return hapd->driver->send_ether(hapd->drv_priv, dst,
897 hapd->own_addr, proto,
899 if (hapd->l2 == NULL)
901 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
905 #ifdef CONFIG_IEEE80211R
907 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
908 const u8 *data, size_t data_len)
910 struct hostapd_data *hapd = ctx;
912 struct ieee80211_mgmt *m;
914 struct sta_info *sta;
916 sta = ap_get_sta(hapd, dst);
917 if (sta == NULL || sta->wpa_sm == NULL)
920 m = os_zalloc(sizeof(*m) + data_len);
923 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
924 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
925 WLAN_FC_STYPE_ACTION);
926 os_memcpy(m->da, dst, ETH_ALEN);
927 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
928 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
929 os_memcpy(&m->u, data, data_len);
931 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
937 static struct wpa_state_machine *
938 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
940 struct hostapd_data *hapd = ctx;
941 struct sta_info *sta;
943 sta = ap_sta_add(hapd, sta_addr);
949 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
950 if (sta->wpa_sm == NULL) {
951 ap_free_sta(hapd, sta);
954 sta->auth_alg = WLAN_AUTH_FT;
960 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
963 struct hostapd_data *hapd = ctx;
964 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
967 #endif /* CONFIG_IEEE80211R */
971 * hostapd_validate_bssid_configuration - Validate BSSID configuration
972 * @iface: Pointer to interface data
973 * Returns: 0 on success, -1 on failure
975 * This function is used to validate that the configured BSSIDs are valid.
977 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
979 u8 mask[ETH_ALEN] = { 0 };
980 struct hostapd_data *hapd = iface->bss[0];
981 unsigned int i = iface->conf->num_bss, bits = 0, j;
984 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
986 /* Determine the bits necessary to cover the number of BSSIDs. */
987 for (i--; i; i >>= 1)
990 /* Determine the bits necessary to any configured BSSIDs,
991 if they are higher than the number of BSSIDs. */
992 for (j = 0; j < iface->conf->num_bss; j++) {
993 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
996 for (i = 0; i < ETH_ALEN; i++) {
998 iface->conf->bss[j].bssid[i] ^
1003 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1009 while (mask[i] != 0) {
1021 os_memset(mask, 0xff, ETH_ALEN);
1023 for (i = 5; i > 5 - j; i--)
1029 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1030 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1032 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1037 printf("Driver did not accept BSSID mask " MACSTR " for start "
1038 "address " MACSTR ".\n",
1039 MAC2STR(mask), MAC2STR(hapd->own_addr));
1043 for (i = 0; i < ETH_ALEN; i++) {
1044 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1045 printf("Invalid BSSID mask " MACSTR " for start "
1046 "address " MACSTR ".\n"
1047 "Start address must be the first address in the"
1048 " block (i.e., addr AND mask == addr).\n",
1049 MAC2STR(mask), MAC2STR(hapd->own_addr));
1058 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1062 for (i = 0; i < conf->num_bss; i++) {
1063 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1072 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1074 struct wpa_auth_config _conf;
1075 struct wpa_auth_callbacks cb;
1079 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1080 os_memset(&cb, 0, sizeof(cb));
1082 cb.logger = hostapd_wpa_auth_logger;
1083 cb.disconnect = hostapd_wpa_auth_disconnect;
1084 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1085 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1086 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1087 cb.get_psk = hostapd_wpa_auth_get_psk;
1088 cb.get_msk = hostapd_wpa_auth_get_msk;
1089 cb.set_key = hostapd_wpa_auth_set_key;
1090 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1091 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1092 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1093 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1094 cb.send_ether = hostapd_wpa_auth_send_ether;
1095 #ifdef CONFIG_IEEE80211R
1096 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1097 cb.add_sta = hostapd_wpa_auth_add_sta;
1098 #endif /* CONFIG_IEEE80211R */
1099 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1100 if (hapd->wpa_auth == NULL) {
1101 printf("WPA initialization failed.\n");
1105 if (hostapd_set_privacy(hapd, 1)) {
1106 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1107 "for interface %s", hapd->conf->iface);
1111 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1112 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1113 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1114 "the kernel driver.");
1118 if (rsn_preauth_iface_init(hapd)) {
1119 printf("Initialization of RSN pre-authentication "
1129 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1130 struct hostapd_bss_config *conf)
1132 struct radius_server_conf srv;
1133 os_memset(&srv, 0, sizeof(srv));
1134 srv.client_file = conf->radius_server_clients;
1135 srv.auth_port = conf->radius_server_auth_port;
1136 srv.conf_ctx = conf;
1137 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1138 srv.ssl_ctx = hapd->ssl_ctx;
1139 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1140 srv.eap_fast_a_id = conf->eap_fast_a_id;
1141 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1142 srv.ipv6 = conf->radius_server_ipv6;
1143 srv.get_eap_user = hostapd_radius_get_eap_user;
1145 hapd->radius_srv = radius_server_init(&srv);
1146 if (hapd->radius_srv == NULL) {
1147 printf("RADIUS server initialization failed.\n");
1156 * hostapd_setup_bss - Per-BSS setup (initialization)
1157 * @hapd: Pointer to BSS data
1158 * @first: Whether this BSS is the first BSS of an interface
1160 * This function is used to initialize all per-BSS data structures and
1161 * resources. This gets called in a loop for each BSS when an interface is
1162 * initialized. Most of the modules that are initialized here will be
1163 * deinitialized in hostapd_cleanup().
1165 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1167 struct hostapd_bss_config *conf = hapd->conf;
1168 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1169 int ssid_len, set_ssid;
1172 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1173 /* Allocate the next available BSSID. */
1175 inc_byte_array(hapd->own_addr, ETH_ALEN);
1176 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1178 /* Allocate the configured BSSID. */
1179 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1181 if (hostapd_mac_comp(hapd->own_addr,
1182 hapd->iface->bss[0]->own_addr) ==
1184 printf("BSS '%s' may not have BSSID "
1185 "set to the MAC address of the radio\n",
1191 hapd->interface_added = 1;
1192 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1194 printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1195 MAC2STR(hapd->own_addr));
1201 * Fetch the SSID from the system and use it or,
1202 * if one was specified in the config file, verify they
1205 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1207 printf("Could not read SSID from system\n");
1210 if (conf->ssid.ssid_set) {
1212 * If SSID is specified in the config file and it differs
1213 * from what is being used then force installation of the
1216 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1217 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1220 * No SSID in the config file; just use the one we got
1224 conf->ssid.ssid_len = ssid_len;
1225 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1226 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1229 printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
1230 hapd->conf->iface, MAC2STR(hapd->own_addr),
1231 hapd->conf->ssid.ssid);
1233 if (hostapd_setup_wpa_psk(conf)) {
1234 printf("WPA-PSK setup failed.\n");
1238 /* Set flag for whether SSID is broadcast in beacons */
1239 if (hostapd_set_broadcast_ssid(hapd,
1240 !!hapd->conf->ignore_broadcast_ssid)) {
1241 printf("Could not set broadcast SSID flag for kernel "
1246 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1247 printf("Could not set DTIM period for kernel driver\n");
1251 /* Set SSID for the kernel driver (to be used in beacon and probe
1252 * response frames) */
1253 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1254 conf->ssid.ssid_len)) {
1255 printf("Could not set SSID for kernel driver\n");
1259 if (wpa_debug_level == MSG_MSGDUMP)
1260 conf->radius->msg_dumps = 1;
1261 hapd->radius = radius_client_init(hapd, conf->radius);
1262 if (hapd->radius == NULL) {
1263 printf("RADIUS client initialization failed.\n");
1267 if (hostapd_acl_init(hapd)) {
1268 printf("ACL initialization failed.\n");
1272 if (ieee802_1x_init(hapd)) {
1273 printf("IEEE 802.1X initialization failed.\n");
1277 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1280 if (accounting_init(hapd)) {
1281 printf("Accounting initialization failed.\n");
1285 if (hapd->conf->ieee802_11f &&
1286 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1287 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1291 if (hostapd_ctrl_iface_init(hapd)) {
1292 printf("Failed to setup control interface\n");
1296 if (vlan_init(hapd)) {
1297 printf("VLAN initialization failed.\n");
1301 #ifdef CONFIG_IEEE80211R
1302 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1303 hostapd_rrb_receive, hapd, 0);
1304 if (hapd->l2 == NULL &&
1305 (hapd->driver == NULL || hapd->driver->send_ether == NULL)) {
1306 printf("Failed to open l2_packet interface\n");
1309 #endif /* CONFIG_IEEE80211R */
1311 ieee802_11_set_beacon(hapd);
1313 if (conf->radius_server_clients &&
1314 hostapd_setup_radius_srv(hapd, conf))
1322 * setup_interface2 - Setup (initialize) an interface (part 2)
1323 * @iface: Pointer to interface data.
1324 * Returns: 0 on success; -1 on failure.
1326 * Flushes old stations, sets the channel, DFS parameters, encryption,
1327 * beacons, and WDS links based on the configuration.
1329 static int setup_interface2(struct hostapd_iface *iface)
1331 struct hostapd_data *hapd = iface->bss[0];
1337 hostapd_flush_old_stations(hapd);
1338 hostapd_set_privacy(hapd, 0);
1340 if (hapd->iconf->channel) {
1341 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1342 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1343 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1344 hapd->iconf->channel, freq);
1346 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1347 printf("Could not set channel for kernel driver\n");
1352 hostapd_broadcast_wep_clear(hapd);
1353 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1356 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1357 ieee802_11_set_beacon(hapd);
1359 if (hapd->iconf->rts_threshold > -1 &&
1360 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1361 printf("Could not set RTS threshold for kernel driver\n");
1365 if (hapd->iconf->fragm_threshold > -1 &&
1366 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1367 printf("Could not set fragmentation threshold for kernel "
1372 prev_addr = hapd->own_addr;
1374 for (j = 0; j < iface->num_bss; j++) {
1375 hapd = iface->bss[j];
1377 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1378 if (hostapd_setup_bss(hapd, j == 0))
1380 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1381 prev_addr = hapd->own_addr;
1384 ap_list_init(iface);
1386 if (hostapd_driver_commit(hapd) < 0) {
1387 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1388 "configuration", __func__);
1396 static void setup_interface_start(void *eloop_data, void *user_ctx);
1397 static void setup_interface2_handler(void *eloop_data, void *user_ctx);
1400 * setup_interface_finalize - Finish setup interface & call the callback
1401 * @iface: Pointer to interface data.
1402 * @status: Status of the setup interface (0 on success; -1 on failure).
1403 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1405 static int setup_interface_finalize(struct hostapd_iface *iface, int status)
1407 hostapd_iface_cb cb;
1409 if (!iface->setup_cb)
1412 eloop_cancel_timeout(setup_interface_start, iface, NULL);
1413 eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
1414 hostapd_select_hw_mode_stop(iface);
1416 cb = iface->setup_cb;
1418 iface->setup_cb = NULL;
1427 * setup_interface2_wrapper - Wrapper for setup_interface2()
1428 * @iface: Pointer to interface data.
1429 * @status: Status of the hw mode select.
1431 * Wrapper for setup_interface2() to calls finalize function upon completion.
1433 static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
1437 printf("Could not select hw_mode and channel. (%d)\n", ret);
1439 ret = setup_interface2(iface);
1441 setup_interface_finalize(iface, ret);
1446 * setup_interface2_handler - Used for immediate call of setup_interface2
1447 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1448 * @user_ctx: Unused.
1450 static void setup_interface2_handler(void *eloop_data, void *user_ctx)
1452 struct hostapd_iface *iface = eloop_data;
1454 setup_interface2_wrapper(iface, 0);
1458 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1459 size_t identity_len, int phase2,
1460 struct eap_user *user)
1462 const struct hostapd_eap_user *eap_user;
1465 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1466 if (eap_user == NULL)
1472 os_memset(user, 0, sizeof(*user));
1473 count = EAP_USER_MAX_METHODS;
1474 if (count > EAP_MAX_METHODS)
1475 count = EAP_MAX_METHODS;
1476 for (i = 0; i < count; i++) {
1477 user->methods[i].vendor = eap_user->methods[i].vendor;
1478 user->methods[i].method = eap_user->methods[i].method;
1481 if (eap_user->password) {
1482 user->password = os_malloc(eap_user->password_len);
1483 if (user->password == NULL)
1485 os_memcpy(user->password, eap_user->password,
1486 eap_user->password_len);
1487 user->password_len = eap_user->password_len;
1488 user->password_hash = eap_user->password_hash;
1490 user->force_version = eap_user->force_version;
1491 user->ttls_auth = eap_user->ttls_auth;
1498 * setup_interface1 - Setup (initialize) an interface (part 1)
1499 * @iface: Pointer to interface data
1500 * Returns: 0 on success, -1 on failure
1502 * Initializes the driver interface, validates the configuration,
1503 * and sets driver parameters based on the configuration.
1504 * Schedules setup_interface2() to be called immediately or after
1505 * hardware mode setup takes place.
1507 static int setup_interface1(struct hostapd_iface *iface)
1509 struct hostapd_data *hapd = iface->bss[0];
1510 struct hostapd_bss_config *conf = hapd->conf;
1513 u8 *b = conf->bssid;
1516 * Initialize the driver interface and make sure that all BSSes get
1517 * configured with a pointer to this driver interface.
1519 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1520 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1522 hapd->drv_priv = hostapd_driver_init(hapd);
1525 if (hapd->drv_priv == NULL) {
1526 printf("%s driver initialization failed.\n",
1527 hapd->driver ? hapd->driver->name : "Unknown");
1528 hapd->driver = NULL;
1531 for (i = 0; i < iface->num_bss; i++) {
1532 iface->bss[i]->driver = hapd->driver;
1533 iface->bss[i]->drv_priv = hapd->drv_priv;
1536 if (hostapd_validate_bssid_configuration(iface))
1539 os_memcpy(country, hapd->iconf->country, 3);
1541 if (hostapd_set_country(hapd, country) < 0) {
1542 printf("Failed to set country code\n");
1546 if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
1547 if (hostapd_set_ieee80211d(hapd, 1) < 0) {
1548 printf("Failed to set ieee80211d (%d)\n",
1549 hapd->iconf->ieee80211d);
1554 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1555 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1556 printf("Failed to set bridge_packets for kernel driver\n");
1560 /* TODO: merge with hostapd_driver_init() ? */
1561 if (hostapd_wireless_event_init(hapd) < 0)
1564 if (hostapd_get_hw_features(iface)) {
1565 /* Not all drivers support this yet, so continue without hw
1568 return hostapd_select_hw_mode_start(iface,
1569 setup_interface2_wrapper);
1572 eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
1578 * setup_interface_start - Handler to start setup interface
1579 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1580 * @user_ctx: Unused.
1582 * An eloop handler is used so that all errors can be processed by the
1583 * callback without introducing stack recursion.
1585 static void setup_interface_start(void *eloop_data, void *user_ctx)
1587 struct hostapd_iface *iface = eloop_data;
1591 ret = setup_interface1(iface);
1593 setup_interface_finalize(iface, ret);
1598 * hostapd_setup_interface_start - Start the setup of an interface
1599 * @iface: Pointer to interface data.
1600 * @cb: The function to callback when done.
1601 * Returns: 0 if it starts successfully; cb will be called when done.
1602 * -1 on failure; cb will not be called.
1604 * Initializes the driver interface, validates the configuration,
1605 * and sets driver parameters based on the configuration.
1606 * Flushes old stations, sets the channel, DFS parameters, encryption,
1607 * beacons, and WDS links based on the configuration.
1609 int hostapd_setup_interface_start(struct hostapd_iface *iface,
1610 hostapd_iface_cb cb)
1612 if (iface->setup_cb) {
1613 wpa_printf(MSG_DEBUG,
1614 "%s: Interface setup already in progress.\n",
1615 iface->bss[0]->conf->iface);
1619 iface->setup_cb = cb;
1621 eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
1628 * hostapd_setup_interace_stop - Stops the setup of an interface
1629 * @iface: Pointer to interface data
1630 * Returns: 0 if successfully stopped;
1631 * -1 on failure (i.e., was not in progress)
1633 int hostapd_setup_interface_stop(struct hostapd_iface *iface)
1635 return setup_interface_finalize(iface, -1);
1639 static void show_version(void)
1642 "hostapd v" VERSION_STR "\n"
1643 "User space daemon for IEEE 802.11 AP management,\n"
1644 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1645 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1646 "and contributors\n");
1650 static void usage(void)
1655 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1656 "<configuration file(s)>\n"
1659 " -h show this usage\n"
1660 " -d show more debug messages (-dd for even more)\n"
1661 " -B run daemon in the background\n"
1663 " -K include key data in debug messages\n"
1664 " -t include timestamps in some debug messages\n"
1665 " -v show hostapd version\n");
1672 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1673 * @hapd_iface: Pointer to interface data
1674 * @conf: Pointer to per-interface configuration
1675 * @bss: Pointer to per-BSS configuration for this BSS
1676 * Returns: Pointer to allocated BSS data
1678 * This function is used to allocate per-BSS data structure. This data will be
1679 * freed after hostapd_cleanup() is called for it during interface
1682 static struct hostapd_data *
1683 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1684 struct hostapd_config *conf,
1685 struct hostapd_bss_config *bss)
1687 struct hostapd_data *hapd;
1689 hapd = os_zalloc(sizeof(*hapd));
1695 hapd->iface = hapd_iface;
1697 if (hapd->conf->individual_wep_key_len > 0) {
1698 /* use key0 in individual key and key1 in broadcast key */
1699 hapd->default_wep_key_idx = 1;
1702 #ifdef EAP_TLS_FUNCS
1703 if (hapd->conf->eap_server &&
1704 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1705 hapd->conf->dh_file)) {
1706 struct tls_connection_params params;
1708 hapd->ssl_ctx = tls_init(NULL);
1709 if (hapd->ssl_ctx == NULL) {
1710 printf("Failed to initialize TLS\n");
1714 os_memset(¶ms, 0, sizeof(params));
1715 params.ca_cert = hapd->conf->ca_cert;
1716 params.client_cert = hapd->conf->server_cert;
1717 params.private_key = hapd->conf->private_key;
1718 params.private_key_passwd = hapd->conf->private_key_passwd;
1719 params.dh_file = hapd->conf->dh_file;
1721 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1722 printf("Failed to set TLS parameters\n");
1726 if (tls_global_set_verify(hapd->ssl_ctx,
1727 hapd->conf->check_crl)) {
1728 printf("Failed to enable check_crl\n");
1732 #endif /* EAP_TLS_FUNCS */
1735 if (hapd->conf->eap_sim_db) {
1736 hapd->eap_sim_db_priv =
1737 eap_sim_db_init(hapd->conf->eap_sim_db,
1738 hostapd_sim_db_cb, hapd);
1739 if (hapd->eap_sim_db_priv == NULL) {
1740 printf("Failed to initialize EAP-SIM database "
1745 #endif /* EAP_SERVER */
1747 if (hapd->conf->assoc_ap)
1748 hapd->assoc_ap_state = WAIT_BEACON;
1750 hapd->driver = hapd->iconf->driver;
1754 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1757 /* TODO: cleanup allocated resources(?) */
1764 * hostapd_init - Allocate and initialize per-interface data
1765 * @config_file: Path to the configuration file
1766 * Returns: Pointer to the allocated interface data or %NULL on failure
1768 * This function is used to allocate main data structures for per-interface
1769 * data. The allocated data buffer will be freed by calling
1770 * hostapd_cleanup_iface().
1772 static struct hostapd_iface * hostapd_init(const char *config_file)
1774 struct hostapd_iface *hapd_iface = NULL;
1775 struct hostapd_config *conf = NULL;
1776 struct hostapd_data *hapd;
1779 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1780 if (hapd_iface == NULL)
1783 hapd_iface->config_fname = os_strdup(config_file);
1784 if (hapd_iface->config_fname == NULL)
1787 conf = hostapd_config_read(hapd_iface->config_fname);
1790 hapd_iface->conf = conf;
1792 hapd_iface->num_bss = conf->num_bss;
1793 hapd_iface->bss = os_zalloc(conf->num_bss *
1794 sizeof(struct hostapd_data *));
1795 if (hapd_iface->bss == NULL)
1798 for (i = 0; i < conf->num_bss; i++) {
1799 hapd = hapd_iface->bss[i] =
1800 hostapd_alloc_bss_data(hapd_iface, conf,
1810 hostapd_config_free(conf);
1812 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1813 hapd = hapd_iface->bss[i];
1814 if (hapd && hapd->ssl_ctx)
1815 tls_deinit(hapd->ssl_ctx);
1818 os_free(hapd_iface->config_fname);
1819 os_free(hapd_iface->bss);
1820 os_free(hapd_iface);
1827 * register_drivers - Register driver interfaces
1829 * This function is generated by Makefile (into driver_conf.c) to call all
1830 * configured driver interfaces to register them to core hostapd.
1832 void register_drivers(void);
1836 * setup_interface_done - Callback when an interface is done being setup.
1837 * @iface: Pointer to interface data.
1838 * @status: Status of the interface setup (0 on success; -1 on failure).
1840 static void setup_interface_done(struct hostapd_iface *iface, int status)
1843 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1844 iface->bss[0]->conf->iface);
1847 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1848 iface->bss[0]->conf->iface);
1852 int main(int argc, char *argv[])
1854 struct hapd_interfaces interfaces;
1857 int c, debug = 0, daemonize = 0;
1858 const char *pid_file = NULL;
1860 hostapd_logger_register_cb(hostapd_logger_cb);
1863 c = getopt(argc, argv, "BdhKP:tv");
1872 if (wpa_debug_level > 0)
1879 wpa_debug_show_keys++;
1885 wpa_debug_timestamp++;
1901 if (eap_server_register_methods()) {
1902 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1906 interfaces.count = argc - optind;
1908 interfaces.iface = os_malloc(interfaces.count *
1909 sizeof(struct hostapd_iface *));
1910 if (interfaces.iface == NULL) {
1911 wpa_printf(MSG_ERROR, "malloc failed\n");
1915 if (eloop_init(&interfaces)) {
1916 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1920 #ifndef CONFIG_NATIVE_WINDOWS
1921 eloop_register_signal(SIGHUP, handle_reload, NULL);
1922 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1923 #endif /* CONFIG_NATIVE_WINDOWS */
1924 eloop_register_signal_terminate(handle_term, NULL);
1926 /* Initialize interfaces */
1927 for (i = 0; i < interfaces.count; i++) {
1928 printf("Configuration file: %s\n", argv[optind + i]);
1929 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1930 if (!interfaces.iface[i])
1932 for (k = 0; k < debug; k++) {
1933 if (interfaces.iface[i]->bss[0]->conf->
1934 logger_stdout_level > 0)
1935 interfaces.iface[i]->bss[0]->conf->
1936 logger_stdout_level--;
1939 ret = hostapd_setup_interface_start(interfaces.iface[i],
1940 setup_interface_done);
1945 if (daemonize && os_daemonize(pid_file)) {
1950 #ifndef CONFIG_NATIVE_WINDOWS
1951 openlog("hostapd", 0, LOG_DAEMON);
1952 #endif /* CONFIG_NATIVE_WINDOWS */
1956 /* Disconnect associated stations from all interfaces and BSSes */
1957 for (i = 0; i < interfaces.count; i++) {
1958 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1959 struct hostapd_data *hapd =
1960 interfaces.iface[i]->bss[j];
1961 hostapd_free_stas(hapd);
1962 hostapd_flush_old_stations(hapd);
1969 /* Deinitialize all interfaces */
1970 for (i = 0; i < interfaces.count; i++) {
1971 if (!interfaces.iface[i])
1973 hostapd_setup_interface_stop(interfaces.iface[i]);
1974 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1975 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1976 struct hostapd_data *hapd =
1977 interfaces.iface[i]->bss[j];
1978 hostapd_cleanup(hapd);
1979 if (j == interfaces.iface[i]->num_bss - 1 &&
1981 hostapd_driver_deinit(hapd);
1983 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1984 os_free(interfaces.iface[i]->bss[j]);
1985 hostapd_cleanup_iface(interfaces.iface[i]);
1987 os_free(interfaces.iface);
1991 #ifndef CONFIG_NATIVE_WINDOWS
1993 #endif /* CONFIG_NATIVE_WINDOWS */
1995 eap_server_unregister_methods();
1997 os_daemonize_terminate(pid_file);