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%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_WME ? "[WME]" : ""),
418 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
419 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
421 sta->listen_interval);
423 fprintf(f, " supported_rates=");
424 for (i = 0; i < sta->supported_rates_len; i++)
425 fprintf(f, "%02x ", sta->supported_rates[i]);
429 " timeout_next=%s\n",
430 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
431 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
434 ieee802_1x_dump_state(f, " ", sta);
437 buf = os_malloc(4096);
439 int count = radius_client_get_mib(hapd->radius, buf, 4096);
442 else if (count > 4095)
445 fprintf(f, "%s", buf);
447 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
450 else if (count > 4095)
453 fprintf(f, "%s", buf);
458 #endif /* HOSTAPD_DUMP_STATE */
461 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
463 #ifdef HOSTAPD_DUMP_STATE
464 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
467 for (i = 0; i < hapds->count; i++) {
468 struct hostapd_iface *hapd_iface = hapds->iface[i];
469 for (j = 0; j < hapd_iface->num_bss; j++)
470 hostapd_dump_state(hapd_iface->bss[j]);
472 #endif /* HOSTAPD_DUMP_STATE */
474 #endif /* CONFIG_NATIVE_WINDOWS */
476 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
481 for (i = 0; i < NUM_WEP_KEYS; i++) {
482 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
483 0, i == 0 ? 1 : 0)) {
484 printf("Failed to clear default encryption keys "
485 "(ifname=%s keyidx=%d)\n", ifname, i);
491 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
493 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
498 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
501 struct hostapd_ssid *ssid = &hapd->conf->ssid;
504 if (ssid->wep.default_len &&
505 hostapd_set_encryption(hapd->conf->iface,
506 hapd, "WEP", NULL, idx,
509 idx == ssid->wep.idx)) {
510 printf("Could not set WEP encryption.\n");
514 if (ssid->dyn_vlan_keys) {
516 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
518 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
521 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
527 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
531 printf("Could not set dynamic VLAN WEP "
542 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
543 * @hapd: Pointer to BSS data
545 * This function is used to free all per-BSS data structures and resources.
546 * This gets called in a loop for each BSS between calls to
547 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
548 * is deinitialized. Most of the modules that are initialized in
549 * hostapd_setup_bss() are deinitialized here.
551 static void hostapd_cleanup(struct hostapd_data *hapd)
553 hostapd_ctrl_iface_deinit(hapd);
555 os_free(hapd->default_wep_key);
556 hapd->default_wep_key = NULL;
557 iapp_deinit(hapd->iapp);
559 accounting_deinit(hapd);
560 rsn_preauth_iface_deinit(hapd);
561 if (hapd->wpa_auth) {
562 wpa_deinit(hapd->wpa_auth);
563 hapd->wpa_auth = NULL;
565 if (hostapd_set_privacy(hapd, 0)) {
566 wpa_printf(MSG_DEBUG, "Could not disable "
567 "PrivacyInvoked for interface %s",
571 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
572 wpa_printf(MSG_DEBUG, "Could not remove generic "
573 "information element from interface %s",
577 ieee802_1x_deinit(hapd);
579 hostapd_acl_deinit(hapd);
580 radius_client_deinit(hapd->radius);
582 radius_server_deinit(hapd->radius_srv);
583 hapd->radius_srv = NULL;
585 #ifdef CONFIG_IEEE80211R
586 l2_packet_deinit(hapd->l2);
587 #endif /* CONFIG_IEEE80211R */
589 hostapd_wireless_event_deinit(hapd);
593 tls_deinit(hapd->ssl_ctx);
594 hapd->ssl_ctx = NULL;
596 #endif /* EAP_TLS_FUNCS */
599 if (hapd->eap_sim_db_priv) {
600 eap_sim_db_deinit(hapd->eap_sim_db_priv);
601 hapd->eap_sim_db_priv = NULL;
603 #endif /* EAP_SERVER */
605 if (hapd->interface_added &&
606 hostapd_bss_remove(hapd, hapd->conf->iface)) {
607 printf("Failed to remove BSS interface %s\n",
614 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
615 * @iface: Pointer to interface data
617 * This function is called before per-BSS data structures are deinitialized
618 * with hostapd_cleanup().
620 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
626 * hostapd_cleanup_iface - Complete per-interface cleanup
627 * @iface: Pointer to interface data
629 * This function is called after per-BSS data structures are deinitialized
630 * with hostapd_cleanup().
632 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
634 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
635 iface->hw_features = NULL;
636 os_free(iface->current_rates);
637 iface->current_rates = NULL;
638 ap_list_deinit(iface);
639 hostapd_config_free(iface->conf);
642 os_free(iface->config_fname);
648 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
652 hostapd_broadcast_wep_set(hapd);
654 if (hapd->conf->ssid.wep.default_len)
657 for (i = 0; i < 4; i++) {
658 if (hapd->conf->ssid.wep.key[i] &&
659 hostapd_set_encryption(iface, hapd, "WEP", NULL,
660 i, hapd->conf->ssid.wep.key[i],
661 hapd->conf->ssid.wep.len[i],
662 i == hapd->conf->ssid.wep.idx)) {
663 printf("Could not set WEP encryption.\n");
666 if (hapd->conf->ssid.wep.key[i] &&
667 i == hapd->conf->ssid.wep.idx)
668 hostapd_set_privacy(hapd, 1);
675 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
679 wpa_printf(MSG_DEBUG, "Flushing old station entries");
680 if (hostapd_flush(hapd)) {
681 printf("Could not connect to kernel driver.\n");
684 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
685 hostapd_deauth_all_stas(hapd);
691 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
692 logger_level level, const char *txt)
694 struct hostapd_data *hapd = ctx;
699 hlevel = HOSTAPD_LEVEL_WARNING;
702 hlevel = HOSTAPD_LEVEL_INFO;
706 hlevel = HOSTAPD_LEVEL_DEBUG;
710 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
714 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
717 struct hostapd_data *hapd = ctx;
718 struct sta_info *sta;
720 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
721 "STA " MACSTR " reason %d",
722 __func__, MAC2STR(addr), reason);
724 sta = ap_get_sta(hapd, addr);
725 hostapd_sta_deauth(hapd, addr, reason);
728 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
729 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
730 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
731 sta->timeout_next = STA_REMOVE;
735 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
737 struct hostapd_data *hapd = ctx;
738 ieee80211_michael_mic_failure(hapd, addr, 0);
742 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
743 wpa_eapol_variable var, int value)
745 struct hostapd_data *hapd = ctx;
746 struct sta_info *sta = ap_get_sta(hapd, addr);
750 case WPA_EAPOL_portEnabled:
751 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
753 case WPA_EAPOL_portValid:
754 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
756 case WPA_EAPOL_authorized:
757 ieee802_1x_set_sta_authorized(hapd, sta, value);
759 case WPA_EAPOL_portControl_Auto:
761 sta->eapol_sm->portControl = Auto;
763 case WPA_EAPOL_keyRun:
765 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
767 case WPA_EAPOL_keyAvailable:
769 sta->eapol_sm->eap_if->eapKeyAvailable =
770 value ? TRUE : FALSE;
772 case WPA_EAPOL_keyDone:
774 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
776 case WPA_EAPOL_inc_EapolFramesTx:
778 sta->eapol_sm->dot1xAuthEapolFramesTx++;
784 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
785 wpa_eapol_variable var)
787 struct hostapd_data *hapd = ctx;
788 struct sta_info *sta = ap_get_sta(hapd, addr);
789 if (sta == NULL || sta->eapol_sm == NULL)
792 case WPA_EAPOL_keyRun:
793 return sta->eapol_sm->keyRun;
794 case WPA_EAPOL_keyAvailable:
795 return sta->eapol_sm->eap_if->eapKeyAvailable;
802 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
805 struct hostapd_data *hapd = ctx;
806 return hostapd_get_psk(hapd->conf, addr, prev_psk);
810 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
813 struct hostapd_data *hapd = ctx;
816 struct sta_info *sta;
818 sta = ap_get_sta(hapd, addr);
822 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
828 os_memcpy(msk, key, keylen);
835 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
836 const u8 *addr, int idx, u8 *key,
839 struct hostapd_data *hapd = ctx;
840 const char *ifname = hapd->conf->iface;
843 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
848 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
853 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
856 struct hostapd_data *hapd = ctx;
857 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
861 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
864 struct hostapd_data *hapd = ctx;
865 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
870 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
871 const u8 *data, size_t data_len,
874 struct hostapd_data *hapd = ctx;
875 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
879 static int hostapd_wpa_auth_for_each_sta(
880 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
883 struct hostapd_data *hapd = ctx;
884 struct sta_info *sta;
886 for (sta = hapd->sta_list; sta; sta = sta->next) {
887 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
894 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
895 const u8 *data, size_t data_len)
897 struct hostapd_data *hapd = ctx;
899 if (hapd->driver && hapd->driver->send_ether)
900 return hapd->driver->send_ether(hapd->drv_priv, dst,
901 hapd->own_addr, proto,
903 if (hapd->l2 == NULL)
905 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
909 #ifdef CONFIG_IEEE80211R
911 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
912 const u8 *data, size_t data_len)
914 struct hostapd_data *hapd = ctx;
916 struct ieee80211_mgmt *m;
918 struct sta_info *sta;
920 sta = ap_get_sta(hapd, dst);
921 if (sta == NULL || sta->wpa_sm == NULL)
924 m = os_zalloc(sizeof(*m) + data_len);
927 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
928 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
929 WLAN_FC_STYPE_ACTION);
930 os_memcpy(m->da, dst, ETH_ALEN);
931 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
932 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
933 os_memcpy(&m->u, data, data_len);
935 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
941 static struct wpa_state_machine *
942 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
944 struct hostapd_data *hapd = ctx;
945 struct sta_info *sta;
947 sta = ap_sta_add(hapd, sta_addr);
953 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
954 if (sta->wpa_sm == NULL) {
955 ap_free_sta(hapd, sta);
958 sta->auth_alg = WLAN_AUTH_FT;
964 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
967 struct hostapd_data *hapd = ctx;
968 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
971 #endif /* CONFIG_IEEE80211R */
975 * hostapd_validate_bssid_configuration - Validate BSSID configuration
976 * @iface: Pointer to interface data
977 * Returns: 0 on success, -1 on failure
979 * This function is used to validate that the configured BSSIDs are valid.
981 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
983 u8 mask[ETH_ALEN] = { 0 };
984 struct hostapd_data *hapd = iface->bss[0];
985 unsigned int i = iface->conf->num_bss, bits = 0, j;
988 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
990 /* Determine the bits necessary to cover the number of BSSIDs. */
991 for (i--; i; i >>= 1)
994 /* Determine the bits necessary to any configured BSSIDs,
995 if they are higher than the number of BSSIDs. */
996 for (j = 0; j < iface->conf->num_bss; j++) {
997 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1000 for (i = 0; i < ETH_ALEN; i++) {
1002 iface->conf->bss[j].bssid[i] ^
1007 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1013 while (mask[i] != 0) {
1025 os_memset(mask, 0xff, ETH_ALEN);
1027 for (i = 5; i > 5 - j; i--)
1033 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1034 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1036 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1041 printf("Driver did not accept BSSID mask " MACSTR " for start "
1042 "address " MACSTR ".\n",
1043 MAC2STR(mask), MAC2STR(hapd->own_addr));
1047 for (i = 0; i < ETH_ALEN; i++) {
1048 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1049 printf("Invalid BSSID mask " MACSTR " for start "
1050 "address " MACSTR ".\n"
1051 "Start address must be the first address in the"
1052 " block (i.e., addr AND mask == addr).\n",
1053 MAC2STR(mask), MAC2STR(hapd->own_addr));
1062 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1066 for (i = 0; i < conf->num_bss; i++) {
1067 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1076 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1078 struct wpa_auth_config _conf;
1079 struct wpa_auth_callbacks cb;
1083 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1084 os_memset(&cb, 0, sizeof(cb));
1086 cb.logger = hostapd_wpa_auth_logger;
1087 cb.disconnect = hostapd_wpa_auth_disconnect;
1088 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1089 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1090 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1091 cb.get_psk = hostapd_wpa_auth_get_psk;
1092 cb.get_msk = hostapd_wpa_auth_get_msk;
1093 cb.set_key = hostapd_wpa_auth_set_key;
1094 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1095 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1096 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1097 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1098 cb.send_ether = hostapd_wpa_auth_send_ether;
1099 #ifdef CONFIG_IEEE80211R
1100 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1101 cb.add_sta = hostapd_wpa_auth_add_sta;
1102 #endif /* CONFIG_IEEE80211R */
1103 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1104 if (hapd->wpa_auth == NULL) {
1105 printf("WPA initialization failed.\n");
1109 if (hostapd_set_privacy(hapd, 1)) {
1110 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1111 "for interface %s", hapd->conf->iface);
1115 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1116 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1117 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1118 "the kernel driver.");
1122 if (rsn_preauth_iface_init(hapd)) {
1123 printf("Initialization of RSN pre-authentication "
1133 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1134 struct hostapd_bss_config *conf)
1136 struct radius_server_conf srv;
1137 os_memset(&srv, 0, sizeof(srv));
1138 srv.client_file = conf->radius_server_clients;
1139 srv.auth_port = conf->radius_server_auth_port;
1140 srv.conf_ctx = conf;
1141 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1142 srv.ssl_ctx = hapd->ssl_ctx;
1143 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1144 srv.eap_fast_a_id = conf->eap_fast_a_id;
1145 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1146 srv.tnc = conf->tnc;
1147 srv.ipv6 = conf->radius_server_ipv6;
1148 srv.get_eap_user = hostapd_radius_get_eap_user;
1150 hapd->radius_srv = radius_server_init(&srv);
1151 if (hapd->radius_srv == NULL) {
1152 printf("RADIUS server initialization failed.\n");
1161 * hostapd_setup_bss - Per-BSS setup (initialization)
1162 * @hapd: Pointer to BSS data
1163 * @first: Whether this BSS is the first BSS of an interface
1165 * This function is used to initialize all per-BSS data structures and
1166 * resources. This gets called in a loop for each BSS when an interface is
1167 * initialized. Most of the modules that are initialized here will be
1168 * deinitialized in hostapd_cleanup().
1170 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1172 struct hostapd_bss_config *conf = hapd->conf;
1173 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1174 int ssid_len, set_ssid;
1177 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1178 /* Allocate the next available BSSID. */
1180 inc_byte_array(hapd->own_addr, ETH_ALEN);
1181 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1183 /* Allocate the configured BSSID. */
1184 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1186 if (hostapd_mac_comp(hapd->own_addr,
1187 hapd->iface->bss[0]->own_addr) ==
1189 printf("BSS '%s' may not have BSSID "
1190 "set to the MAC address of the radio\n",
1196 hapd->interface_added = 1;
1197 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1199 printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1200 MAC2STR(hapd->own_addr));
1206 * Fetch the SSID from the system and use it or,
1207 * if one was specified in the config file, verify they
1210 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1212 printf("Could not read SSID from system\n");
1215 if (conf->ssid.ssid_set) {
1217 * If SSID is specified in the config file and it differs
1218 * from what is being used then force installation of the
1221 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1222 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1225 * No SSID in the config file; just use the one we got
1229 conf->ssid.ssid_len = ssid_len;
1230 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1231 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1234 printf("Using interface %s with hwaddr " MACSTR " and ssid '%s'\n",
1235 hapd->conf->iface, MAC2STR(hapd->own_addr),
1236 hapd->conf->ssid.ssid);
1238 if (hostapd_setup_wpa_psk(conf)) {
1239 printf("WPA-PSK setup failed.\n");
1243 /* Set flag for whether SSID is broadcast in beacons */
1244 if (hostapd_set_broadcast_ssid(hapd,
1245 !!hapd->conf->ignore_broadcast_ssid)) {
1246 printf("Could not set broadcast SSID flag for kernel "
1251 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1252 printf("Could not set DTIM period for kernel driver\n");
1256 /* Set SSID for the kernel driver (to be used in beacon and probe
1257 * response frames) */
1258 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1259 conf->ssid.ssid_len)) {
1260 printf("Could not set SSID for kernel driver\n");
1264 if (wpa_debug_level == MSG_MSGDUMP)
1265 conf->radius->msg_dumps = 1;
1266 hapd->radius = radius_client_init(hapd, conf->radius);
1267 if (hapd->radius == NULL) {
1268 printf("RADIUS client initialization failed.\n");
1272 if (hostapd_acl_init(hapd)) {
1273 printf("ACL initialization failed.\n");
1277 if (ieee802_1x_init(hapd)) {
1278 printf("IEEE 802.1X initialization failed.\n");
1282 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1285 if (accounting_init(hapd)) {
1286 printf("Accounting initialization failed.\n");
1290 if (hapd->conf->ieee802_11f &&
1291 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1292 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1296 if (hostapd_ctrl_iface_init(hapd)) {
1297 printf("Failed to setup control interface\n");
1301 if (vlan_init(hapd)) {
1302 printf("VLAN initialization failed.\n");
1306 #ifdef CONFIG_IEEE80211R
1307 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1308 hostapd_rrb_receive, hapd, 0);
1309 if (hapd->l2 == NULL &&
1310 (hapd->driver == NULL || hapd->driver->send_ether == NULL)) {
1311 printf("Failed to open l2_packet interface\n");
1314 #endif /* CONFIG_IEEE80211R */
1316 ieee802_11_set_beacon(hapd);
1318 if (conf->radius_server_clients &&
1319 hostapd_setup_radius_srv(hapd, conf))
1327 * setup_interface2 - Setup (initialize) an interface (part 2)
1328 * @iface: Pointer to interface data.
1329 * Returns: 0 on success; -1 on failure.
1331 * Flushes old stations, sets the channel, DFS parameters, encryption,
1332 * beacons, and WDS links based on the configuration.
1334 static int setup_interface2(struct hostapd_iface *iface)
1336 struct hostapd_data *hapd = iface->bss[0];
1342 hostapd_flush_old_stations(hapd);
1343 hostapd_set_privacy(hapd, 0);
1345 if (hapd->iconf->channel) {
1346 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1347 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1348 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1349 hapd->iconf->channel, freq);
1351 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1352 printf("Could not set channel for kernel driver\n");
1357 hostapd_broadcast_wep_clear(hapd);
1358 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1361 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1362 ieee802_11_set_beacon(hapd);
1364 if (hapd->iconf->rts_threshold > -1 &&
1365 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1366 printf("Could not set RTS threshold for kernel driver\n");
1370 if (hapd->iconf->fragm_threshold > -1 &&
1371 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1372 printf("Could not set fragmentation threshold for kernel "
1377 prev_addr = hapd->own_addr;
1379 for (j = 0; j < iface->num_bss; j++) {
1380 hapd = iface->bss[j];
1382 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1383 if (hostapd_setup_bss(hapd, j == 0))
1385 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1386 prev_addr = hapd->own_addr;
1389 ap_list_init(iface);
1391 if (hostapd_driver_commit(hapd) < 0) {
1392 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1393 "configuration", __func__);
1401 static void setup_interface_start(void *eloop_data, void *user_ctx);
1402 static void setup_interface2_handler(void *eloop_data, void *user_ctx);
1405 * setup_interface_finalize - Finish setup interface & call the callback
1406 * @iface: Pointer to interface data.
1407 * @status: Status of the setup interface (0 on success; -1 on failure).
1408 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1410 static int setup_interface_finalize(struct hostapd_iface *iface, int status)
1412 hostapd_iface_cb cb;
1414 if (!iface->setup_cb)
1417 eloop_cancel_timeout(setup_interface_start, iface, NULL);
1418 eloop_cancel_timeout(setup_interface2_handler, iface, NULL);
1419 hostapd_select_hw_mode_stop(iface);
1421 cb = iface->setup_cb;
1423 iface->setup_cb = NULL;
1432 * setup_interface2_wrapper - Wrapper for setup_interface2()
1433 * @iface: Pointer to interface data.
1434 * @status: Status of the hw mode select.
1436 * Wrapper for setup_interface2() to calls finalize function upon completion.
1438 static void setup_interface2_wrapper(struct hostapd_iface *iface, int status)
1442 printf("Could not select hw_mode and channel. (%d)\n", ret);
1444 ret = setup_interface2(iface);
1446 setup_interface_finalize(iface, ret);
1451 * setup_interface2_handler - Used for immediate call of setup_interface2
1452 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1453 * @user_ctx: Unused.
1455 static void setup_interface2_handler(void *eloop_data, void *user_ctx)
1457 struct hostapd_iface *iface = eloop_data;
1459 setup_interface2_wrapper(iface, 0);
1463 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1464 size_t identity_len, int phase2,
1465 struct eap_user *user)
1467 const struct hostapd_eap_user *eap_user;
1470 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1471 if (eap_user == NULL)
1477 os_memset(user, 0, sizeof(*user));
1478 count = EAP_USER_MAX_METHODS;
1479 if (count > EAP_MAX_METHODS)
1480 count = EAP_MAX_METHODS;
1481 for (i = 0; i < count; i++) {
1482 user->methods[i].vendor = eap_user->methods[i].vendor;
1483 user->methods[i].method = eap_user->methods[i].method;
1486 if (eap_user->password) {
1487 user->password = os_malloc(eap_user->password_len);
1488 if (user->password == NULL)
1490 os_memcpy(user->password, eap_user->password,
1491 eap_user->password_len);
1492 user->password_len = eap_user->password_len;
1493 user->password_hash = eap_user->password_hash;
1495 user->force_version = eap_user->force_version;
1496 user->ttls_auth = eap_user->ttls_auth;
1503 * setup_interface1 - Setup (initialize) an interface (part 1)
1504 * @iface: Pointer to interface data
1505 * Returns: 0 on success, -1 on failure
1507 * Initializes the driver interface, validates the configuration,
1508 * and sets driver parameters based on the configuration.
1509 * Schedules setup_interface2() to be called immediately or after
1510 * hardware mode setup takes place.
1512 static int setup_interface1(struct hostapd_iface *iface)
1514 struct hostapd_data *hapd = iface->bss[0];
1515 struct hostapd_bss_config *conf = hapd->conf;
1518 u8 *b = conf->bssid;
1521 * Initialize the driver interface and make sure that all BSSes get
1522 * configured with a pointer to this driver interface.
1524 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1525 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1527 hapd->drv_priv = hostapd_driver_init(hapd);
1530 if (hapd->drv_priv == NULL) {
1531 printf("%s driver initialization failed.\n",
1532 hapd->driver ? hapd->driver->name : "Unknown");
1533 hapd->driver = NULL;
1536 for (i = 0; i < iface->num_bss; i++) {
1537 iface->bss[i]->driver = hapd->driver;
1538 iface->bss[i]->drv_priv = hapd->drv_priv;
1541 if (hostapd_validate_bssid_configuration(iface))
1544 os_memcpy(country, hapd->iconf->country, 3);
1546 if (hostapd_set_country(hapd, country) < 0) {
1547 printf("Failed to set country code\n");
1551 if (hapd->iconf->ieee80211d || hapd->iconf->ieee80211h) {
1552 if (hostapd_set_ieee80211d(hapd, 1) < 0) {
1553 printf("Failed to set ieee80211d (%d)\n",
1554 hapd->iconf->ieee80211d);
1559 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1560 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1561 printf("Failed to set bridge_packets for kernel driver\n");
1565 /* TODO: merge with hostapd_driver_init() ? */
1566 if (hostapd_wireless_event_init(hapd) < 0)
1569 if (hostapd_get_hw_features(iface)) {
1570 /* Not all drivers support this yet, so continue without hw
1573 return hostapd_select_hw_mode_start(iface,
1574 setup_interface2_wrapper);
1577 eloop_register_timeout(0, 0, setup_interface2_handler, iface, NULL);
1583 * setup_interface_start - Handler to start setup interface
1584 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1585 * @user_ctx: Unused.
1587 * An eloop handler is used so that all errors can be processed by the
1588 * callback without introducing stack recursion.
1590 static void setup_interface_start(void *eloop_data, void *user_ctx)
1592 struct hostapd_iface *iface = eloop_data;
1596 ret = setup_interface1(iface);
1598 setup_interface_finalize(iface, ret);
1603 * hostapd_setup_interface_start - Start the setup of an interface
1604 * @iface: Pointer to interface data.
1605 * @cb: The function to callback when done.
1606 * Returns: 0 if it starts successfully; cb will be called when done.
1607 * -1 on failure; cb will not be called.
1609 * Initializes the driver interface, validates the configuration,
1610 * and sets driver parameters based on the configuration.
1611 * Flushes old stations, sets the channel, DFS parameters, encryption,
1612 * beacons, and WDS links based on the configuration.
1614 int hostapd_setup_interface_start(struct hostapd_iface *iface,
1615 hostapd_iface_cb cb)
1617 if (iface->setup_cb) {
1618 wpa_printf(MSG_DEBUG,
1619 "%s: Interface setup already in progress.\n",
1620 iface->bss[0]->conf->iface);
1624 iface->setup_cb = cb;
1626 eloop_register_timeout(0, 0, setup_interface_start, iface, NULL);
1633 * hostapd_setup_interace_stop - Stops the setup of an interface
1634 * @iface: Pointer to interface data
1635 * Returns: 0 if successfully stopped;
1636 * -1 on failure (i.e., was not in progress)
1638 int hostapd_setup_interface_stop(struct hostapd_iface *iface)
1640 return setup_interface_finalize(iface, -1);
1644 static void show_version(void)
1647 "hostapd v" VERSION_STR "\n"
1648 "User space daemon for IEEE 802.11 AP management,\n"
1649 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1650 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1651 "and contributors\n");
1655 static void usage(void)
1660 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1661 "<configuration file(s)>\n"
1664 " -h show this usage\n"
1665 " -d show more debug messages (-dd for even more)\n"
1666 " -B run daemon in the background\n"
1668 " -K include key data in debug messages\n"
1669 " -t include timestamps in some debug messages\n"
1670 " -v show hostapd version\n");
1677 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1678 * @hapd_iface: Pointer to interface data
1679 * @conf: Pointer to per-interface configuration
1680 * @bss: Pointer to per-BSS configuration for this BSS
1681 * Returns: Pointer to allocated BSS data
1683 * This function is used to allocate per-BSS data structure. This data will be
1684 * freed after hostapd_cleanup() is called for it during interface
1687 static struct hostapd_data *
1688 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1689 struct hostapd_config *conf,
1690 struct hostapd_bss_config *bss)
1692 struct hostapd_data *hapd;
1694 hapd = os_zalloc(sizeof(*hapd));
1700 hapd->iface = hapd_iface;
1702 if (hapd->conf->individual_wep_key_len > 0) {
1703 /* use key0 in individual key and key1 in broadcast key */
1704 hapd->default_wep_key_idx = 1;
1707 #ifdef EAP_TLS_FUNCS
1708 if (hapd->conf->eap_server &&
1709 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1710 hapd->conf->dh_file)) {
1711 struct tls_connection_params params;
1713 hapd->ssl_ctx = tls_init(NULL);
1714 if (hapd->ssl_ctx == NULL) {
1715 printf("Failed to initialize TLS\n");
1719 os_memset(¶ms, 0, sizeof(params));
1720 params.ca_cert = hapd->conf->ca_cert;
1721 params.client_cert = hapd->conf->server_cert;
1722 params.private_key = hapd->conf->private_key;
1723 params.private_key_passwd = hapd->conf->private_key_passwd;
1724 params.dh_file = hapd->conf->dh_file;
1726 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1727 printf("Failed to set TLS parameters\n");
1731 if (tls_global_set_verify(hapd->ssl_ctx,
1732 hapd->conf->check_crl)) {
1733 printf("Failed to enable check_crl\n");
1737 #endif /* EAP_TLS_FUNCS */
1740 if (hapd->conf->eap_sim_db) {
1741 hapd->eap_sim_db_priv =
1742 eap_sim_db_init(hapd->conf->eap_sim_db,
1743 hostapd_sim_db_cb, hapd);
1744 if (hapd->eap_sim_db_priv == NULL) {
1745 printf("Failed to initialize EAP-SIM database "
1750 #endif /* EAP_SERVER */
1752 if (hapd->conf->assoc_ap)
1753 hapd->assoc_ap_state = WAIT_BEACON;
1755 hapd->driver = hapd->iconf->driver;
1759 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1762 /* TODO: cleanup allocated resources(?) */
1769 * hostapd_init - Allocate and initialize per-interface data
1770 * @config_file: Path to the configuration file
1771 * Returns: Pointer to the allocated interface data or %NULL on failure
1773 * This function is used to allocate main data structures for per-interface
1774 * data. The allocated data buffer will be freed by calling
1775 * hostapd_cleanup_iface().
1777 static struct hostapd_iface * hostapd_init(const char *config_file)
1779 struct hostapd_iface *hapd_iface = NULL;
1780 struct hostapd_config *conf = NULL;
1781 struct hostapd_data *hapd;
1784 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1785 if (hapd_iface == NULL)
1788 hapd_iface->config_fname = os_strdup(config_file);
1789 if (hapd_iface->config_fname == NULL)
1792 conf = hostapd_config_read(hapd_iface->config_fname);
1795 hapd_iface->conf = conf;
1797 hapd_iface->num_bss = conf->num_bss;
1798 hapd_iface->bss = os_zalloc(conf->num_bss *
1799 sizeof(struct hostapd_data *));
1800 if (hapd_iface->bss == NULL)
1803 for (i = 0; i < conf->num_bss; i++) {
1804 hapd = hapd_iface->bss[i] =
1805 hostapd_alloc_bss_data(hapd_iface, conf,
1815 hostapd_config_free(conf);
1817 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1818 hapd = hapd_iface->bss[i];
1819 if (hapd && hapd->ssl_ctx)
1820 tls_deinit(hapd->ssl_ctx);
1823 os_free(hapd_iface->config_fname);
1824 os_free(hapd_iface->bss);
1825 os_free(hapd_iface);
1832 * register_drivers - Register driver interfaces
1834 * This function is generated by Makefile (into driver_conf.c) to call all
1835 * configured driver interfaces to register them to core hostapd.
1837 void register_drivers(void);
1841 * setup_interface_done - Callback when an interface is done being setup.
1842 * @iface: Pointer to interface data.
1843 * @status: Status of the interface setup (0 on success; -1 on failure).
1845 static void setup_interface_done(struct hostapd_iface *iface, int status)
1848 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1849 iface->bss[0]->conf->iface);
1852 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1853 iface->bss[0]->conf->iface);
1857 int main(int argc, char *argv[])
1859 struct hapd_interfaces interfaces;
1862 int c, debug = 0, daemonize = 0, tnc = 0;
1863 const char *pid_file = NULL;
1865 hostapd_logger_register_cb(hostapd_logger_cb);
1868 c = getopt(argc, argv, "BdhKP:tv");
1877 if (wpa_debug_level > 0)
1884 wpa_debug_show_keys++;
1890 wpa_debug_timestamp++;
1906 if (eap_server_register_methods()) {
1907 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1911 interfaces.count = argc - optind;
1913 interfaces.iface = os_malloc(interfaces.count *
1914 sizeof(struct hostapd_iface *));
1915 if (interfaces.iface == NULL) {
1916 wpa_printf(MSG_ERROR, "malloc failed\n");
1920 if (eloop_init(&interfaces)) {
1921 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1925 #ifndef CONFIG_NATIVE_WINDOWS
1926 eloop_register_signal(SIGHUP, handle_reload, NULL);
1927 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1928 #endif /* CONFIG_NATIVE_WINDOWS */
1929 eloop_register_signal_terminate(handle_term, NULL);
1931 /* Initialize interfaces */
1932 for (i = 0; i < interfaces.count; i++) {
1933 printf("Configuration file: %s\n", argv[optind + i]);
1934 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1935 if (!interfaces.iface[i])
1937 for (k = 0; k < debug; k++) {
1938 if (interfaces.iface[i]->bss[0]->conf->
1939 logger_stdout_level > 0)
1940 interfaces.iface[i]->bss[0]->conf->
1941 logger_stdout_level--;
1944 ret = hostapd_setup_interface_start(interfaces.iface[i],
1945 setup_interface_done);
1949 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1950 if (interfaces.iface[i]->bss[0]->conf->tnc)
1956 if (tnc && tncs_global_init() < 0) {
1957 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1960 #endif /* EAP_TNC */
1962 if (daemonize && os_daemonize(pid_file)) {
1967 #ifndef CONFIG_NATIVE_WINDOWS
1968 openlog("hostapd", 0, LOG_DAEMON);
1969 #endif /* CONFIG_NATIVE_WINDOWS */
1973 /* Disconnect associated stations from all interfaces and BSSes */
1974 for (i = 0; i < interfaces.count; 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_free_stas(hapd);
1979 hostapd_flush_old_stations(hapd);
1986 /* Deinitialize all interfaces */
1987 for (i = 0; i < interfaces.count; i++) {
1988 if (!interfaces.iface[i])
1990 hostapd_setup_interface_stop(interfaces.iface[i]);
1991 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1992 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1993 struct hostapd_data *hapd =
1994 interfaces.iface[i]->bss[j];
1995 hostapd_cleanup(hapd);
1996 if (j == interfaces.iface[i]->num_bss - 1 &&
1998 hostapd_driver_deinit(hapd);
2000 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2001 os_free(interfaces.iface[i]->bss[j]);
2002 hostapd_cleanup_iface(interfaces.iface[i]);
2004 os_free(interfaces.iface);
2007 tncs_global_deinit();
2008 #endif /* EAP_TNC */
2012 #ifndef CONFIG_NATIVE_WINDOWS
2014 #endif /* CONFIG_NATIVE_WINDOWS */
2016 eap_server_unregister_methods();
2018 os_daemonize_terminate(pid_file);