2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
25 #include "hw_features.h"
26 #include "accounting.h"
30 #include "ieee802_11_auth.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
36 #include "radius/radius.h"
40 #include "vlan_init.h"
41 #include "ctrl_iface.h"
43 #include "eap_server/eap_sim_db.h"
44 #include "eap_server/eap.h"
45 #include "eap_server/tncs.h"
47 #include "l2_packet/l2_packet.h"
48 #include "wps_hostapd.h"
51 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
52 size_t identity_len, int phase2,
53 struct eap_user *user);
54 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
55 static int hostapd_setup_wpa(struct hostapd_data *hapd);
56 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
58 struct hapd_interfaces {
60 struct hostapd_iface **iface;
64 extern int wpa_debug_level;
65 extern int wpa_debug_show_keys;
66 extern int wpa_debug_timestamp;
69 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
70 int level, const char *txt, size_t len)
72 struct hostapd_data *hapd = ctx;
73 char *format, *module_str;
75 int conf_syslog_level, conf_stdout_level;
76 unsigned int conf_syslog, conf_stdout;
79 format = os_malloc(maxlen);
83 if (hapd && hapd->conf) {
84 conf_syslog_level = hapd->conf->logger_syslog_level;
85 conf_stdout_level = hapd->conf->logger_stdout_level;
86 conf_syslog = hapd->conf->logger_syslog;
87 conf_stdout = hapd->conf->logger_stdout;
89 conf_syslog_level = conf_stdout_level = 0;
90 conf_syslog = conf_stdout = (unsigned int) -1;
94 case HOSTAPD_MODULE_IEEE80211:
95 module_str = "IEEE 802.11";
97 case HOSTAPD_MODULE_IEEE8021X:
98 module_str = "IEEE 802.1X";
100 case HOSTAPD_MODULE_RADIUS:
101 module_str = "RADIUS";
103 case HOSTAPD_MODULE_WPA:
106 case HOSTAPD_MODULE_DRIVER:
107 module_str = "DRIVER";
109 case HOSTAPD_MODULE_IAPP:
112 case HOSTAPD_MODULE_MLME:
120 if (hapd && hapd->conf && addr)
121 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
122 hapd->conf->iface, MAC2STR(addr),
123 module_str ? " " : "", module_str, txt);
124 else if (hapd && hapd->conf)
125 os_snprintf(format, maxlen, "%s:%s%s %s",
126 hapd->conf->iface, module_str ? " " : "",
129 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
130 MAC2STR(addr), module_str ? " " : "",
133 os_snprintf(format, maxlen, "%s%s%s",
134 module_str, module_str ? ": " : "", txt);
136 if ((conf_stdout & module) && level >= conf_stdout_level) {
137 wpa_debug_print_timestamp();
138 printf("%s\n", format);
141 #ifndef CONFIG_NATIVE_WINDOWS
142 if ((conf_syslog & module) && level >= conf_syslog_level) {
145 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
146 case HOSTAPD_LEVEL_DEBUG:
147 priority = LOG_DEBUG;
149 case HOSTAPD_LEVEL_INFO:
152 case HOSTAPD_LEVEL_NOTICE:
153 priority = LOG_NOTICE;
155 case HOSTAPD_LEVEL_WARNING:
156 priority = LOG_WARNING;
162 syslog(priority, "%s", format);
164 #endif /* CONFIG_NATIVE_WINDOWS */
170 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
174 /* New Prism2.5/3 STA firmware versions seem to have issues with this
175 * broadcast deauth frame. This gets the firmware in odd state where
176 * nothing works correctly, so let's skip sending this for the hostap
179 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
180 os_memset(addr, 0xff, ETH_ALEN);
181 hostapd_sta_deauth(hapd, addr,
182 WLAN_REASON_PREV_AUTH_NOT_VALID);
188 * hostapd_prune_associations - Remove extraneous associations
189 * @hapd: Pointer to BSS data for the most recent association
190 * @sta: Pointer to the associated STA data
192 * This function looks through all radios and BSS's for previous
193 * (stale) associations of STA. If any are found they are removed.
195 static void hostapd_prune_associations(struct hostapd_data *hapd,
196 struct sta_info *sta)
198 struct sta_info *osta;
199 struct hostapd_data *ohapd;
201 struct hapd_interfaces *interfaces = eloop_get_user_data();
203 for (i = 0; i < interfaces->count; i++) {
204 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
205 ohapd = interfaces->iface[i]->bss[j];
208 osta = ap_get_sta(ohapd, sta->addr);
212 ap_sta_disassociate(ohapd, osta,
213 WLAN_REASON_UNSPECIFIED);
220 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
221 * @hapd: Pointer to BSS data
222 * @sta: Pointer to the associated STA data
223 * @reassoc: 1 to indicate this was a re-association; 0 = first association
225 * This function will be called whenever a station associates with the AP. It
226 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
227 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
228 * authentication and association) internally.
230 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
233 if (hapd->tkip_countermeasures) {
234 hostapd_sta_deauth(hapd, sta->addr,
235 WLAN_REASON_MICHAEL_MIC_FAILURE);
239 hostapd_prune_associations(hapd, sta);
241 /* IEEE 802.11F (IAPP) */
242 if (hapd->conf->ieee802_11f)
243 iapp_new_station(hapd->iapp, sta);
245 /* Start accounting here, if IEEE 802.1X and WPA are not used.
246 * IEEE 802.1X/WPA code will start accounting after the station has
247 * been authorized. */
248 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
249 accounting_sta_start(hapd, sta);
251 hostapd_wme_sta_config(hapd, sta);
253 /* Start IEEE 802.1X authentication process for new stations */
254 ieee802_1x_new_station(hapd, sta);
256 if (sta->auth_alg != WLAN_AUTH_FT &&
257 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
258 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
260 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
264 void hostapd_tx_status(struct hostapd_data *hapd, const u8 *addr,
265 const u8 *buf, size_t len, int ack)
267 struct sta_info *sta;
269 sta = ap_get_sta(hapd, addr);
270 if (sta && sta->flags & WLAN_STA_PENDING_POLL) {
271 wpa_printf(MSG_DEBUG, "STA " MACSTR " %s pending "
272 "activity poll", MAC2STR(sta->addr),
273 ack ? "ACKed" : "did not ACK");
275 sta->flags &= ~WLAN_STA_PENDING_POLL;
278 ieee802_1x_tx_status(hapd, sta, buf, len, ack);
282 void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, const u8 *addr)
284 struct sta_info *sta;
286 sta = ap_get_sta(hapd, addr);
287 if (!sta || !(sta->flags & WLAN_STA_ASSOC)) {
288 wpa_printf(MSG_DEBUG, "Data/PS-poll frame from not associated "
289 "STA " MACSTR, MAC2STR(addr));
290 if (sta && (sta->flags & WLAN_STA_AUTH))
291 hostapd_sta_disassoc(
293 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
297 WLAN_REASON_CLASS3_FRAME_FROM_NONASSOC_STA);
302 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
303 const u8 *ie, size_t ielen)
305 struct sta_info *sta;
308 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
309 HOSTAPD_LEVEL_INFO, "associated");
311 sta = ap_get_sta(hapd, addr);
313 accounting_sta_stop(hapd, sta);
315 sta = ap_sta_add(hapd, addr);
319 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS);
321 if (hapd->conf->wpa) {
322 if (ie == NULL || ielen == 0) {
323 if (hapd->conf->wps_state) {
324 wpa_printf(MSG_DEBUG, "STA did not include "
325 "WPA/RSN IE in (Re)Association "
326 "Request - possible WPS use");
327 sta->flags |= WLAN_STA_MAYBE_WPS;
331 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
334 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
335 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
336 sta->flags |= WLAN_STA_WPS;
340 if (sta->wpa_sm == NULL)
341 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
343 if (sta->wpa_sm == NULL) {
344 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
348 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
350 if (res != WPA_IE_OK) {
351 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
352 "rejected? (res %u)", res);
353 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
359 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
360 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
361 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
363 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
365 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
371 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
373 struct sta_info *sta;
375 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
376 HOSTAPD_LEVEL_INFO, "disassociated");
378 sta = ap_get_sta(hapd, addr);
380 wpa_printf(MSG_DEBUG, "Disassociation notification for "
381 "unknown STA " MACSTR, MAC2STR(addr));
385 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
386 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
387 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
388 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
389 ap_free_sta(hapd, sta);
393 void hostapd_eapol_receive(struct hostapd_data *hapd, const u8 *sa,
394 const u8 *buf, size_t len)
396 ieee802_1x_receive(hapd, sa, buf, len);
401 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
402 struct sta_info *sta, void *ctx)
404 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
410 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
412 struct hostapd_data *hapd = ctx;
413 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
414 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
416 #endif /* EAP_SERVER */
420 * handle_term - SIGINT and SIGTERM handler to terminate hostapd process
422 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
424 wpa_printf(MSG_DEBUG, "Signal %d received - terminating", sig);
429 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
430 struct wpa_auth_config *wconf)
432 wconf->wpa = conf->wpa;
433 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
434 wconf->wpa_pairwise = conf->wpa_pairwise;
435 wconf->wpa_group = conf->wpa_group;
436 wconf->wpa_group_rekey = conf->wpa_group_rekey;
437 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
438 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
439 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
440 wconf->rsn_pairwise = conf->rsn_pairwise;
441 wconf->rsn_preauth = conf->rsn_preauth;
442 wconf->eapol_version = conf->eapol_version;
443 wconf->peerkey = conf->peerkey;
444 wconf->wme_enabled = conf->wme_enabled;
445 wconf->okc = conf->okc;
446 #ifdef CONFIG_IEEE80211W
447 wconf->ieee80211w = conf->ieee80211w;
448 #endif /* CONFIG_IEEE80211W */
449 #ifdef CONFIG_IEEE80211R
450 wconf->ssid_len = conf->ssid.ssid_len;
451 if (wconf->ssid_len > SSID_LEN)
452 wconf->ssid_len = SSID_LEN;
453 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
454 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
455 MOBILITY_DOMAIN_ID_LEN);
456 if (conf->nas_identifier &&
457 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
458 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
459 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
460 wconf->r0_key_holder_len);
462 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
463 wconf->r0_key_lifetime = conf->r0_key_lifetime;
464 wconf->reassociation_deadline = conf->reassociation_deadline;
465 wconf->r0kh_list = conf->r0kh_list;
466 wconf->r1kh_list = conf->r1kh_list;
467 wconf->pmk_r1_push = conf->pmk_r1_push;
468 #endif /* CONFIG_IEEE80211R */
472 int hostapd_reload_config(struct hostapd_iface *iface)
474 struct hostapd_data *hapd = iface->bss[0];
475 struct hostapd_config *newconf, *oldconf;
476 struct wpa_auth_config wpa_auth_conf;
478 newconf = hostapd_config_read(iface->config_fname);
483 * Deauthenticate all stations since the new configuration may not
484 * allow them to use the BSS anymore.
486 hostapd_flush_old_stations(hapd);
488 /* TODO: update dynamic data based on changed configuration
489 * items (e.g., open/close sockets, etc.) */
490 radius_client_flush(hapd->radius, 0);
492 oldconf = hapd->iconf;
493 hapd->iconf = newconf;
494 hapd->conf = &newconf->bss[0];
495 iface->conf = newconf;
497 if (hostapd_setup_wpa_psk(hapd->conf)) {
498 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
499 "after reloading configuration");
502 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
503 hostapd_setup_wpa(hapd);
504 else if (hapd->conf->wpa) {
505 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
506 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
507 } else if (hapd->wpa_auth) {
508 wpa_deinit(hapd->wpa_auth);
509 hapd->wpa_auth = NULL;
510 hostapd_set_privacy(hapd, 0);
511 hostapd_setup_encryption(hapd->conf->iface, hapd);
514 ieee802_11_set_beacon(hapd);
516 hostapd_config_free(oldconf);
518 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
524 #ifndef CONFIG_NATIVE_WINDOWS
526 * handle_reload - SIGHUP handler to reload configuration
528 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
530 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
533 wpa_printf(MSG_DEBUG, "Signal %d received - reloading configuration",
536 for (i = 0; i < hapds->count; i++) {
537 if (hostapd_reload_config(hapds->iface[i]) < 0) {
538 wpa_printf(MSG_WARNING, "Failed to read new "
539 "configuration file - continuing with "
547 #ifdef HOSTAPD_DUMP_STATE
549 * hostapd_dump_state - SIGUSR1 handler to dump hostapd state to a text file
551 static void hostapd_dump_state(struct hostapd_data *hapd)
555 struct sta_info *sta;
559 if (!hapd->conf->dump_log_name) {
560 wpa_printf(MSG_DEBUG, "Dump file not defined - ignoring dump "
565 wpa_printf(MSG_DEBUG, "Dumping hostapd state to '%s'",
566 hapd->conf->dump_log_name);
567 f = fopen(hapd->conf->dump_log_name, "w");
569 wpa_printf(MSG_WARNING, "Could not open dump file '%s' for "
570 "writing.", hapd->conf->dump_log_name);
575 fprintf(f, "hostapd state dump - %s", ctime(&now));
576 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
577 "num_sta_no_short_slot_time=%d\n"
578 "num_sta_no_short_preamble=%d\n",
579 hapd->num_sta, hapd->iface->num_sta_non_erp,
580 hapd->iface->num_sta_no_short_slot_time,
581 hapd->iface->num_sta_no_short_preamble);
583 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
584 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
587 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
588 " capability=0x%x listen_interval=%d\n",
591 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
592 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
593 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
594 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
595 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
596 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
598 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
600 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
601 "[SHORT_PREAMBLE]" : ""),
602 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
603 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
604 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
605 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
606 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
607 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
609 sta->listen_interval);
611 fprintf(f, " supported_rates=");
612 for (i = 0; i < sta->supported_rates_len; i++)
613 fprintf(f, "%02x ", sta->supported_rates[i]);
617 " timeout_next=%s\n",
618 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
619 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
622 ieee802_1x_dump_state(f, " ", sta);
625 buf = os_malloc(4096);
627 int count = radius_client_get_mib(hapd->radius, buf, 4096);
630 else if (count > 4095)
633 fprintf(f, "%s", buf);
635 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
638 else if (count > 4095)
641 fprintf(f, "%s", buf);
646 #endif /* HOSTAPD_DUMP_STATE */
649 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
651 #ifdef HOSTAPD_DUMP_STATE
652 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
655 for (i = 0; i < hapds->count; i++) {
656 struct hostapd_iface *hapd_iface = hapds->iface[i];
657 for (j = 0; j < hapd_iface->num_bss; j++)
658 hostapd_dump_state(hapd_iface->bss[j]);
660 #endif /* HOSTAPD_DUMP_STATE */
662 #endif /* CONFIG_NATIVE_WINDOWS */
664 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
669 for (i = 0; i < NUM_WEP_KEYS; i++) {
670 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
671 0, i == 0 ? 1 : 0)) {
672 wpa_printf(MSG_DEBUG, "Failed to clear default "
673 "encryption keys (ifname=%s keyidx=%d)",
677 #ifdef CONFIG_IEEE80211W
678 if (hapd->conf->ieee80211w) {
679 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
680 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
683 wpa_printf(MSG_DEBUG, "Failed to clear "
684 "default mgmt encryption keys "
685 "(ifname=%s keyidx=%d)", ifname, i);
689 #endif /* CONFIG_IEEE80211W */
693 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
695 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
700 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
703 struct hostapd_ssid *ssid = &hapd->conf->ssid;
706 if (ssid->wep.default_len &&
707 hostapd_set_encryption(hapd->conf->iface,
708 hapd, "WEP", NULL, idx,
711 idx == ssid->wep.idx)) {
712 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
716 if (ssid->dyn_vlan_keys) {
718 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
720 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
723 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
729 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
733 wpa_printf(MSG_WARNING, "Could not set "
734 "dynamic VLAN WEP encryption.");
744 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
745 * @hapd: Pointer to BSS data
747 * This function is used to free all per-BSS data structures and resources.
748 * This gets called in a loop for each BSS between calls to
749 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
750 * is deinitialized. Most of the modules that are initialized in
751 * hostapd_setup_bss() are deinitialized here.
753 static void hostapd_cleanup(struct hostapd_data *hapd)
755 hostapd_ctrl_iface_deinit(hapd);
757 os_free(hapd->default_wep_key);
758 hapd->default_wep_key = NULL;
759 iapp_deinit(hapd->iapp);
761 accounting_deinit(hapd);
762 rsn_preauth_iface_deinit(hapd);
763 if (hapd->wpa_auth) {
764 wpa_deinit(hapd->wpa_auth);
765 hapd->wpa_auth = NULL;
767 if (hostapd_set_privacy(hapd, 0)) {
768 wpa_printf(MSG_DEBUG, "Could not disable "
769 "PrivacyInvoked for interface %s",
773 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
774 wpa_printf(MSG_DEBUG, "Could not remove generic "
775 "information element from interface %s",
779 ieee802_1x_deinit(hapd);
781 hostapd_acl_deinit(hapd);
782 radius_client_deinit(hapd->radius);
784 radius_server_deinit(hapd->radius_srv);
785 hapd->radius_srv = NULL;
787 #ifdef CONFIG_IEEE80211R
788 l2_packet_deinit(hapd->l2);
789 #endif /* CONFIG_IEEE80211R */
791 hostapd_deinit_wps(hapd);
793 hostapd_wireless_event_deinit(hapd);
797 tls_deinit(hapd->ssl_ctx);
798 hapd->ssl_ctx = NULL;
800 #endif /* EAP_TLS_FUNCS */
803 if (hapd->eap_sim_db_priv) {
804 eap_sim_db_deinit(hapd->eap_sim_db_priv);
805 hapd->eap_sim_db_priv = NULL;
807 #endif /* EAP_SERVER */
809 if (hapd->interface_added &&
810 hostapd_bss_remove(hapd, hapd->conf->iface)) {
811 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
818 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
819 * @iface: Pointer to interface data
821 * This function is called before per-BSS data structures are deinitialized
822 * with hostapd_cleanup().
824 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
830 * hostapd_cleanup_iface - Complete per-interface cleanup
831 * @iface: Pointer to interface data
833 * This function is called after per-BSS data structures are deinitialized
834 * with hostapd_cleanup().
836 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
838 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
839 iface->hw_features = NULL;
840 os_free(iface->current_rates);
841 iface->current_rates = NULL;
842 ap_list_deinit(iface);
843 hostapd_config_free(iface->conf);
846 os_free(iface->config_fname);
852 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
856 hostapd_broadcast_wep_set(hapd);
858 if (hapd->conf->ssid.wep.default_len)
861 for (i = 0; i < 4; i++) {
862 if (hapd->conf->ssid.wep.key[i] &&
863 hostapd_set_encryption(iface, hapd, "WEP", NULL,
864 i, hapd->conf->ssid.wep.key[i],
865 hapd->conf->ssid.wep.len[i],
866 i == hapd->conf->ssid.wep.idx)) {
867 wpa_printf(MSG_WARNING, "Could not set WEP "
871 if (hapd->conf->ssid.wep.key[i] &&
872 i == hapd->conf->ssid.wep.idx)
873 hostapd_set_privacy(hapd, 1);
880 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
884 if (hostapd_drv_none(hapd))
887 wpa_printf(MSG_DEBUG, "Flushing old station entries");
888 if (hostapd_flush(hapd)) {
889 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
892 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
893 hostapd_deauth_all_stas(hapd);
899 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
900 logger_level level, const char *txt)
902 struct hostapd_data *hapd = ctx;
907 hlevel = HOSTAPD_LEVEL_WARNING;
910 hlevel = HOSTAPD_LEVEL_INFO;
914 hlevel = HOSTAPD_LEVEL_DEBUG;
918 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
922 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
925 struct hostapd_data *hapd = ctx;
926 struct sta_info *sta;
928 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
929 "STA " MACSTR " reason %d",
930 __func__, MAC2STR(addr), reason);
932 sta = ap_get_sta(hapd, addr);
933 hostapd_sta_deauth(hapd, addr, reason);
936 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
937 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
938 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
939 sta->timeout_next = STA_REMOVE;
943 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
945 struct hostapd_data *hapd = ctx;
946 ieee80211_michael_mic_failure(hapd, addr, 0);
950 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
951 wpa_eapol_variable var, int value)
953 struct hostapd_data *hapd = ctx;
954 struct sta_info *sta = ap_get_sta(hapd, addr);
958 case WPA_EAPOL_portEnabled:
959 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
961 case WPA_EAPOL_portValid:
962 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
964 case WPA_EAPOL_authorized:
965 ieee802_1x_set_sta_authorized(hapd, sta, value);
967 case WPA_EAPOL_portControl_Auto:
969 sta->eapol_sm->portControl = Auto;
971 case WPA_EAPOL_keyRun:
973 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
975 case WPA_EAPOL_keyAvailable:
977 sta->eapol_sm->eap_if->eapKeyAvailable =
978 value ? TRUE : FALSE;
980 case WPA_EAPOL_keyDone:
982 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
984 case WPA_EAPOL_inc_EapolFramesTx:
986 sta->eapol_sm->dot1xAuthEapolFramesTx++;
992 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
993 wpa_eapol_variable var)
995 struct hostapd_data *hapd = ctx;
996 struct sta_info *sta = ap_get_sta(hapd, addr);
997 if (sta == NULL || sta->eapol_sm == NULL)
1000 case WPA_EAPOL_keyRun:
1001 return sta->eapol_sm->keyRun;
1002 case WPA_EAPOL_keyAvailable:
1003 return sta->eapol_sm->eap_if->eapKeyAvailable;
1010 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
1013 struct hostapd_data *hapd = ctx;
1014 return hostapd_get_psk(hapd->conf, addr, prev_psk);
1018 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
1021 struct hostapd_data *hapd = ctx;
1024 struct sta_info *sta;
1026 sta = ap_get_sta(hapd, addr);
1030 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
1036 os_memcpy(msk, key, keylen);
1043 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
1044 const u8 *addr, int idx, u8 *key,
1047 struct hostapd_data *hapd = ctx;
1048 const char *ifname = hapd->conf->iface;
1051 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
1056 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
1061 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
1064 struct hostapd_data *hapd = ctx;
1065 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
1069 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
1072 struct hostapd_data *hapd = ctx;
1073 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
1078 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
1079 const u8 *data, size_t data_len,
1082 struct hostapd_data *hapd = ctx;
1083 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
1087 static int hostapd_wpa_auth_for_each_sta(
1088 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
1091 struct hostapd_data *hapd = ctx;
1092 struct sta_info *sta;
1094 for (sta = hapd->sta_list; sta; sta = sta->next) {
1095 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
1102 static int hostapd_wpa_auth_for_each_auth(
1103 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
1106 struct hostapd_data *ohapd;
1108 struct hapd_interfaces *interfaces = eloop_get_user_data();
1110 for (i = 0; i < interfaces->count; i++) {
1111 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
1112 ohapd = interfaces->iface[i]->bss[j];
1113 if (cb(ohapd->wpa_auth, cb_ctx))
1122 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
1123 const u8 *data, size_t data_len)
1125 struct hostapd_data *hapd = ctx;
1127 if (hapd->driver && hapd->driver->send_ether)
1128 return hapd->driver->send_ether(hapd->drv_priv, dst,
1129 hapd->own_addr, proto,
1131 if (hapd->l2 == NULL)
1133 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
1137 #ifdef CONFIG_IEEE80211R
1139 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
1140 const u8 *data, size_t data_len)
1142 struct hostapd_data *hapd = ctx;
1144 struct ieee80211_mgmt *m;
1146 struct sta_info *sta;
1148 sta = ap_get_sta(hapd, dst);
1149 if (sta == NULL || sta->wpa_sm == NULL)
1152 m = os_zalloc(sizeof(*m) + data_len);
1155 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1156 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1157 WLAN_FC_STYPE_ACTION);
1158 os_memcpy(m->da, dst, ETH_ALEN);
1159 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1160 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1161 os_memcpy(&m->u, data, data_len);
1163 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1169 static struct wpa_state_machine *
1170 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1172 struct hostapd_data *hapd = ctx;
1173 struct sta_info *sta;
1175 sta = ap_sta_add(hapd, sta_addr);
1181 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1182 if (sta->wpa_sm == NULL) {
1183 ap_free_sta(hapd, sta);
1186 sta->auth_alg = WLAN_AUTH_FT;
1192 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1195 struct hostapd_data *hapd = ctx;
1196 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1199 #endif /* CONFIG_IEEE80211R */
1203 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1204 * @iface: Pointer to interface data
1205 * Returns: 0 on success, -1 on failure
1207 * This function is used to validate that the configured BSSIDs are valid.
1209 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1211 u8 mask[ETH_ALEN] = { 0 };
1212 struct hostapd_data *hapd = iface->bss[0];
1213 unsigned int i = iface->conf->num_bss, bits = 0, j;
1216 if (hostapd_drv_none(hapd))
1219 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1221 /* Determine the bits necessary to cover the number of BSSIDs. */
1222 for (i--; i; i >>= 1)
1225 /* Determine the bits necessary to any configured BSSIDs,
1226 if they are higher than the number of BSSIDs. */
1227 for (j = 0; j < iface->conf->num_bss; j++) {
1228 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1231 for (i = 0; i < ETH_ALEN; i++) {
1233 iface->conf->bss[j].bssid[i] ^
1238 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1244 while (mask[i] != 0) {
1256 os_memset(mask, 0xff, ETH_ALEN);
1258 for (i = 5; i > 5 - j; i--)
1264 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1265 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1267 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1272 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
1273 MACSTR " for start address " MACSTR ".",
1274 MAC2STR(mask), MAC2STR(hapd->own_addr));
1278 for (i = 0; i < ETH_ALEN; i++) {
1279 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1280 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
1281 " for start address " MACSTR ".",
1282 MAC2STR(mask), MAC2STR(hapd->own_addr));
1283 wpa_printf(MSG_ERROR, "Start address must be the "
1284 "first address in the block (i.e., addr "
1285 "AND mask == addr).");
1294 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1298 for (i = 0; i < conf->num_bss; i++) {
1299 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1308 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1310 struct wpa_auth_config _conf;
1311 struct wpa_auth_callbacks cb;
1315 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1316 os_memset(&cb, 0, sizeof(cb));
1318 cb.logger = hostapd_wpa_auth_logger;
1319 cb.disconnect = hostapd_wpa_auth_disconnect;
1320 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1321 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1322 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1323 cb.get_psk = hostapd_wpa_auth_get_psk;
1324 cb.get_msk = hostapd_wpa_auth_get_msk;
1325 cb.set_key = hostapd_wpa_auth_set_key;
1326 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1327 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1328 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1329 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1330 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1331 cb.send_ether = hostapd_wpa_auth_send_ether;
1332 #ifdef CONFIG_IEEE80211R
1333 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1334 cb.add_sta = hostapd_wpa_auth_add_sta;
1335 #endif /* CONFIG_IEEE80211R */
1336 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1337 if (hapd->wpa_auth == NULL) {
1338 wpa_printf(MSG_ERROR, "WPA initialization failed.");
1342 if (hostapd_set_privacy(hapd, 1)) {
1343 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1344 "for interface %s", hapd->conf->iface);
1348 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1349 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1350 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1351 "the kernel driver.");
1355 if (rsn_preauth_iface_init(hapd)) {
1356 wpa_printf(MSG_ERROR, "Initialization of RSN "
1357 "pre-authentication failed.");
1366 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1367 struct hostapd_bss_config *conf)
1369 struct radius_server_conf srv;
1370 os_memset(&srv, 0, sizeof(srv));
1371 srv.client_file = conf->radius_server_clients;
1372 srv.auth_port = conf->radius_server_auth_port;
1373 srv.conf_ctx = conf;
1374 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1375 srv.ssl_ctx = hapd->ssl_ctx;
1376 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1377 srv.eap_fast_a_id = conf->eap_fast_a_id;
1378 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1379 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1380 srv.eap_fast_prov = conf->eap_fast_prov;
1381 srv.pac_key_lifetime = conf->pac_key_lifetime;
1382 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1383 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1384 srv.tnc = conf->tnc;
1385 srv.wps = hapd->wps;
1386 srv.ipv6 = conf->radius_server_ipv6;
1387 srv.get_eap_user = hostapd_radius_get_eap_user;
1388 srv.eap_req_id_text = conf->eap_req_id_text;
1389 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1391 hapd->radius_srv = radius_server_init(&srv);
1392 if (hapd->radius_srv == NULL) {
1393 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1402 * hostapd_setup_bss - Per-BSS setup (initialization)
1403 * @hapd: Pointer to BSS data
1404 * @first: Whether this BSS is the first BSS of an interface
1406 * This function is used to initialize all per-BSS data structures and
1407 * resources. This gets called in a loop for each BSS when an interface is
1408 * initialized. Most of the modules that are initialized here will be
1409 * deinitialized in hostapd_cleanup().
1411 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1413 struct hostapd_bss_config *conf = hapd->conf;
1414 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1415 int ssid_len, set_ssid;
1418 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1419 /* Allocate the next available BSSID. */
1421 inc_byte_array(hapd->own_addr, ETH_ALEN);
1422 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1424 /* Allocate the configured BSSID. */
1425 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1427 if (hostapd_mac_comp(hapd->own_addr,
1428 hapd->iface->bss[0]->own_addr) ==
1430 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1431 "BSSID set to the MAC address of "
1432 "the radio", hapd->conf->iface);
1437 hapd->interface_added = 1;
1438 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1440 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1441 MACSTR ")", MAC2STR(hapd->own_addr));
1447 * Fetch the SSID from the system and use it or,
1448 * if one was specified in the config file, verify they
1451 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1453 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1456 if (conf->ssid.ssid_set) {
1458 * If SSID is specified in the config file and it differs
1459 * from what is being used then force installation of the
1462 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1463 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1466 * No SSID in the config file; just use the one we got
1470 conf->ssid.ssid_len = ssid_len;
1471 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1472 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1475 if (!hostapd_drv_none(hapd)) {
1476 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1478 hapd->conf->iface, MAC2STR(hapd->own_addr),
1479 hapd->conf->ssid.ssid);
1482 if (hostapd_setup_wpa_psk(conf)) {
1483 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1487 /* Set flag for whether SSID is broadcast in beacons */
1488 if (hostapd_set_broadcast_ssid(hapd,
1489 !!hapd->conf->ignore_broadcast_ssid)) {
1490 wpa_printf(MSG_ERROR, "Could not set broadcast SSID flag for "
1495 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1496 wpa_printf(MSG_ERROR, "Could not set DTIM period for kernel "
1501 /* Set SSID for the kernel driver (to be used in beacon and probe
1502 * response frames) */
1503 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1504 conf->ssid.ssid_len)) {
1505 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1509 if (wpa_debug_level == MSG_MSGDUMP)
1510 conf->radius->msg_dumps = 1;
1511 hapd->radius = radius_client_init(hapd, conf->radius);
1512 if (hapd->radius == NULL) {
1513 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1517 if (hostapd_acl_init(hapd)) {
1518 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1521 if (hostapd_init_wps(hapd, conf))
1524 if (ieee802_1x_init(hapd)) {
1525 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1529 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1532 if (accounting_init(hapd)) {
1533 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1537 if (hapd->conf->ieee802_11f &&
1538 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1539 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1544 if (hostapd_ctrl_iface_init(hapd)) {
1545 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1549 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1550 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1554 #ifdef CONFIG_IEEE80211R
1555 if (!hostapd_drv_none(hapd)) {
1556 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1557 hostapd_rrb_receive, hapd, 0);
1558 if (hapd->l2 == NULL &&
1559 (hapd->driver == NULL ||
1560 hapd->driver->send_ether == NULL)) {
1561 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1566 #endif /* CONFIG_IEEE80211R */
1568 ieee802_11_set_beacon(hapd);
1570 if (conf->radius_server_clients &&
1571 hostapd_setup_radius_srv(hapd, conf))
1578 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1580 struct hostapd_data *hapd = iface->bss[0];
1582 struct hostapd_tx_queue_params *p;
1584 for (i = 0; i < NUM_TX_QUEUES; i++) {
1585 p = &iface->conf->tx_queue[i];
1590 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1591 p->cwmax, p->burst)) {
1592 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1593 "parameters for queue %d.", i);
1594 /* Continue anyway */
1600 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1601 size_t identity_len, int phase2,
1602 struct eap_user *user)
1604 const struct hostapd_eap_user *eap_user;
1607 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1608 if (eap_user == NULL)
1614 os_memset(user, 0, sizeof(*user));
1615 count = EAP_USER_MAX_METHODS;
1616 if (count > EAP_MAX_METHODS)
1617 count = EAP_MAX_METHODS;
1618 for (i = 0; i < count; i++) {
1619 user->methods[i].vendor = eap_user->methods[i].vendor;
1620 user->methods[i].method = eap_user->methods[i].method;
1623 if (eap_user->password) {
1624 user->password = os_malloc(eap_user->password_len);
1625 if (user->password == NULL)
1627 os_memcpy(user->password, eap_user->password,
1628 eap_user->password_len);
1629 user->password_len = eap_user->password_len;
1630 user->password_hash = eap_user->password_hash;
1632 user->force_version = eap_user->force_version;
1633 user->ttls_auth = eap_user->ttls_auth;
1639 static int setup_interface(struct hostapd_iface *iface)
1641 struct hostapd_data *hapd = iface->bss[0];
1642 struct hostapd_bss_config *conf = hapd->conf;
1645 u8 *b = conf->bssid;
1652 * Initialize the driver interface and make sure that all BSSes get
1653 * configured with a pointer to this driver interface.
1655 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1656 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1658 hapd->drv_priv = hostapd_driver_init(hapd);
1661 if (hapd->drv_priv == NULL) {
1662 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1663 hapd->driver ? hapd->driver->name : "Unknown");
1664 hapd->driver = NULL;
1667 for (i = 0; i < iface->num_bss; i++) {
1668 iface->bss[i]->driver = hapd->driver;
1669 iface->bss[i]->drv_priv = hapd->drv_priv;
1672 if (hostapd_validate_bssid_configuration(iface))
1675 #ifdef CONFIG_IEEE80211N
1676 SET_2BIT_LE16(&iface->ht_op_mode,
1677 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1679 #endif /* CONFIG_IEEE80211N */
1681 os_memcpy(country, hapd->iconf->country, 3);
1683 if (hostapd_set_country(hapd, country) < 0) {
1684 wpa_printf(MSG_ERROR, "Failed to set country code");
1688 if (hapd->iconf->ieee80211d &&
1689 hostapd_set_ieee80211d(hapd, 1) < 0) {
1690 wpa_printf(MSG_ERROR, "Failed to set ieee80211d (%d)",
1691 hapd->iconf->ieee80211d);
1695 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1696 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1697 wpa_printf(MSG_ERROR, "Failed to set bridge_packets for "
1702 /* TODO: merge with hostapd_driver_init() ? */
1703 if (hostapd_wireless_event_init(hapd) < 0)
1706 if (hostapd_get_hw_features(iface)) {
1707 /* Not all drivers support this yet, so continue without hw
1710 int ret = hostapd_select_hw_mode(iface);
1712 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1713 "channel. (%d)", ret);
1718 hostapd_flush_old_stations(hapd);
1719 hostapd_set_privacy(hapd, 0);
1721 if (hapd->iconf->channel) {
1722 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1723 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1724 "Frequency: %d MHz",
1725 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1726 hapd->iconf->channel, freq);
1728 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1729 hapd->iconf->ieee80211n,
1730 hapd->iconf->secondary_channel)) {
1731 wpa_printf(MSG_ERROR, "Could not set channel for "
1737 hostapd_broadcast_wep_clear(hapd);
1738 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1741 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1742 ieee802_11_set_beacon(hapd);
1744 if (hapd->iconf->rts_threshold > -1 &&
1745 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1746 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1751 if (hapd->iconf->fragm_threshold > -1 &&
1752 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1753 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1754 "for kernel driver");
1758 prev_addr = hapd->own_addr;
1760 for (j = 0; j < iface->num_bss; j++) {
1761 hapd = iface->bss[j];
1763 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1764 if (hostapd_setup_bss(hapd, j == 0))
1766 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1767 prev_addr = hapd->own_addr;
1770 hostapd_tx_queue_params(iface);
1772 ap_list_init(iface);
1774 if (hostapd_driver_commit(hapd) < 0) {
1775 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1776 "configuration", __func__);
1785 * hostapd_setup_interface - Setup of an interface
1786 * @iface: Pointer to interface data.
1787 * Returns: 0 on success, -1 on failure
1789 * Initializes the driver interface, validates the configuration,
1790 * and sets driver parameters based on the configuration.
1791 * Flushes old stations, sets the channel, encryption,
1792 * beacons, and WDS links based on the configuration.
1794 static int hostapd_setup_interface(struct hostapd_iface *iface)
1798 ret = setup_interface(iface);
1800 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1801 iface->bss[0]->conf->iface);
1804 } else if (!hostapd_drv_none(iface->bss[0])) {
1805 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1806 iface->bss[0]->conf->iface);
1813 static void show_version(void)
1816 "hostapd v" VERSION_STR "\n"
1817 "User space daemon for IEEE 802.11 AP management,\n"
1818 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1819 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1820 "and contributors\n");
1824 static void usage(void)
1829 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1830 "<configuration file(s)>\n"
1833 " -h show this usage\n"
1834 " -d show more debug messages (-dd for even more)\n"
1835 " -B run daemon in the background\n"
1837 " -K include key data in debug messages\n"
1838 " -t include timestamps in some debug messages\n"
1839 " -v show hostapd version\n");
1846 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1847 * @hapd_iface: Pointer to interface data
1848 * @conf: Pointer to per-interface configuration
1849 * @bss: Pointer to per-BSS configuration for this BSS
1850 * Returns: Pointer to allocated BSS data
1852 * This function is used to allocate per-BSS data structure. This data will be
1853 * freed after hostapd_cleanup() is called for it during interface
1856 static struct hostapd_data *
1857 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1858 struct hostapd_config *conf,
1859 struct hostapd_bss_config *bss)
1861 struct hostapd_data *hapd;
1863 hapd = os_zalloc(sizeof(*hapd));
1869 hapd->iface = hapd_iface;
1871 if (hapd->conf->individual_wep_key_len > 0) {
1872 /* use key0 in individual key and key1 in broadcast key */
1873 hapd->default_wep_key_idx = 1;
1876 #ifdef EAP_TLS_FUNCS
1877 if (hapd->conf->eap_server &&
1878 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1879 hapd->conf->dh_file)) {
1880 struct tls_connection_params params;
1882 hapd->ssl_ctx = tls_init(NULL);
1883 if (hapd->ssl_ctx == NULL) {
1884 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1888 os_memset(¶ms, 0, sizeof(params));
1889 params.ca_cert = hapd->conf->ca_cert;
1890 params.client_cert = hapd->conf->server_cert;
1891 params.private_key = hapd->conf->private_key;
1892 params.private_key_passwd = hapd->conf->private_key_passwd;
1893 params.dh_file = hapd->conf->dh_file;
1895 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1896 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1900 if (tls_global_set_verify(hapd->ssl_ctx,
1901 hapd->conf->check_crl)) {
1902 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1906 #endif /* EAP_TLS_FUNCS */
1909 if (hapd->conf->eap_sim_db) {
1910 hapd->eap_sim_db_priv =
1911 eap_sim_db_init(hapd->conf->eap_sim_db,
1912 hostapd_sim_db_cb, hapd);
1913 if (hapd->eap_sim_db_priv == NULL) {
1914 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1915 "database interface");
1919 #endif /* EAP_SERVER */
1921 hapd->driver = hapd->iconf->driver;
1925 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1928 /* TODO: cleanup allocated resources(?) */
1935 * hostapd_init - Allocate and initialize per-interface data
1936 * @config_file: Path to the configuration file
1937 * Returns: Pointer to the allocated interface data or %NULL on failure
1939 * This function is used to allocate main data structures for per-interface
1940 * data. The allocated data buffer will be freed by calling
1941 * hostapd_cleanup_iface().
1943 static struct hostapd_iface * hostapd_init(const char *config_file)
1945 struct hostapd_iface *hapd_iface = NULL;
1946 struct hostapd_config *conf = NULL;
1947 struct hostapd_data *hapd;
1950 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1951 if (hapd_iface == NULL)
1954 hapd_iface->config_fname = os_strdup(config_file);
1955 if (hapd_iface->config_fname == NULL)
1958 conf = hostapd_config_read(hapd_iface->config_fname);
1961 hapd_iface->conf = conf;
1963 hapd_iface->num_bss = conf->num_bss;
1964 hapd_iface->bss = os_zalloc(conf->num_bss *
1965 sizeof(struct hostapd_data *));
1966 if (hapd_iface->bss == NULL)
1969 for (i = 0; i < conf->num_bss; i++) {
1970 hapd = hapd_iface->bss[i] =
1971 hostapd_alloc_bss_data(hapd_iface, conf,
1981 hostapd_config_free(conf);
1983 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1984 hapd = hapd_iface->bss[i];
1985 if (hapd && hapd->ssl_ctx)
1986 tls_deinit(hapd->ssl_ctx);
1989 os_free(hapd_iface->config_fname);
1990 os_free(hapd_iface->bss);
1991 os_free(hapd_iface);
1997 int main(int argc, char *argv[])
1999 struct hapd_interfaces interfaces;
2002 int c, debug = 0, daemonize = 0, tnc = 0;
2003 const char *pid_file = NULL;
2005 hostapd_logger_register_cb(hostapd_logger_cb);
2008 c = getopt(argc, argv, "BdhKP:tv");
2017 if (wpa_debug_level > 0)
2024 wpa_debug_show_keys++;
2030 wpa_debug_timestamp++;
2046 if (eap_server_register_methods()) {
2047 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
2051 interfaces.count = argc - optind;
2053 interfaces.iface = os_malloc(interfaces.count *
2054 sizeof(struct hostapd_iface *));
2055 if (interfaces.iface == NULL) {
2056 wpa_printf(MSG_ERROR, "malloc failed\n");
2060 if (eloop_init(&interfaces)) {
2061 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
2065 #ifndef CONFIG_NATIVE_WINDOWS
2066 eloop_register_signal(SIGHUP, handle_reload, NULL);
2067 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
2068 #endif /* CONFIG_NATIVE_WINDOWS */
2069 eloop_register_signal_terminate(handle_term, NULL);
2071 /* Initialize interfaces */
2072 for (i = 0; i < interfaces.count; i++) {
2073 wpa_printf(MSG_ERROR, "Configuration file: %s",
2075 interfaces.iface[i] = hostapd_init(argv[optind + i]);
2076 if (!interfaces.iface[i])
2078 for (k = 0; k < debug; k++) {
2079 if (interfaces.iface[i]->bss[0]->conf->
2080 logger_stdout_level > 0)
2081 interfaces.iface[i]->bss[0]->conf->
2082 logger_stdout_level--;
2085 ret = hostapd_setup_interface(interfaces.iface[i]);
2089 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
2090 if (interfaces.iface[i]->bss[0]->conf->tnc)
2096 if (tnc && tncs_global_init() < 0) {
2097 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
2100 #endif /* EAP_TNC */
2102 if (daemonize && os_daemonize(pid_file)) {
2107 #ifndef CONFIG_NATIVE_WINDOWS
2108 openlog("hostapd", 0, LOG_DAEMON);
2109 #endif /* CONFIG_NATIVE_WINDOWS */
2113 /* Disconnect associated stations from all interfaces and BSSes */
2114 for (i = 0; i < interfaces.count; i++) {
2115 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2116 struct hostapd_data *hapd =
2117 interfaces.iface[i]->bss[j];
2118 hostapd_free_stas(hapd);
2119 hostapd_flush_old_stations(hapd);
2126 /* Deinitialize all interfaces */
2127 for (i = 0; i < interfaces.count; i++) {
2128 if (!interfaces.iface[i])
2130 hostapd_cleanup_iface_pre(interfaces.iface[i]);
2131 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
2132 struct hostapd_data *hapd =
2133 interfaces.iface[i]->bss[j];
2134 hostapd_cleanup(hapd);
2135 if (j == interfaces.iface[i]->num_bss - 1 &&
2137 hostapd_driver_deinit(hapd);
2139 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
2140 os_free(interfaces.iface[i]->bss[j]);
2141 hostapd_cleanup_iface(interfaces.iface[i]);
2143 os_free(interfaces.iface);
2146 tncs_global_deinit();
2147 #endif /* EAP_TNC */
2151 #ifndef CONFIG_NATIVE_WINDOWS
2153 #endif /* CONFIG_NATIVE_WINDOWS */
2155 eap_server_unregister_methods();
2157 os_daemonize_terminate(pid_file);