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"
44 #include "eap_server/tncs.h"
46 #include "l2_packet/l2_packet.h"
49 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
50 size_t identity_len, int phase2,
51 struct eap_user *user);
53 struct hapd_interfaces {
55 struct hostapd_iface **iface;
58 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
61 extern int wpa_debug_level;
62 extern int wpa_debug_show_keys;
63 extern int wpa_debug_timestamp;
66 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
67 int level, const char *txt, size_t len)
69 struct hostapd_data *hapd = ctx;
70 char *format, *module_str;
72 int conf_syslog_level, conf_stdout_level;
73 unsigned int conf_syslog, conf_stdout;
76 format = os_malloc(maxlen);
80 if (hapd && hapd->conf) {
81 conf_syslog_level = hapd->conf->logger_syslog_level;
82 conf_stdout_level = hapd->conf->logger_stdout_level;
83 conf_syslog = hapd->conf->logger_syslog;
84 conf_stdout = hapd->conf->logger_stdout;
86 conf_syslog_level = conf_stdout_level = 0;
87 conf_syslog = conf_stdout = (unsigned int) -1;
91 case HOSTAPD_MODULE_IEEE80211:
92 module_str = "IEEE 802.11";
94 case HOSTAPD_MODULE_IEEE8021X:
95 module_str = "IEEE 802.1X";
97 case HOSTAPD_MODULE_RADIUS:
98 module_str = "RADIUS";
100 case HOSTAPD_MODULE_WPA:
103 case HOSTAPD_MODULE_DRIVER:
104 module_str = "DRIVER";
106 case HOSTAPD_MODULE_IAPP:
109 case HOSTAPD_MODULE_MLME:
117 if (hapd && hapd->conf && addr)
118 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
119 hapd->conf->iface, MAC2STR(addr),
120 module_str ? " " : "", module_str, txt);
121 else if (hapd && hapd->conf)
122 os_snprintf(format, maxlen, "%s:%s%s %s",
123 hapd->conf->iface, module_str ? " " : "",
126 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
127 MAC2STR(addr), module_str ? " " : "",
130 os_snprintf(format, maxlen, "%s%s%s",
131 module_str, module_str ? ": " : "", txt);
133 if ((conf_stdout & module) && level >= conf_stdout_level) {
134 wpa_debug_print_timestamp();
135 printf("%s\n", format);
138 #ifndef CONFIG_NATIVE_WINDOWS
139 if ((conf_syslog & module) && level >= conf_syslog_level) {
142 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
143 case HOSTAPD_LEVEL_DEBUG:
144 priority = LOG_DEBUG;
146 case HOSTAPD_LEVEL_INFO:
149 case HOSTAPD_LEVEL_NOTICE:
150 priority = LOG_NOTICE;
152 case HOSTAPD_LEVEL_WARNING:
153 priority = LOG_WARNING;
159 syslog(priority, "%s", format);
161 #endif /* CONFIG_NATIVE_WINDOWS */
167 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
171 /* New Prism2.5/3 STA firmware versions seem to have issues with this
172 * broadcast deauth frame. This gets the firmware in odd state where
173 * nothing works correctly, so let's skip sending this for the hostap
176 if (os_strcmp(hapd->driver->name, "hostap") != 0) {
177 os_memset(addr, 0xff, ETH_ALEN);
178 hostapd_sta_deauth(hapd, addr,
179 WLAN_REASON_PREV_AUTH_NOT_VALID);
185 * hostapd_prune_associations - Remove extraneous associations
186 * @hapd: Pointer to BSS data for the most recent association
187 * @sta: Pointer to the associated STA data
189 * This function looks through all radios and BSS's for previous
190 * (stale) associations of STA. If any are found they are removed.
192 static void hostapd_prune_associations(struct hostapd_data *hapd,
193 struct sta_info *sta)
195 struct sta_info *osta;
196 struct hostapd_data *ohapd;
198 struct hapd_interfaces *interfaces = eloop_get_user_data();
200 for (i = 0; i < interfaces->count; i++) {
201 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
202 ohapd = interfaces->iface[i]->bss[j];
205 osta = ap_get_sta(ohapd, sta->addr);
209 ap_sta_disassociate(ohapd, osta,
210 WLAN_REASON_UNSPECIFIED);
217 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
218 * @hapd: Pointer to BSS data
219 * @sta: Pointer to the associated STA data
220 * @reassoc: 1 to indicate this was a re-association; 0 = first association
222 * This function will be called whenever a station associates with the AP. It
223 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
224 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
225 * authentication and association) internally.
227 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
230 if (hapd->tkip_countermeasures) {
231 hostapd_sta_deauth(hapd, sta->addr,
232 WLAN_REASON_MICHAEL_MIC_FAILURE);
236 hostapd_prune_associations(hapd, sta);
238 /* IEEE 802.11F (IAPP) */
239 if (hapd->conf->ieee802_11f)
240 iapp_new_station(hapd->iapp, sta);
242 /* Start accounting here, if IEEE 802.1X and WPA are not used.
243 * IEEE 802.1X/WPA code will start accounting after the station has
244 * been authorized. */
245 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
246 accounting_sta_start(hapd, sta);
248 hostapd_wme_sta_config(hapd, sta);
250 /* Start IEEE 802.1X authentication process for new stations */
251 ieee802_1x_new_station(hapd, sta);
253 if (sta->auth_alg != WLAN_AUTH_FT)
254 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
256 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
261 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
262 struct sta_info *sta, void *ctx)
264 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
270 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
272 struct hostapd_data *hapd = ctx;
273 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
274 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
276 #endif /* EAP_SERVER */
279 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
281 printf("Signal %d received - terminating\n", sig);
286 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
287 struct wpa_auth_config *wconf)
289 wconf->wpa = conf->wpa;
290 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
291 wconf->wpa_pairwise = conf->wpa_pairwise;
292 wconf->wpa_group = conf->wpa_group;
293 wconf->wpa_group_rekey = conf->wpa_group_rekey;
294 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
295 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
296 wconf->rsn_pairwise = conf->rsn_pairwise;
297 wconf->rsn_preauth = conf->rsn_preauth;
298 wconf->eapol_version = conf->eapol_version;
299 wconf->peerkey = conf->peerkey;
300 wconf->wme_enabled = conf->wme_enabled;
301 #ifdef CONFIG_IEEE80211W
302 wconf->ieee80211w = conf->ieee80211w;
303 #endif /* CONFIG_IEEE80211W */
304 #ifdef CONFIG_IEEE80211R
305 wconf->ssid_len = conf->ssid.ssid_len;
306 if (wconf->ssid_len > SSID_LEN)
307 wconf->ssid_len = SSID_LEN;
308 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
309 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
310 MOBILITY_DOMAIN_ID_LEN);
311 if (conf->nas_identifier &&
312 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
313 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
314 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
315 wconf->r0_key_holder_len);
317 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
318 wconf->r0_key_lifetime = conf->r0_key_lifetime;
319 wconf->reassociation_deadline = conf->reassociation_deadline;
320 wconf->r0kh_list = conf->r0kh_list;
321 wconf->r1kh_list = conf->r1kh_list;
322 wconf->pmk_r1_push = conf->pmk_r1_push;
323 #endif /* CONFIG_IEEE80211R */
327 #ifndef CONFIG_NATIVE_WINDOWS
328 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
330 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
331 struct hostapd_config *newconf;
333 struct wpa_auth_config wpa_auth_conf;
335 printf("Signal %d received - reloading configuration\n", sig);
337 for (i = 0; i < hapds->count; i++) {
338 struct hostapd_data *hapd = hapds->iface[i]->bss[0];
339 newconf = hostapd_config_read(hapds->iface[i]->config_fname);
340 if (newconf == NULL) {
341 printf("Failed to read new configuration file - "
342 "continuing with old.\n");
345 /* TODO: update dynamic data based on changed configuration
346 * items (e.g., open/close sockets, remove stations added to
347 * deny list, etc.) */
348 radius_client_flush(hapd->radius, 0);
349 hostapd_config_free(hapd->iconf);
351 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
352 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
354 hapd->iconf = newconf;
355 hapd->conf = &newconf->bss[0];
356 hapds->iface[i]->conf = newconf;
358 if (hostapd_setup_wpa_psk(hapd->conf)) {
359 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
360 "after reloading configuration");
366 #ifdef HOSTAPD_DUMP_STATE
367 static void hostapd_dump_state(struct hostapd_data *hapd)
371 struct sta_info *sta;
375 if (!hapd->conf->dump_log_name) {
376 printf("Dump file not defined - ignoring dump request\n");
380 printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
381 f = fopen(hapd->conf->dump_log_name, "w");
383 printf("Could not open dump file '%s' for writing.\n",
384 hapd->conf->dump_log_name);
389 fprintf(f, "hostapd state dump - %s", ctime(&now));
390 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
391 "num_sta_no_short_slot_time=%d\n"
392 "num_sta_no_short_preamble=%d\n",
393 hapd->num_sta, hapd->iface->num_sta_non_erp,
394 hapd->iface->num_sta_no_short_slot_time,
395 hapd->iface->num_sta_no_short_preamble);
397 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
398 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
401 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s\n"
402 " capability=0x%x listen_interval=%d\n",
405 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
406 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
407 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
408 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
409 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
410 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
412 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
414 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
415 "[SHORT_PREAMBLE]" : ""),
416 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
417 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
419 sta->listen_interval);
421 fprintf(f, " supported_rates=");
422 for (i = 0; i < sta->supported_rates_len; i++)
423 fprintf(f, "%02x ", sta->supported_rates[i]);
427 " timeout_next=%s\n",
428 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
429 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
432 ieee802_1x_dump_state(f, " ", sta);
435 buf = os_malloc(4096);
437 int count = radius_client_get_mib(hapd->radius, buf, 4096);
440 else if (count > 4095)
443 fprintf(f, "%s", buf);
445 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
448 else if (count > 4095)
451 fprintf(f, "%s", buf);
456 #endif /* HOSTAPD_DUMP_STATE */
459 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
461 #ifdef HOSTAPD_DUMP_STATE
462 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
465 for (i = 0; i < hapds->count; i++) {
466 struct hostapd_iface *hapd_iface = hapds->iface[i];
467 for (j = 0; j < hapd_iface->num_bss; j++)
468 hostapd_dump_state(hapd_iface->bss[j]);
470 #endif /* HOSTAPD_DUMP_STATE */
472 #endif /* CONFIG_NATIVE_WINDOWS */
474 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
479 for (i = 0; i < NUM_WEP_KEYS; i++) {
480 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
481 0, i == 0 ? 1 : 0)) {
482 printf("Failed to clear default encryption keys "
483 "(ifname=%s keyidx=%d)\n", ifname, i);
489 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
491 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
496 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
499 struct hostapd_ssid *ssid = &hapd->conf->ssid;
502 if (ssid->wep.default_len &&
503 hostapd_set_encryption(hapd->conf->iface,
504 hapd, "WEP", NULL, idx,
507 idx == ssid->wep.idx)) {
508 printf("Could not set WEP encryption.\n");
512 if (ssid->dyn_vlan_keys) {
514 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
516 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
519 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
525 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
529 printf("Could not set dynamic VLAN WEP "
540 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
541 * @hapd: Pointer to BSS data
543 * This function is used to free all per-BSS data structures and resources.
544 * This gets called in a loop for each BSS between calls to
545 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
546 * is deinitialized. Most of the modules that are initialized in
547 * hostapd_setup_bss() are deinitialized here.
549 static void hostapd_cleanup(struct hostapd_data *hapd)
551 hostapd_ctrl_iface_deinit(hapd);
553 os_free(hapd->default_wep_key);
554 hapd->default_wep_key = NULL;
555 iapp_deinit(hapd->iapp);
557 accounting_deinit(hapd);
558 rsn_preauth_iface_deinit(hapd);
559 if (hapd->wpa_auth) {
560 wpa_deinit(hapd->wpa_auth);
561 hapd->wpa_auth = NULL;
563 if (hostapd_set_privacy(hapd, 0)) {
564 wpa_printf(MSG_DEBUG, "Could not disable "
565 "PrivacyInvoked for interface %s",
569 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
570 wpa_printf(MSG_DEBUG, "Could not remove generic "
571 "information element from interface %s",
575 ieee802_1x_deinit(hapd);
577 hostapd_acl_deinit(hapd);
578 radius_client_deinit(hapd->radius);
580 radius_server_deinit(hapd->radius_srv);
581 hapd->radius_srv = NULL;
583 #ifdef CONFIG_IEEE80211R
584 l2_packet_deinit(hapd->l2);
585 #endif /* CONFIG_IEEE80211R */
587 hostapd_wireless_event_deinit(hapd);
591 tls_deinit(hapd->ssl_ctx);
592 hapd->ssl_ctx = NULL;
594 #endif /* EAP_TLS_FUNCS */
597 if (hapd->eap_sim_db_priv) {
598 eap_sim_db_deinit(hapd->eap_sim_db_priv);
599 hapd->eap_sim_db_priv = NULL;
601 #endif /* EAP_SERVER */
603 if (hapd->interface_added &&
604 hostapd_bss_remove(hapd, hapd->conf->iface)) {
605 printf("Failed to remove BSS interface %s\n",
612 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
613 * @iface: Pointer to interface data
615 * This function is called before per-BSS data structures are deinitialized
616 * with hostapd_cleanup().
618 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
624 * hostapd_cleanup_iface - Complete per-interface cleanup
625 * @iface: Pointer to interface data
627 * This function is called after per-BSS data structures are deinitialized
628 * with hostapd_cleanup().
630 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
632 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
633 iface->hw_features = NULL;
634 os_free(iface->current_rates);
635 iface->current_rates = NULL;
636 ap_list_deinit(iface);
637 hostapd_config_free(iface->conf);
640 os_free(iface->config_fname);
646 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
650 hostapd_broadcast_wep_set(hapd);
652 if (hapd->conf->ssid.wep.default_len)
655 for (i = 0; i < 4; i++) {
656 if (hapd->conf->ssid.wep.key[i] &&
657 hostapd_set_encryption(iface, hapd, "WEP", NULL,
658 i, hapd->conf->ssid.wep.key[i],
659 hapd->conf->ssid.wep.len[i],
660 i == hapd->conf->ssid.wep.idx)) {
661 printf("Could not set WEP encryption.\n");
664 if (hapd->conf->ssid.wep.key[i] &&
665 i == hapd->conf->ssid.wep.idx)
666 hostapd_set_privacy(hapd, 1);
673 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
677 wpa_printf(MSG_DEBUG, "Flushing old station entries");
678 if (hostapd_flush(hapd)) {
679 printf("Could not connect to kernel driver.\n");
682 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
683 hostapd_deauth_all_stas(hapd);
689 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
690 logger_level level, const char *txt)
692 struct hostapd_data *hapd = ctx;
697 hlevel = HOSTAPD_LEVEL_WARNING;
700 hlevel = HOSTAPD_LEVEL_INFO;
704 hlevel = HOSTAPD_LEVEL_DEBUG;
708 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
712 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
715 struct hostapd_data *hapd = ctx;
716 struct sta_info *sta;
718 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
719 "STA " MACSTR " reason %d",
720 __func__, MAC2STR(addr), reason);
722 sta = ap_get_sta(hapd, addr);
723 hostapd_sta_deauth(hapd, addr, reason);
726 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
727 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
728 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
729 sta->timeout_next = STA_REMOVE;
733 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
735 struct hostapd_data *hapd = ctx;
736 ieee80211_michael_mic_failure(hapd, addr, 0);
740 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
741 wpa_eapol_variable var, int value)
743 struct hostapd_data *hapd = ctx;
744 struct sta_info *sta = ap_get_sta(hapd, addr);
748 case WPA_EAPOL_portEnabled:
749 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
751 case WPA_EAPOL_portValid:
752 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
754 case WPA_EAPOL_authorized:
755 ieee802_1x_set_sta_authorized(hapd, sta, value);
757 case WPA_EAPOL_portControl_Auto:
759 sta->eapol_sm->portControl = Auto;
761 case WPA_EAPOL_keyRun:
763 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
765 case WPA_EAPOL_keyAvailable:
767 sta->eapol_sm->eap_if->eapKeyAvailable =
768 value ? TRUE : FALSE;
770 case WPA_EAPOL_keyDone:
772 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
774 case WPA_EAPOL_inc_EapolFramesTx:
776 sta->eapol_sm->dot1xAuthEapolFramesTx++;
782 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
783 wpa_eapol_variable var)
785 struct hostapd_data *hapd = ctx;
786 struct sta_info *sta = ap_get_sta(hapd, addr);
787 if (sta == NULL || sta->eapol_sm == NULL)
790 case WPA_EAPOL_keyRun:
791 return sta->eapol_sm->keyRun;
792 case WPA_EAPOL_keyAvailable:
793 return sta->eapol_sm->eap_if->eapKeyAvailable;
800 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
803 struct hostapd_data *hapd = ctx;
804 return hostapd_get_psk(hapd->conf, addr, prev_psk);
808 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
811 struct hostapd_data *hapd = ctx;
814 struct sta_info *sta;
816 sta = ap_get_sta(hapd, addr);
820 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
826 os_memcpy(msk, key, keylen);
833 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
834 const u8 *addr, int idx, u8 *key,
837 struct hostapd_data *hapd = ctx;
838 const char *ifname = hapd->conf->iface;
841 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
846 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
851 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
854 struct hostapd_data *hapd = ctx;
855 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
859 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
862 struct hostapd_data *hapd = ctx;
863 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
868 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
869 const u8 *data, size_t data_len,
872 struct hostapd_data *hapd = ctx;
873 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
877 static int hostapd_wpa_auth_for_each_sta(
878 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
881 struct hostapd_data *hapd = ctx;
882 struct sta_info *sta;
884 for (sta = hapd->sta_list; sta; sta = sta->next) {
885 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
892 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
893 const u8 *data, size_t data_len)
895 struct hostapd_data *hapd = ctx;
897 if (hapd->driver && hapd->driver->send_ether)
898 return hapd->driver->send_ether(hapd->drv_priv, dst,
899 hapd->own_addr, proto,
901 if (hapd->l2 == NULL)
903 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
907 #ifdef CONFIG_IEEE80211R
909 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
910 const u8 *data, size_t data_len)
912 struct hostapd_data *hapd = ctx;
914 struct ieee80211_mgmt *m;
916 struct sta_info *sta;
918 sta = ap_get_sta(hapd, dst);
919 if (sta == NULL || sta->wpa_sm == NULL)
922 m = os_zalloc(sizeof(*m) + data_len);
925 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
926 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
927 WLAN_FC_STYPE_ACTION);
928 os_memcpy(m->da, dst, ETH_ALEN);
929 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
930 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
931 os_memcpy(&m->u, data, data_len);
933 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
939 static struct wpa_state_machine *
940 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
942 struct hostapd_data *hapd = ctx;
943 struct sta_info *sta;
945 sta = ap_sta_add(hapd, sta_addr);
951 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
952 if (sta->wpa_sm == NULL) {
953 ap_free_sta(hapd, sta);
956 sta->auth_alg = WLAN_AUTH_FT;
962 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
965 struct hostapd_data *hapd = ctx;
966 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
969 #endif /* CONFIG_IEEE80211R */
973 * hostapd_validate_bssid_configuration - Validate BSSID configuration
974 * @iface: Pointer to interface data
975 * Returns: 0 on success, -1 on failure
977 * This function is used to validate that the configured BSSIDs are valid.
979 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
981 u8 mask[ETH_ALEN] = { 0 };
982 struct hostapd_data *hapd = iface->bss[0];
983 unsigned int i = iface->conf->num_bss, bits = 0, j;
986 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
988 /* Determine the bits necessary to cover the number of BSSIDs. */
989 for (i--; i; i >>= 1)
992 /* Determine the bits necessary to any configured BSSIDs,
993 if they are higher than the number of BSSIDs. */
994 for (j = 0; j < iface->conf->num_bss; j++) {
995 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
998 for (i = 0; i < ETH_ALEN; i++) {
1000 iface->conf->bss[j].bssid[i] ^
1005 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1011 while (mask[i] != 0) {
1023 os_memset(mask, 0xff, ETH_ALEN);
1025 for (i = 5; i > 5 - j; i--)
1031 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1032 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1034 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1039 printf("Driver did not accept BSSID mask " MACSTR " for start "
1040 "address " MACSTR ".\n",
1041 MAC2STR(mask), MAC2STR(hapd->own_addr));
1045 for (i = 0; i < ETH_ALEN; i++) {
1046 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1047 printf("Invalid BSSID mask " MACSTR " for start "
1048 "address " MACSTR ".\n"
1049 "Start address must be the first address in the"
1050 " block (i.e., addr AND mask == addr).\n",
1051 MAC2STR(mask), MAC2STR(hapd->own_addr));
1060 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1064 for (i = 0; i < conf->num_bss; i++) {
1065 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1074 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1076 struct wpa_auth_config _conf;
1077 struct wpa_auth_callbacks cb;
1081 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1082 os_memset(&cb, 0, sizeof(cb));
1084 cb.logger = hostapd_wpa_auth_logger;
1085 cb.disconnect = hostapd_wpa_auth_disconnect;
1086 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1087 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1088 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1089 cb.get_psk = hostapd_wpa_auth_get_psk;
1090 cb.get_msk = hostapd_wpa_auth_get_msk;
1091 cb.set_key = hostapd_wpa_auth_set_key;
1092 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1093 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1094 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1095 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1096 cb.send_ether = hostapd_wpa_auth_send_ether;
1097 #ifdef CONFIG_IEEE80211R
1098 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1099 cb.add_sta = hostapd_wpa_auth_add_sta;
1100 #endif /* CONFIG_IEEE80211R */
1101 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1102 if (hapd->wpa_auth == NULL) {
1103 printf("WPA initialization failed.\n");
1107 if (hostapd_set_privacy(hapd, 1)) {
1108 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1109 "for interface %s", hapd->conf->iface);
1113 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1114 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1115 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1116 "the kernel driver.");
1120 if (rsn_preauth_iface_init(hapd)) {
1121 printf("Initialization of RSN pre-authentication "
1131 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1132 struct hostapd_bss_config *conf)
1134 struct radius_server_conf srv;
1135 os_memset(&srv, 0, sizeof(srv));
1136 srv.client_file = conf->radius_server_clients;
1137 srv.auth_port = conf->radius_server_auth_port;
1138 srv.conf_ctx = conf;
1139 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1140 srv.ssl_ctx = hapd->ssl_ctx;
1141 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1142 srv.eap_fast_a_id = conf->eap_fast_a_id;
1143 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1144 srv.tnc = conf->tnc;
1145 srv.ipv6 = conf->radius_server_ipv6;
1146 srv.get_eap_user = hostapd_radius_get_eap_user;
1148 hapd->radius_srv = radius_server_init(&srv);
1149 if (hapd->radius_srv == NULL) {
1150 printf("RADIUS server initialization failed.\n");
1159 * hostapd_setup_bss - Per-BSS setup (initialization)
1160 * @hapd: Pointer to BSS data
1161 * @first: Whether this BSS is the first BSS of an interface
1163 * This function is used to initialize all per-BSS data structures and
1164 * resources. This gets called in a loop for each BSS when an interface is
1165 * initialized. Most of the modules that are initialized here will be
1166 * deinitialized in hostapd_cleanup().
1168 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1170 struct hostapd_bss_config *conf = hapd->conf;
1171 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1172 int ssid_len, set_ssid;
1175 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1176 /* Allocate the next available BSSID. */
1178 inc_byte_array(hapd->own_addr, ETH_ALEN);
1179 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1181 /* Allocate the configured BSSID. */
1182 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1184 if (hostapd_mac_comp(hapd->own_addr,
1185 hapd->iface->bss[0]->own_addr) ==
1187 printf("BSS '%s' may not have BSSID "
1188 "set to the MAC address of the radio\n",
1194 hapd->interface_added = 1;
1195 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1197 printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1198 MAC2STR(hapd->own_addr));
1204 * Fetch the SSID from the system and use it or,
1205 * if one was specified in the config file, verify they
1208 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1210 printf("Could not read SSID from system\n");
1213 if (conf->ssid.ssid_set) {
1215 * If SSID is specified in the config file and it differs
1216 * from what is being used then force installation of the
1219 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1220 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1223 * No SSID in the config file; just use the one we got
1227 conf->ssid.ssid_len = ssid_len;
1228 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1229 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1232 printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
1233 hapd->conf->iface, MAC2STR(hapd->own_addr),
1234 hapd->conf->ssid.ssid);
1236 if (hostapd_setup_wpa_psk(conf)) {
1237 printf("WPA-PSK setup failed.\n");
1241 /* Set flag for whether SSID is broadcast in beacons */
1242 if (hostapd_set_broadcast_ssid(hapd,
1243 !!hapd->conf->ignore_broadcast_ssid)) {
1244 printf("Could not set broadcast SSID flag for kernel "
1249 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1250 printf("Could not set DTIM period for kernel driver\n");
1254 /* Set SSID for the kernel driver (to be used in beacon and probe
1255 * response frames) */
1256 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1257 conf->ssid.ssid_len)) {
1258 printf("Could not set SSID for kernel driver\n");
1262 if (wpa_debug_level == MSG_MSGDUMP)
1263 conf->radius->msg_dumps = 1;
1264 hapd->radius = radius_client_init(hapd, conf->radius);
1265 if (hapd->radius == NULL) {
1266 printf("RADIUS client initialization failed.\n");
1270 if (hostapd_acl_init(hapd)) {
1271 printf("ACL initialization failed.\n");
1275 if (ieee802_1x_init(hapd)) {
1276 printf("IEEE 802.1X initialization failed.\n");
1280 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1283 if (accounting_init(hapd)) {
1284 printf("Accounting initialization failed.\n");
1288 if (hapd->conf->ieee802_11f &&
1289 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1290 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1294 if (hostapd_ctrl_iface_init(hapd)) {
1295 printf("Failed to setup control interface\n");
1299 if (vlan_init(hapd)) {
1300 printf("VLAN initialization failed.\n");
1304 #ifdef CONFIG_IEEE80211R
1305 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1306 hostapd_rrb_receive, hapd, 0);
1307 if (hapd->l2 == NULL &&
1308 (hapd->driver == NULL || hapd->driver->send_ether == NULL)) {
1309 printf("Failed to open l2_packet interface\n");
1312 #endif /* CONFIG_IEEE80211R */
1314 ieee802_11_set_beacon(hapd);
1316 if (conf->radius_server_clients &&
1317 hostapd_setup_radius_srv(hapd, conf))
1325 * setup_interface2 - Setup (initialize) an interface (part 2)
1326 * @iface: Pointer to interface data.
1327 * Returns: 0 on success; -1 on failure.
1329 * Flushes old stations, sets the channel, DFS parameters, encryption,
1330 * beacons, and WDS links based on the configuration.
1332 static int setup_interface2(struct hostapd_iface *iface)
1334 struct hostapd_data *hapd = iface->bss[0];
1340 hostapd_flush_old_stations(hapd);
1341 hostapd_set_privacy(hapd, 0);
1343 if (hapd->iconf->channel) {
1344 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1345 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1346 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1347 hapd->iconf->channel, freq);
1349 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1350 printf("Could not set channel for kernel driver\n");
1355 hostapd_broadcast_wep_clear(hapd);
1356 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1359 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1360 ieee802_11_set_beacon(hapd);
1362 if (hapd->iconf->rts_threshold > -1 &&
1363 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1364 printf("Could not set RTS threshold for kernel driver\n");
1368 if (hapd->iconf->fragm_threshold > -1 &&
1369 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1370 printf("Could not set fragmentation threshold for kernel "
1375 prev_addr = hapd->own_addr;
1377 for (j = 0; j < iface->num_bss; j++) {
1378 hapd = iface->bss[j];
1380 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1381 if (hostapd_setup_bss(hapd, j == 0))
1383 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1384 prev_addr = hapd->own_addr;
1387 ap_list_init(iface);
1389 if (hostapd_driver_commit(hapd) < 0) {
1390 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1391 "configuration", __func__);
1399 static void setup_interface_start(void *eloop_data, void *user_ctx);
1400 static void setup_interface2_handler(void *eloop_data, void *user_ctx);
1403 * setup_interface_finalize - Finish setup interface & call the callback
1404 * @iface: Pointer to interface data.
1405 * @status: Status of the setup interface (0 on success; -1 on failure).
1406 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1408 static int setup_interface_finalize(struct hostapd_iface *iface, int status)
1410 hostapd_iface_cb cb;
1412 if (!iface->setup_cb)
1415 eloop_cancel_timeout(setup_interface_start, iface, NULL);
1416 eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
1417 hostapd_select_hw_mode_stop(iface);
1419 cb = iface->setup_cb;
1421 iface->setup_cb = NULL;
1430 * setup_interface2_wrapper - Wrapper for setup_interface2()
1431 * @iface: Pointer to interface data.
1432 * @status: Status of the hw mode select.
1434 * Wrapper for setup_interface2() to calls finalize function upon completion.
1436 static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
1440 printf("Could not select hw_mode and channel. (%d)\n", ret);
1442 ret = setup_interface2(iface);
1444 setup_interface_finalize(iface, ret);
1449 * setup_interface2_handler - Used for immediate call of setup_interface2
1450 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1451 * @user_ctx: Unused.
1453 static void setup_interface2_handler(void *eloop_data, void *user_ctx)
1455 struct hostapd_iface *iface = eloop_data;
1457 setup_interface2_wrapper(iface, 0);
1461 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1462 size_t identity_len, int phase2,
1463 struct eap_user *user)
1465 const struct hostapd_eap_user *eap_user;
1468 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1469 if (eap_user == NULL)
1475 os_memset(user, 0, sizeof(*user));
1476 count = EAP_USER_MAX_METHODS;
1477 if (count > EAP_MAX_METHODS)
1478 count = EAP_MAX_METHODS;
1479 for (i = 0; i < count; i++) {
1480 user->methods[i].vendor = eap_user->methods[i].vendor;
1481 user->methods[i].method = eap_user->methods[i].method;
1484 if (eap_user->password) {
1485 user->password = os_malloc(eap_user->password_len);
1486 if (user->password == NULL)
1488 os_memcpy(user->password, eap_user->password,
1489 eap_user->password_len);
1490 user->password_len = eap_user->password_len;
1491 user->password_hash = eap_user->password_hash;
1493 user->force_version = eap_user->force_version;
1494 user->ttls_auth = eap_user->ttls_auth;
1501 * setup_interface1 - Setup (initialize) an interface (part 1)
1502 * @iface: Pointer to interface data
1503 * Returns: 0 on success, -1 on failure
1505 * Initializes the driver interface, validates the configuration,
1506 * and sets driver parameters based on the configuration.
1507 * Schedules setup_interface2() to be called immediately or after
1508 * hardware mode setup takes place.
1510 static int setup_interface1(struct hostapd_iface *iface)
1512 struct hostapd_data *hapd = iface->bss[0];
1513 struct hostapd_bss_config *conf = hapd->conf;
1516 u8 *b = conf->bssid;
1519 * Initialize the driver interface and make sure that all BSSes get
1520 * configured with a pointer to this driver interface.
1522 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1523 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1525 hapd->drv_priv = hostapd_driver_init(hapd);
1528 if (hapd->drv_priv == NULL) {
1529 printf("%s driver initialization failed.\n",
1530 hapd->driver ? hapd->driver->name : "Unknown");
1531 hapd->driver = NULL;
1534 for (i = 0; i < iface->num_bss; i++) {
1535 iface->bss[i]->driver = hapd->driver;
1536 iface->bss[i]->drv_priv = hapd->drv_priv;
1539 if (hostapd_validate_bssid_configuration(iface))
1542 os_memcpy(country, hapd->iconf->country, 3);
1544 if (hostapd_set_country(hapd, country) < 0) {
1545 printf("Failed to set country code\n");
1549 if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
1550 if (hostapd_set_ieee80211d(hapd, 1) < 0) {
1551 printf("Failed to set ieee80211d (%d)\n",
1552 hapd->iconf->ieee80211d);
1557 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1558 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1559 printf("Failed to set bridge_packets for kernel driver\n");
1563 /* TODO: merge with hostapd_driver_init() ? */
1564 if (hostapd_wireless_event_init(hapd) < 0)
1567 if (hostapd_get_hw_features(iface)) {
1568 /* Not all drivers support this yet, so continue without hw
1571 return hostapd_select_hw_mode_start(iface,
1572 setup_interface2_wrapper);
1575 eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
1581 * setup_interface_start - Handler to start setup interface
1582 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1583 * @user_ctx: Unused.
1585 * An eloop handler is used so that all errors can be processed by the
1586 * callback without introducing stack recursion.
1588 static void setup_interface_start(void *eloop_data, void *user_ctx)
1590 struct hostapd_iface *iface = eloop_data;
1594 ret = setup_interface1(iface);
1596 setup_interface_finalize(iface, ret);
1601 * hostapd_setup_interface_start - Start the setup of an interface
1602 * @iface: Pointer to interface data.
1603 * @cb: The function to callback when done.
1604 * Returns: 0 if it starts successfully; cb will be called when done.
1605 * -1 on failure; cb will not be called.
1607 * Initializes the driver interface, validates the configuration,
1608 * and sets driver parameters based on the configuration.
1609 * Flushes old stations, sets the channel, DFS parameters, encryption,
1610 * beacons, and WDS links based on the configuration.
1612 int hostapd_setup_interface_start(struct hostapd_iface *iface,
1613 hostapd_iface_cb cb)
1615 if (iface->setup_cb) {
1616 wpa_printf(MSG_DEBUG,
1617 "%s: Interface setup already in progress.\n",
1618 iface->bss[0]->conf->iface);
1622 iface->setup_cb = cb;
1624 eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
1631 * hostapd_setup_interace_stop - Stops the setup of an interface
1632 * @iface: Pointer to interface data
1633 * Returns: 0 if successfully stopped;
1634 * -1 on failure (i.e., was not in progress)
1636 int hostapd_setup_interface_stop(struct hostapd_iface *iface)
1638 return setup_interface_finalize(iface, -1);
1642 static void show_version(void)
1645 "hostapd v" VERSION_STR "\n"
1646 "User space daemon for IEEE 802.11 AP management,\n"
1647 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1648 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1649 "and contributors\n");
1653 static void usage(void)
1658 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1659 "<configuration file(s)>\n"
1662 " -h show this usage\n"
1663 " -d show more debug messages (-dd for even more)\n"
1664 " -B run daemon in the background\n"
1666 " -K include key data in debug messages\n"
1667 " -t include timestamps in some debug messages\n"
1668 " -v show hostapd version\n");
1675 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1676 * @hapd_iface: Pointer to interface data
1677 * @conf: Pointer to per-interface configuration
1678 * @bss: Pointer to per-BSS configuration for this BSS
1679 * Returns: Pointer to allocated BSS data
1681 * This function is used to allocate per-BSS data structure. This data will be
1682 * freed after hostapd_cleanup() is called for it during interface
1685 static struct hostapd_data *
1686 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1687 struct hostapd_config *conf,
1688 struct hostapd_bss_config *bss)
1690 struct hostapd_data *hapd;
1692 hapd = os_zalloc(sizeof(*hapd));
1698 hapd->iface = hapd_iface;
1700 if (hapd->conf->individual_wep_key_len > 0) {
1701 /* use key0 in individual key and key1 in broadcast key */
1702 hapd->default_wep_key_idx = 1;
1705 #ifdef EAP_TLS_FUNCS
1706 if (hapd->conf->eap_server &&
1707 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1708 hapd->conf->dh_file)) {
1709 struct tls_connection_params params;
1711 hapd->ssl_ctx = tls_init(NULL);
1712 if (hapd->ssl_ctx == NULL) {
1713 printf("Failed to initialize TLS\n");
1717 os_memset(¶ms, 0, sizeof(params));
1718 params.ca_cert = hapd->conf->ca_cert;
1719 params.client_cert = hapd->conf->server_cert;
1720 params.private_key = hapd->conf->private_key;
1721 params.private_key_passwd = hapd->conf->private_key_passwd;
1722 params.dh_file = hapd->conf->dh_file;
1724 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1725 printf("Failed to set TLS parameters\n");
1729 if (tls_global_set_verify(hapd->ssl_ctx,
1730 hapd->conf->check_crl)) {
1731 printf("Failed to enable check_crl\n");
1735 #endif /* EAP_TLS_FUNCS */
1738 if (hapd->conf->eap_sim_db) {
1739 hapd->eap_sim_db_priv =
1740 eap_sim_db_init(hapd->conf->eap_sim_db,
1741 hostapd_sim_db_cb, hapd);
1742 if (hapd->eap_sim_db_priv == NULL) {
1743 printf("Failed to initialize EAP-SIM database "
1748 #endif /* EAP_SERVER */
1750 if (hapd->conf->assoc_ap)
1751 hapd->assoc_ap_state = WAIT_BEACON;
1753 hapd->driver = hapd->iconf->driver;
1757 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1760 /* TODO: cleanup allocated resources(?) */
1767 * hostapd_init - Allocate and initialize per-interface data
1768 * @config_file: Path to the configuration file
1769 * Returns: Pointer to the allocated interface data or %NULL on failure
1771 * This function is used to allocate main data structures for per-interface
1772 * data. The allocated data buffer will be freed by calling
1773 * hostapd_cleanup_iface().
1775 static struct hostapd_iface * hostapd_init(const char *config_file)
1777 struct hostapd_iface *hapd_iface = NULL;
1778 struct hostapd_config *conf = NULL;
1779 struct hostapd_data *hapd;
1782 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1783 if (hapd_iface == NULL)
1786 hapd_iface->config_fname = os_strdup(config_file);
1787 if (hapd_iface->config_fname == NULL)
1790 conf = hostapd_config_read(hapd_iface->config_fname);
1793 hapd_iface->conf = conf;
1795 hapd_iface->num_bss = conf->num_bss;
1796 hapd_iface->bss = os_zalloc(conf->num_bss *
1797 sizeof(struct hostapd_data *));
1798 if (hapd_iface->bss == NULL)
1801 for (i = 0; i < conf->num_bss; i++) {
1802 hapd = hapd_iface->bss[i] =
1803 hostapd_alloc_bss_data(hapd_iface, conf,
1813 hostapd_config_free(conf);
1815 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1816 hapd = hapd_iface->bss[i];
1817 if (hapd && hapd->ssl_ctx)
1818 tls_deinit(hapd->ssl_ctx);
1821 os_free(hapd_iface->config_fname);
1822 os_free(hapd_iface->bss);
1823 os_free(hapd_iface);
1830 * register_drivers - Register driver interfaces
1832 * This function is generated by Makefile (into driver_conf.c) to call all
1833 * configured driver interfaces to register them to core hostapd.
1835 void register_drivers(void);
1839 * setup_interface_done - Callback when an interface is done being setup.
1840 * @iface: Pointer to interface data.
1841 * @status: Status of the interface setup (0 on success; -1 on failure).
1843 static void setup_interface_done(struct hostapd_iface *iface, int status)
1846 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1847 iface->bss[0]->conf->iface);
1850 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1851 iface->bss[0]->conf->iface);
1855 int main(int argc, char *argv[])
1857 struct hapd_interfaces interfaces;
1860 int c, debug = 0, daemonize = 0, tnc = 0;
1861 const char *pid_file = NULL;
1863 hostapd_logger_register_cb(hostapd_logger_cb);
1866 c = getopt(argc, argv, "BdhKP:tv");
1875 if (wpa_debug_level > 0)
1882 wpa_debug_show_keys++;
1888 wpa_debug_timestamp++;
1904 if (eap_server_register_methods()) {
1905 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1909 interfaces.count = argc - optind;
1911 interfaces.iface = os_malloc(interfaces.count *
1912 sizeof(struct hostapd_iface *));
1913 if (interfaces.iface == NULL) {
1914 wpa_printf(MSG_ERROR, "malloc failed\n");
1918 if (eloop_init(&interfaces)) {
1919 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1923 #ifndef CONFIG_NATIVE_WINDOWS
1924 eloop_register_signal(SIGHUP, handle_reload, NULL);
1925 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1926 #endif /* CONFIG_NATIVE_WINDOWS */
1927 eloop_register_signal_terminate(handle_term, NULL);
1929 /* Initialize interfaces */
1930 for (i = 0; i < interfaces.count; i++) {
1931 printf("Configuration file: %s\n", argv[optind + i]);
1932 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1933 if (!interfaces.iface[i])
1935 for (k = 0; k < debug; k++) {
1936 if (interfaces.iface[i]->bss[0]->conf->
1937 logger_stdout_level > 0)
1938 interfaces.iface[i]->bss[0]->conf->
1939 logger_stdout_level--;
1942 ret = hostapd_setup_interface_start(interfaces.iface[i],
1943 setup_interface_done);
1947 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1948 if (interfaces.iface[i]->bss[0]->conf->tnc)
1954 if (tnc && tncs_global_init() < 0) {
1955 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1958 #endif /* EAP_TNC */
1960 if (daemonize && os_daemonize(pid_file)) {
1965 #ifndef CONFIG_NATIVE_WINDOWS
1966 openlog("hostapd", 0, LOG_DAEMON);
1967 #endif /* CONFIG_NATIVE_WINDOWS */
1971 /* Disconnect associated stations from all interfaces and BSSes */
1972 for (i = 0; i < interfaces.count; i++) {
1973 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1974 struct hostapd_data *hapd =
1975 interfaces.iface[i]->bss[j];
1976 hostapd_free_stas(hapd);
1977 hostapd_flush_old_stations(hapd);
1984 /* Deinitialize all interfaces */
1985 for (i = 0; i < interfaces.count; i++) {
1986 if (!interfaces.iface[i])
1988 hostapd_setup_interface_stop(interfaces.iface[i]);
1989 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1990 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1991 struct hostapd_data *hapd =
1992 interfaces.iface[i]->bss[j];
1993 hostapd_cleanup(hapd);
1994 if (j == interfaces.iface[i]->num_bss - 1 &&
1996 hostapd_driver_deinit(hapd);
1998 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1999 os_free(interfaces.iface[i]->bss[j]);
2000 hostapd_cleanup_iface(interfaces.iface[i]);
2002 os_free(interfaces.iface);
2005 tncs_global_deinit();
2006 #endif /* EAP_TNC */
2010 #ifndef CONFIG_NATIVE_WINDOWS
2012 #endif /* CONFIG_NATIVE_WINDOWS */
2014 eap_server_unregister_methods();
2016 os_daemonize_terminate(pid_file);