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 (hapd->driver && 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->wpa_ptk_rekey = conf->wpa_ptk_rekey;
297 wconf->rsn_pairwise = conf->rsn_pairwise;
298 wconf->rsn_preauth = conf->rsn_preauth;
299 wconf->eapol_version = conf->eapol_version;
300 wconf->peerkey = conf->peerkey;
301 wconf->wme_enabled = conf->wme_enabled;
302 wconf->okc = conf->okc;
303 #ifdef CONFIG_IEEE80211W
304 wconf->ieee80211w = conf->ieee80211w;
305 #endif /* CONFIG_IEEE80211W */
306 #ifdef CONFIG_IEEE80211R
307 wconf->ssid_len = conf->ssid.ssid_len;
308 if (wconf->ssid_len > SSID_LEN)
309 wconf->ssid_len = SSID_LEN;
310 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
311 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
312 MOBILITY_DOMAIN_ID_LEN);
313 if (conf->nas_identifier &&
314 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
315 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
316 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
317 wconf->r0_key_holder_len);
319 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
320 wconf->r0_key_lifetime = conf->r0_key_lifetime;
321 wconf->reassociation_deadline = conf->reassociation_deadline;
322 wconf->r0kh_list = conf->r0kh_list;
323 wconf->r1kh_list = conf->r1kh_list;
324 wconf->pmk_r1_push = conf->pmk_r1_push;
325 #endif /* CONFIG_IEEE80211R */
329 #ifndef CONFIG_NATIVE_WINDOWS
330 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
332 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
333 struct hostapd_config *newconf;
335 struct wpa_auth_config wpa_auth_conf;
337 printf("Signal %d received - reloading configuration\n", sig);
339 for (i = 0; i < hapds->count; i++) {
340 struct hostapd_data *hapd = hapds->iface[i]->bss[0];
341 newconf = hostapd_config_read(hapds->iface[i]->config_fname);
342 if (newconf == NULL) {
343 printf("Failed to read new configuration file - "
344 "continuing with old.\n");
347 /* TODO: update dynamic data based on changed configuration
348 * items (e.g., open/close sockets, remove stations added to
349 * deny list, etc.) */
350 radius_client_flush(hapd->radius, 0);
351 hostapd_config_free(hapd->iconf);
353 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
354 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
356 hapd->iconf = newconf;
357 hapd->conf = &newconf->bss[0];
358 hapds->iface[i]->conf = newconf;
360 if (hostapd_setup_wpa_psk(hapd->conf)) {
361 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
362 "after reloading configuration");
368 #ifdef HOSTAPD_DUMP_STATE
369 static void hostapd_dump_state(struct hostapd_data *hapd)
373 struct sta_info *sta;
377 if (!hapd->conf->dump_log_name) {
378 printf("Dump file not defined - ignoring dump request\n");
382 printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
383 f = fopen(hapd->conf->dump_log_name, "w");
385 printf("Could not open dump file '%s' for writing.\n",
386 hapd->conf->dump_log_name);
391 fprintf(f, "hostapd state dump - %s", ctime(&now));
392 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
393 "num_sta_no_short_slot_time=%d\n"
394 "num_sta_no_short_preamble=%d\n",
395 hapd->num_sta, hapd->iface->num_sta_non_erp,
396 hapd->iface->num_sta_no_short_slot_time,
397 hapd->iface->num_sta_no_short_preamble);
399 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
400 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
403 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s\n"
404 " capability=0x%x listen_interval=%d\n",
407 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
408 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
409 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
410 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
411 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
412 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
414 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
416 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
417 "[SHORT_PREAMBLE]" : ""),
418 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
419 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
420 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
421 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
423 sta->listen_interval);
425 fprintf(f, " supported_rates=");
426 for (i = 0; i < sta->supported_rates_len; i++)
427 fprintf(f, "%02x ", sta->supported_rates[i]);
431 " timeout_next=%s\n",
432 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
433 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
436 ieee802_1x_dump_state(f, " ", sta);
439 buf = os_malloc(4096);
441 int count = radius_client_get_mib(hapd->radius, buf, 4096);
444 else if (count > 4095)
447 fprintf(f, "%s", buf);
449 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
452 else if (count > 4095)
455 fprintf(f, "%s", buf);
460 #endif /* HOSTAPD_DUMP_STATE */
463 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
465 #ifdef HOSTAPD_DUMP_STATE
466 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
469 for (i = 0; i < hapds->count; i++) {
470 struct hostapd_iface *hapd_iface = hapds->iface[i];
471 for (j = 0; j < hapd_iface->num_bss; j++)
472 hostapd_dump_state(hapd_iface->bss[j]);
474 #endif /* HOSTAPD_DUMP_STATE */
476 #endif /* CONFIG_NATIVE_WINDOWS */
478 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
483 for (i = 0; i < NUM_WEP_KEYS; i++) {
484 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
485 0, i == 0 ? 1 : 0)) {
486 printf("Failed to clear default encryption keys "
487 "(ifname=%s keyidx=%d)\n", ifname, i);
493 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
495 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
500 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
503 struct hostapd_ssid *ssid = &hapd->conf->ssid;
506 if (ssid->wep.default_len &&
507 hostapd_set_encryption(hapd->conf->iface,
508 hapd, "WEP", NULL, idx,
511 idx == ssid->wep.idx)) {
512 printf("Could not set WEP encryption.\n");
516 if (ssid->dyn_vlan_keys) {
518 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
520 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
523 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
529 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
533 printf("Could not set dynamic VLAN WEP "
544 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
545 * @hapd: Pointer to BSS data
547 * This function is used to free all per-BSS data structures and resources.
548 * This gets called in a loop for each BSS between calls to
549 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
550 * is deinitialized. Most of the modules that are initialized in
551 * hostapd_setup_bss() are deinitialized here.
553 static void hostapd_cleanup(struct hostapd_data *hapd)
555 hostapd_ctrl_iface_deinit(hapd);
557 os_free(hapd->default_wep_key);
558 hapd->default_wep_key = NULL;
559 iapp_deinit(hapd->iapp);
561 accounting_deinit(hapd);
562 rsn_preauth_iface_deinit(hapd);
563 if (hapd->wpa_auth) {
564 wpa_deinit(hapd->wpa_auth);
565 hapd->wpa_auth = NULL;
567 if (hostapd_set_privacy(hapd, 0)) {
568 wpa_printf(MSG_DEBUG, "Could not disable "
569 "PrivacyInvoked for interface %s",
573 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
574 wpa_printf(MSG_DEBUG, "Could not remove generic "
575 "information element from interface %s",
579 ieee802_1x_deinit(hapd);
581 hostapd_acl_deinit(hapd);
582 radius_client_deinit(hapd->radius);
584 radius_server_deinit(hapd->radius_srv);
585 hapd->radius_srv = NULL;
587 #ifdef CONFIG_IEEE80211R
588 l2_packet_deinit(hapd->l2);
589 #endif /* CONFIG_IEEE80211R */
591 hostapd_wireless_event_deinit(hapd);
595 tls_deinit(hapd->ssl_ctx);
596 hapd->ssl_ctx = NULL;
598 #endif /* EAP_TLS_FUNCS */
601 if (hapd->eap_sim_db_priv) {
602 eap_sim_db_deinit(hapd->eap_sim_db_priv);
603 hapd->eap_sim_db_priv = NULL;
605 #endif /* EAP_SERVER */
607 if (hapd->interface_added &&
608 hostapd_bss_remove(hapd, hapd->conf->iface)) {
609 printf("Failed to remove BSS interface %s\n",
616 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
617 * @iface: Pointer to interface data
619 * This function is called before per-BSS data structures are deinitialized
620 * with hostapd_cleanup().
622 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
628 * hostapd_cleanup_iface - Complete per-interface cleanup
629 * @iface: Pointer to interface data
631 * This function is called after per-BSS data structures are deinitialized
632 * with hostapd_cleanup().
634 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
636 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
637 iface->hw_features = NULL;
638 os_free(iface->current_rates);
639 iface->current_rates = NULL;
640 ap_list_deinit(iface);
641 hostapd_config_free(iface->conf);
644 os_free(iface->config_fname);
650 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
654 hostapd_broadcast_wep_set(hapd);
656 if (hapd->conf->ssid.wep.default_len)
659 for (i = 0; i < 4; i++) {
660 if (hapd->conf->ssid.wep.key[i] &&
661 hostapd_set_encryption(iface, hapd, "WEP", NULL,
662 i, hapd->conf->ssid.wep.key[i],
663 hapd->conf->ssid.wep.len[i],
664 i == hapd->conf->ssid.wep.idx)) {
665 printf("Could not set WEP encryption.\n");
668 if (hapd->conf->ssid.wep.key[i] &&
669 i == hapd->conf->ssid.wep.idx)
670 hostapd_set_privacy(hapd, 1);
677 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
681 if (hostapd_drv_none(hapd))
684 wpa_printf(MSG_DEBUG, "Flushing old station entries");
685 if (hostapd_flush(hapd)) {
686 printf("Could not connect to kernel driver.\n");
689 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
690 hostapd_deauth_all_stas(hapd);
696 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
697 logger_level level, const char *txt)
699 struct hostapd_data *hapd = ctx;
704 hlevel = HOSTAPD_LEVEL_WARNING;
707 hlevel = HOSTAPD_LEVEL_INFO;
711 hlevel = HOSTAPD_LEVEL_DEBUG;
715 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
719 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
722 struct hostapd_data *hapd = ctx;
723 struct sta_info *sta;
725 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
726 "STA " MACSTR " reason %d",
727 __func__, MAC2STR(addr), reason);
729 sta = ap_get_sta(hapd, addr);
730 hostapd_sta_deauth(hapd, addr, reason);
733 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
734 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
735 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
736 sta->timeout_next = STA_REMOVE;
740 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
742 struct hostapd_data *hapd = ctx;
743 ieee80211_michael_mic_failure(hapd, addr, 0);
747 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
748 wpa_eapol_variable var, int value)
750 struct hostapd_data *hapd = ctx;
751 struct sta_info *sta = ap_get_sta(hapd, addr);
755 case WPA_EAPOL_portEnabled:
756 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
758 case WPA_EAPOL_portValid:
759 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
761 case WPA_EAPOL_authorized:
762 ieee802_1x_set_sta_authorized(hapd, sta, value);
764 case WPA_EAPOL_portControl_Auto:
766 sta->eapol_sm->portControl = Auto;
768 case WPA_EAPOL_keyRun:
770 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
772 case WPA_EAPOL_keyAvailable:
774 sta->eapol_sm->eap_if->eapKeyAvailable =
775 value ? TRUE : FALSE;
777 case WPA_EAPOL_keyDone:
779 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
781 case WPA_EAPOL_inc_EapolFramesTx:
783 sta->eapol_sm->dot1xAuthEapolFramesTx++;
789 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
790 wpa_eapol_variable var)
792 struct hostapd_data *hapd = ctx;
793 struct sta_info *sta = ap_get_sta(hapd, addr);
794 if (sta == NULL || sta->eapol_sm == NULL)
797 case WPA_EAPOL_keyRun:
798 return sta->eapol_sm->keyRun;
799 case WPA_EAPOL_keyAvailable:
800 return sta->eapol_sm->eap_if->eapKeyAvailable;
807 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
810 struct hostapd_data *hapd = ctx;
811 return hostapd_get_psk(hapd->conf, addr, prev_psk);
815 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
818 struct hostapd_data *hapd = ctx;
821 struct sta_info *sta;
823 sta = ap_get_sta(hapd, addr);
827 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
833 os_memcpy(msk, key, keylen);
840 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
841 const u8 *addr, int idx, u8 *key,
844 struct hostapd_data *hapd = ctx;
845 const char *ifname = hapd->conf->iface;
848 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
853 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
858 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
861 struct hostapd_data *hapd = ctx;
862 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
866 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
869 struct hostapd_data *hapd = ctx;
870 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
875 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
876 const u8 *data, size_t data_len,
879 struct hostapd_data *hapd = ctx;
880 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
884 static int hostapd_wpa_auth_for_each_sta(
885 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
888 struct hostapd_data *hapd = ctx;
889 struct sta_info *sta;
891 for (sta = hapd->sta_list; sta; sta = sta->next) {
892 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
899 static int hostapd_wpa_auth_for_each_auth(
900 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
903 struct hostapd_data *ohapd;
905 struct hapd_interfaces *interfaces = eloop_get_user_data();
907 for (i = 0; i < interfaces->count; i++) {
908 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
909 ohapd = interfaces->iface[i]->bss[j];
910 if (cb(ohapd->wpa_auth, cb_ctx))
919 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
920 const u8 *data, size_t data_len)
922 struct hostapd_data *hapd = ctx;
924 if (hapd->driver && hapd->driver->send_ether)
925 return hapd->driver->send_ether(hapd->drv_priv, dst,
926 hapd->own_addr, proto,
928 if (hapd->l2 == NULL)
930 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
934 #ifdef CONFIG_IEEE80211R
936 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
937 const u8 *data, size_t data_len)
939 struct hostapd_data *hapd = ctx;
941 struct ieee80211_mgmt *m;
943 struct sta_info *sta;
945 sta = ap_get_sta(hapd, dst);
946 if (sta == NULL || sta->wpa_sm == NULL)
949 m = os_zalloc(sizeof(*m) + data_len);
952 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
953 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
954 WLAN_FC_STYPE_ACTION);
955 os_memcpy(m->da, dst, ETH_ALEN);
956 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
957 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
958 os_memcpy(&m->u, data, data_len);
960 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
966 static struct wpa_state_machine *
967 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
969 struct hostapd_data *hapd = ctx;
970 struct sta_info *sta;
972 sta = ap_sta_add(hapd, sta_addr);
978 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
979 if (sta->wpa_sm == NULL) {
980 ap_free_sta(hapd, sta);
983 sta->auth_alg = WLAN_AUTH_FT;
989 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
992 struct hostapd_data *hapd = ctx;
993 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
996 #endif /* CONFIG_IEEE80211R */
1000 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1001 * @iface: Pointer to interface data
1002 * Returns: 0 on success, -1 on failure
1004 * This function is used to validate that the configured BSSIDs are valid.
1006 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1008 u8 mask[ETH_ALEN] = { 0 };
1009 struct hostapd_data *hapd = iface->bss[0];
1010 unsigned int i = iface->conf->num_bss, bits = 0, j;
1013 if (hostapd_drv_none(hapd))
1016 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1018 /* Determine the bits necessary to cover the number of BSSIDs. */
1019 for (i--; i; i >>= 1)
1022 /* Determine the bits necessary to any configured BSSIDs,
1023 if they are higher than the number of BSSIDs. */
1024 for (j = 0; j < iface->conf->num_bss; j++) {
1025 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1028 for (i = 0; i < ETH_ALEN; i++) {
1030 iface->conf->bss[j].bssid[i] ^
1035 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1041 while (mask[i] != 0) {
1053 os_memset(mask, 0xff, ETH_ALEN);
1055 for (i = 5; i > 5 - j; i--)
1061 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1062 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1064 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1069 printf("Driver did not accept BSSID mask " MACSTR " for start "
1070 "address " MACSTR ".\n",
1071 MAC2STR(mask), MAC2STR(hapd->own_addr));
1075 for (i = 0; i < ETH_ALEN; i++) {
1076 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1077 printf("Invalid BSSID mask " MACSTR " for start "
1078 "address " MACSTR ".\n"
1079 "Start address must be the first address in the"
1080 " block (i.e., addr AND mask == addr).\n",
1081 MAC2STR(mask), MAC2STR(hapd->own_addr));
1090 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1094 for (i = 0; i < conf->num_bss; i++) {
1095 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1104 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1106 struct wpa_auth_config _conf;
1107 struct wpa_auth_callbacks cb;
1111 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1112 os_memset(&cb, 0, sizeof(cb));
1114 cb.logger = hostapd_wpa_auth_logger;
1115 cb.disconnect = hostapd_wpa_auth_disconnect;
1116 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1117 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1118 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1119 cb.get_psk = hostapd_wpa_auth_get_psk;
1120 cb.get_msk = hostapd_wpa_auth_get_msk;
1121 cb.set_key = hostapd_wpa_auth_set_key;
1122 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1123 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1124 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1125 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1126 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1127 cb.send_ether = hostapd_wpa_auth_send_ether;
1128 #ifdef CONFIG_IEEE80211R
1129 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1130 cb.add_sta = hostapd_wpa_auth_add_sta;
1131 #endif /* CONFIG_IEEE80211R */
1132 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1133 if (hapd->wpa_auth == NULL) {
1134 printf("WPA initialization failed.\n");
1138 if (hostapd_set_privacy(hapd, 1)) {
1139 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1140 "for interface %s", hapd->conf->iface);
1144 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1145 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1146 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1147 "the kernel driver.");
1151 if (rsn_preauth_iface_init(hapd)) {
1152 printf("Initialization of RSN pre-authentication "
1162 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1163 struct hostapd_bss_config *conf)
1165 struct radius_server_conf srv;
1166 os_memset(&srv, 0, sizeof(srv));
1167 srv.client_file = conf->radius_server_clients;
1168 srv.auth_port = conf->radius_server_auth_port;
1169 srv.conf_ctx = conf;
1170 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1171 srv.ssl_ctx = hapd->ssl_ctx;
1172 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1173 srv.eap_fast_a_id = conf->eap_fast_a_id;
1174 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1175 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1176 srv.eap_fast_prov = conf->eap_fast_prov;
1177 srv.pac_key_lifetime = conf->pac_key_lifetime;
1178 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1179 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1180 srv.tnc = conf->tnc;
1181 srv.ipv6 = conf->radius_server_ipv6;
1182 srv.get_eap_user = hostapd_radius_get_eap_user;
1184 hapd->radius_srv = radius_server_init(&srv);
1185 if (hapd->radius_srv == NULL) {
1186 printf("RADIUS server initialization failed.\n");
1195 * hostapd_setup_bss - Per-BSS setup (initialization)
1196 * @hapd: Pointer to BSS data
1197 * @first: Whether this BSS is the first BSS of an interface
1199 * This function is used to initialize all per-BSS data structures and
1200 * resources. This gets called in a loop for each BSS when an interface is
1201 * initialized. Most of the modules that are initialized here will be
1202 * deinitialized in hostapd_cleanup().
1204 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1206 struct hostapd_bss_config *conf = hapd->conf;
1207 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1208 int ssid_len, set_ssid;
1211 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1212 /* Allocate the next available BSSID. */
1214 inc_byte_array(hapd->own_addr, ETH_ALEN);
1215 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1217 /* Allocate the configured BSSID. */
1218 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1220 if (hostapd_mac_comp(hapd->own_addr,
1221 hapd->iface->bss[0]->own_addr) ==
1223 printf("BSS '%s' may not have BSSID "
1224 "set to the MAC address of the radio\n",
1230 hapd->interface_added = 1;
1231 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1233 printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1234 MAC2STR(hapd->own_addr));
1240 * Fetch the SSID from the system and use it or,
1241 * if one was specified in the config file, verify they
1244 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1246 printf("Could not read SSID from system\n");
1249 if (conf->ssid.ssid_set) {
1251 * If SSID is specified in the config file and it differs
1252 * from what is being used then force installation of the
1255 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1256 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1259 * No SSID in the config file; just use the one we got
1263 conf->ssid.ssid_len = ssid_len;
1264 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1265 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1268 if (!hostapd_drv_none(hapd)) {
1269 printf("Using interface %s with hwaddr " MACSTR
1271 hapd->conf->iface, MAC2STR(hapd->own_addr),
1272 hapd->conf->ssid.ssid);
1275 if (hostapd_setup_wpa_psk(conf)) {
1276 printf("WPA-PSK setup failed.\n");
1280 /* Set flag for whether SSID is broadcast in beacons */
1281 if (hostapd_set_broadcast_ssid(hapd,
1282 !!hapd->conf->ignore_broadcast_ssid)) {
1283 printf("Could not set broadcast SSID flag for kernel "
1288 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1289 printf("Could not set DTIM period for kernel driver\n");
1293 /* Set SSID for the kernel driver (to be used in beacon and probe
1294 * response frames) */
1295 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1296 conf->ssid.ssid_len)) {
1297 printf("Could not set SSID for kernel driver\n");
1301 if (wpa_debug_level == MSG_MSGDUMP)
1302 conf->radius->msg_dumps = 1;
1303 hapd->radius = radius_client_init(hapd, conf->radius);
1304 if (hapd->radius == NULL) {
1305 printf("RADIUS client initialization failed.\n");
1309 if (hostapd_acl_init(hapd)) {
1310 printf("ACL initialization failed.\n");
1314 if (ieee802_1x_init(hapd)) {
1315 printf("IEEE 802.1X initialization failed.\n");
1319 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1322 if (accounting_init(hapd)) {
1323 printf("Accounting initialization failed.\n");
1327 if (hapd->conf->ieee802_11f &&
1328 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1329 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1333 if (hostapd_ctrl_iface_init(hapd)) {
1334 printf("Failed to setup control interface\n");
1338 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1339 printf("VLAN initialization failed.\n");
1343 #ifdef CONFIG_IEEE80211R
1344 if (!hostapd_drv_none(hapd)) {
1345 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1346 hostapd_rrb_receive, hapd, 0);
1347 if (hapd->l2 == NULL &&
1348 (hapd->driver == NULL ||
1349 hapd->driver->send_ether == NULL)) {
1350 printf("Failed to open l2_packet interface\n");
1354 #endif /* CONFIG_IEEE80211R */
1356 ieee802_11_set_beacon(hapd);
1358 if (conf->radius_server_clients &&
1359 hostapd_setup_radius_srv(hapd, conf))
1366 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1368 struct hostapd_data *hapd = iface->bss[0];
1370 struct hostapd_tx_queue_params *p;
1372 for (i = 0; i < NUM_TX_QUEUES; i++) {
1373 p = &iface->conf->tx_queue[i];
1378 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1379 p->cwmax, p->burst)) {
1380 printf("Failed to set TX queue parameters for queue %d"
1382 /* Continue anyway */
1388 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1389 size_t identity_len, int phase2,
1390 struct eap_user *user)
1392 const struct hostapd_eap_user *eap_user;
1395 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1396 if (eap_user == NULL)
1402 os_memset(user, 0, sizeof(*user));
1403 count = EAP_USER_MAX_METHODS;
1404 if (count > EAP_MAX_METHODS)
1405 count = EAP_MAX_METHODS;
1406 for (i = 0; i < count; i++) {
1407 user->methods[i].vendor = eap_user->methods[i].vendor;
1408 user->methods[i].method = eap_user->methods[i].method;
1411 if (eap_user->password) {
1412 user->password = os_malloc(eap_user->password_len);
1413 if (user->password == NULL)
1415 os_memcpy(user->password, eap_user->password,
1416 eap_user->password_len);
1417 user->password_len = eap_user->password_len;
1418 user->password_hash = eap_user->password_hash;
1420 user->force_version = eap_user->force_version;
1421 user->ttls_auth = eap_user->ttls_auth;
1427 static int setup_interface(struct hostapd_iface *iface)
1429 struct hostapd_data *hapd = iface->bss[0];
1430 struct hostapd_bss_config *conf = hapd->conf;
1433 u8 *b = conf->bssid;
1440 * Initialize the driver interface and make sure that all BSSes get
1441 * configured with a pointer to this driver interface.
1443 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1444 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1446 hapd->drv_priv = hostapd_driver_init(hapd);
1449 if (hapd->drv_priv == NULL) {
1450 printf("%s driver initialization failed.\n",
1451 hapd->driver ? hapd->driver->name : "Unknown");
1452 hapd->driver = NULL;
1455 for (i = 0; i < iface->num_bss; i++) {
1456 iface->bss[i]->driver = hapd->driver;
1457 iface->bss[i]->drv_priv = hapd->drv_priv;
1460 if (hostapd_validate_bssid_configuration(iface))
1463 #ifdef CONFIG_IEEE80211N
1464 SET_2BIT_LE16(&iface->ht_op_mode,
1465 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1467 #endif /* CONFIG_IEEE80211N */
1469 os_memcpy(country, hapd->iconf->country, 3);
1471 if (hostapd_set_country(hapd, country) < 0) {
1472 printf("Failed to set country code\n");
1476 if (hapd->iconf->ieee80211d &&
1477 hostapd_set_ieee80211d(hapd, 1) < 0) {
1478 printf("Failed to set ieee80211d (%d)\n",
1479 hapd->iconf->ieee80211d);
1483 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1484 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1485 printf("Failed to set bridge_packets for kernel driver\n");
1489 /* TODO: merge with hostapd_driver_init() ? */
1490 if (hostapd_wireless_event_init(hapd) < 0)
1493 if (hostapd_get_hw_features(iface)) {
1494 /* Not all drivers support this yet, so continue without hw
1497 int ret = hostapd_select_hw_mode(iface);
1499 printf("Could not select hw_mode and channel. (%d)\n",
1505 hostapd_flush_old_stations(hapd);
1506 hostapd_set_privacy(hapd, 0);
1508 if (hapd->iconf->channel) {
1509 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1510 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1511 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1512 hapd->iconf->channel, freq);
1514 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq)) {
1515 printf("Could not set channel for kernel driver\n");
1520 hostapd_broadcast_wep_clear(hapd);
1521 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1524 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1525 ieee802_11_set_beacon(hapd);
1527 if (hapd->iconf->rts_threshold > -1 &&
1528 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1529 printf("Could not set RTS threshold for kernel driver\n");
1533 if (hapd->iconf->fragm_threshold > -1 &&
1534 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1535 printf("Could not set fragmentation threshold for kernel "
1540 prev_addr = hapd->own_addr;
1542 for (j = 0; j < iface->num_bss; j++) {
1543 hapd = iface->bss[j];
1545 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1546 if (hostapd_setup_bss(hapd, j == 0))
1548 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1549 prev_addr = hapd->own_addr;
1552 hostapd_tx_queue_params(iface);
1554 ap_list_init(iface);
1556 if (hostapd_driver_commit(hapd) < 0) {
1557 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1558 "configuration", __func__);
1567 * hostapd_setup_interface - Setup of an interface
1568 * @iface: Pointer to interface data.
1569 * Returns: 0 on success, -1 on failure
1571 * Initializes the driver interface, validates the configuration,
1572 * and sets driver parameters based on the configuration.
1573 * Flushes old stations, sets the channel, encryption,
1574 * beacons, and WDS links based on the configuration.
1576 static int hostapd_setup_interface(struct hostapd_iface *iface)
1580 ret = setup_interface(iface);
1582 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1583 iface->bss[0]->conf->iface);
1586 } else if (!hostapd_drv_none(iface->bss[0])) {
1587 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1588 iface->bss[0]->conf->iface);
1595 static void show_version(void)
1598 "hostapd v" VERSION_STR "\n"
1599 "User space daemon for IEEE 802.11 AP management,\n"
1600 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1601 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1602 "and contributors\n");
1606 static void usage(void)
1611 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1612 "<configuration file(s)>\n"
1615 " -h show this usage\n"
1616 " -d show more debug messages (-dd for even more)\n"
1617 " -B run daemon in the background\n"
1619 " -K include key data in debug messages\n"
1620 " -t include timestamps in some debug messages\n"
1621 " -v show hostapd version\n");
1628 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1629 * @hapd_iface: Pointer to interface data
1630 * @conf: Pointer to per-interface configuration
1631 * @bss: Pointer to per-BSS configuration for this BSS
1632 * Returns: Pointer to allocated BSS data
1634 * This function is used to allocate per-BSS data structure. This data will be
1635 * freed after hostapd_cleanup() is called for it during interface
1638 static struct hostapd_data *
1639 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1640 struct hostapd_config *conf,
1641 struct hostapd_bss_config *bss)
1643 struct hostapd_data *hapd;
1645 hapd = os_zalloc(sizeof(*hapd));
1651 hapd->iface = hapd_iface;
1653 if (hapd->conf->individual_wep_key_len > 0) {
1654 /* use key0 in individual key and key1 in broadcast key */
1655 hapd->default_wep_key_idx = 1;
1658 #ifdef EAP_TLS_FUNCS
1659 if (hapd->conf->eap_server &&
1660 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1661 hapd->conf->dh_file)) {
1662 struct tls_connection_params params;
1664 hapd->ssl_ctx = tls_init(NULL);
1665 if (hapd->ssl_ctx == NULL) {
1666 printf("Failed to initialize TLS\n");
1670 os_memset(¶ms, 0, sizeof(params));
1671 params.ca_cert = hapd->conf->ca_cert;
1672 params.client_cert = hapd->conf->server_cert;
1673 params.private_key = hapd->conf->private_key;
1674 params.private_key_passwd = hapd->conf->private_key_passwd;
1675 params.dh_file = hapd->conf->dh_file;
1677 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1678 printf("Failed to set TLS parameters\n");
1682 if (tls_global_set_verify(hapd->ssl_ctx,
1683 hapd->conf->check_crl)) {
1684 printf("Failed to enable check_crl\n");
1688 #endif /* EAP_TLS_FUNCS */
1691 if (hapd->conf->eap_sim_db) {
1692 hapd->eap_sim_db_priv =
1693 eap_sim_db_init(hapd->conf->eap_sim_db,
1694 hostapd_sim_db_cb, hapd);
1695 if (hapd->eap_sim_db_priv == NULL) {
1696 printf("Failed to initialize EAP-SIM database "
1701 #endif /* EAP_SERVER */
1703 hapd->driver = hapd->iconf->driver;
1707 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1710 /* TODO: cleanup allocated resources(?) */
1717 * hostapd_init - Allocate and initialize per-interface data
1718 * @config_file: Path to the configuration file
1719 * Returns: Pointer to the allocated interface data or %NULL on failure
1721 * This function is used to allocate main data structures for per-interface
1722 * data. The allocated data buffer will be freed by calling
1723 * hostapd_cleanup_iface().
1725 static struct hostapd_iface * hostapd_init(const char *config_file)
1727 struct hostapd_iface *hapd_iface = NULL;
1728 struct hostapd_config *conf = NULL;
1729 struct hostapd_data *hapd;
1732 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1733 if (hapd_iface == NULL)
1736 hapd_iface->config_fname = os_strdup(config_file);
1737 if (hapd_iface->config_fname == NULL)
1740 conf = hostapd_config_read(hapd_iface->config_fname);
1743 hapd_iface->conf = conf;
1745 hapd_iface->num_bss = conf->num_bss;
1746 hapd_iface->bss = os_zalloc(conf->num_bss *
1747 sizeof(struct hostapd_data *));
1748 if (hapd_iface->bss == NULL)
1751 for (i = 0; i < conf->num_bss; i++) {
1752 hapd = hapd_iface->bss[i] =
1753 hostapd_alloc_bss_data(hapd_iface, conf,
1763 hostapd_config_free(conf);
1765 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1766 hapd = hapd_iface->bss[i];
1767 if (hapd && hapd->ssl_ctx)
1768 tls_deinit(hapd->ssl_ctx);
1771 os_free(hapd_iface->config_fname);
1772 os_free(hapd_iface->bss);
1773 os_free(hapd_iface);
1779 int main(int argc, char *argv[])
1781 struct hapd_interfaces interfaces;
1784 int c, debug = 0, daemonize = 0, tnc = 0;
1785 const char *pid_file = NULL;
1787 hostapd_logger_register_cb(hostapd_logger_cb);
1790 c = getopt(argc, argv, "BdhKP:tv");
1799 if (wpa_debug_level > 0)
1806 wpa_debug_show_keys++;
1812 wpa_debug_timestamp++;
1828 if (eap_server_register_methods()) {
1829 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1833 interfaces.count = argc - optind;
1835 interfaces.iface = os_malloc(interfaces.count *
1836 sizeof(struct hostapd_iface *));
1837 if (interfaces.iface == NULL) {
1838 wpa_printf(MSG_ERROR, "malloc failed\n");
1842 if (eloop_init(&interfaces)) {
1843 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1847 #ifndef CONFIG_NATIVE_WINDOWS
1848 eloop_register_signal(SIGHUP, handle_reload, NULL);
1849 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1850 #endif /* CONFIG_NATIVE_WINDOWS */
1851 eloop_register_signal_terminate(handle_term, NULL);
1853 /* Initialize interfaces */
1854 for (i = 0; i < interfaces.count; i++) {
1855 printf("Configuration file: %s\n", argv[optind + i]);
1856 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1857 if (!interfaces.iface[i])
1859 for (k = 0; k < debug; k++) {
1860 if (interfaces.iface[i]->bss[0]->conf->
1861 logger_stdout_level > 0)
1862 interfaces.iface[i]->bss[0]->conf->
1863 logger_stdout_level--;
1866 ret = hostapd_setup_interface(interfaces.iface[i]);
1870 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1871 if (interfaces.iface[i]->bss[0]->conf->tnc)
1877 if (tnc && tncs_global_init() < 0) {
1878 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1881 #endif /* EAP_TNC */
1883 if (daemonize && os_daemonize(pid_file)) {
1888 #ifndef CONFIG_NATIVE_WINDOWS
1889 openlog("hostapd", 0, LOG_DAEMON);
1890 #endif /* CONFIG_NATIVE_WINDOWS */
1894 /* Disconnect associated stations from all interfaces and BSSes */
1895 for (i = 0; i < interfaces.count; i++) {
1896 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1897 struct hostapd_data *hapd =
1898 interfaces.iface[i]->bss[j];
1899 hostapd_free_stas(hapd);
1900 hostapd_flush_old_stations(hapd);
1907 /* Deinitialize all interfaces */
1908 for (i = 0; i < interfaces.count; i++) {
1909 if (!interfaces.iface[i])
1911 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1912 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1913 struct hostapd_data *hapd =
1914 interfaces.iface[i]->bss[j];
1915 hostapd_cleanup(hapd);
1916 if (j == interfaces.iface[i]->num_bss - 1 &&
1918 hostapd_driver_deinit(hapd);
1920 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1921 os_free(interfaces.iface[i]->bss[j]);
1922 hostapd_cleanup_iface(interfaces.iface[i]);
1924 os_free(interfaces.iface);
1927 tncs_global_deinit();
1928 #endif /* EAP_TNC */
1932 #ifndef CONFIG_NATIVE_WINDOWS
1934 #endif /* CONFIG_NATIVE_WINDOWS */
1936 eap_server_unregister_methods();
1938 os_daemonize_terminate(pid_file);