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"
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"
47 #include "wps_hostapd.h"
50 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
51 size_t identity_len, int phase2,
52 struct eap_user *user);
53 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
54 static int hostapd_setup_wpa(struct hostapd_data *hapd);
55 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
57 struct hapd_interfaces {
59 struct hostapd_iface **iface;
62 unsigned char rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
65 extern int wpa_debug_level;
66 extern int wpa_debug_show_keys;
67 extern int wpa_debug_timestamp;
70 static void hostapd_logger_cb(void *ctx, const u8 *addr, unsigned int module,
71 int level, const char *txt, size_t len)
73 struct hostapd_data *hapd = ctx;
74 char *format, *module_str;
76 int conf_syslog_level, conf_stdout_level;
77 unsigned int conf_syslog, conf_stdout;
80 format = os_malloc(maxlen);
84 if (hapd && hapd->conf) {
85 conf_syslog_level = hapd->conf->logger_syslog_level;
86 conf_stdout_level = hapd->conf->logger_stdout_level;
87 conf_syslog = hapd->conf->logger_syslog;
88 conf_stdout = hapd->conf->logger_stdout;
90 conf_syslog_level = conf_stdout_level = 0;
91 conf_syslog = conf_stdout = (unsigned int) -1;
95 case HOSTAPD_MODULE_IEEE80211:
96 module_str = "IEEE 802.11";
98 case HOSTAPD_MODULE_IEEE8021X:
99 module_str = "IEEE 802.1X";
101 case HOSTAPD_MODULE_RADIUS:
102 module_str = "RADIUS";
104 case HOSTAPD_MODULE_WPA:
107 case HOSTAPD_MODULE_DRIVER:
108 module_str = "DRIVER";
110 case HOSTAPD_MODULE_IAPP:
113 case HOSTAPD_MODULE_MLME:
121 if (hapd && hapd->conf && addr)
122 os_snprintf(format, maxlen, "%s: STA " MACSTR "%s%s: %s",
123 hapd->conf->iface, MAC2STR(addr),
124 module_str ? " " : "", module_str, txt);
125 else if (hapd && hapd->conf)
126 os_snprintf(format, maxlen, "%s:%s%s %s",
127 hapd->conf->iface, module_str ? " " : "",
130 os_snprintf(format, maxlen, "STA " MACSTR "%s%s: %s",
131 MAC2STR(addr), module_str ? " " : "",
134 os_snprintf(format, maxlen, "%s%s%s",
135 module_str, module_str ? ": " : "", txt);
137 if ((conf_stdout & module) && level >= conf_stdout_level) {
138 wpa_debug_print_timestamp();
139 printf("%s\n", format);
142 #ifndef CONFIG_NATIVE_WINDOWS
143 if ((conf_syslog & module) && level >= conf_syslog_level) {
146 case HOSTAPD_LEVEL_DEBUG_VERBOSE:
147 case HOSTAPD_LEVEL_DEBUG:
148 priority = LOG_DEBUG;
150 case HOSTAPD_LEVEL_INFO:
153 case HOSTAPD_LEVEL_NOTICE:
154 priority = LOG_NOTICE;
156 case HOSTAPD_LEVEL_WARNING:
157 priority = LOG_WARNING;
163 syslog(priority, "%s", format);
165 #endif /* CONFIG_NATIVE_WINDOWS */
171 static void hostapd_deauth_all_stas(struct hostapd_data *hapd)
175 /* New Prism2.5/3 STA firmware versions seem to have issues with this
176 * broadcast deauth frame. This gets the firmware in odd state where
177 * nothing works correctly, so let's skip sending this for the hostap
180 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
181 os_memset(addr, 0xff, ETH_ALEN);
182 hostapd_sta_deauth(hapd, addr,
183 WLAN_REASON_PREV_AUTH_NOT_VALID);
189 * hostapd_prune_associations - Remove extraneous associations
190 * @hapd: Pointer to BSS data for the most recent association
191 * @sta: Pointer to the associated STA data
193 * This function looks through all radios and BSS's for previous
194 * (stale) associations of STA. If any are found they are removed.
196 static void hostapd_prune_associations(struct hostapd_data *hapd,
197 struct sta_info *sta)
199 struct sta_info *osta;
200 struct hostapd_data *ohapd;
202 struct hapd_interfaces *interfaces = eloop_get_user_data();
204 for (i = 0; i < interfaces->count; i++) {
205 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
206 ohapd = interfaces->iface[i]->bss[j];
209 osta = ap_get_sta(ohapd, sta->addr);
213 ap_sta_disassociate(ohapd, osta,
214 WLAN_REASON_UNSPECIFIED);
221 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
222 * @hapd: Pointer to BSS data
223 * @sta: Pointer to the associated STA data
224 * @reassoc: 1 to indicate this was a re-association; 0 = first association
226 * This function will be called whenever a station associates with the AP. It
227 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
228 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
229 * authentication and association) internally.
231 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
234 if (hapd->tkip_countermeasures) {
235 hostapd_sta_deauth(hapd, sta->addr,
236 WLAN_REASON_MICHAEL_MIC_FAILURE);
240 hostapd_prune_associations(hapd, sta);
242 /* IEEE 802.11F (IAPP) */
243 if (hapd->conf->ieee802_11f)
244 iapp_new_station(hapd->iapp, sta);
246 /* Start accounting here, if IEEE 802.1X and WPA are not used.
247 * IEEE 802.1X/WPA code will start accounting after the station has
248 * been authorized. */
249 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
250 accounting_sta_start(hapd, sta);
252 hostapd_wme_sta_config(hapd, sta);
254 /* Start IEEE 802.1X authentication process for new stations */
255 ieee802_1x_new_station(hapd, sta);
257 if (sta->auth_alg != WLAN_AUTH_FT &&
258 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
259 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
261 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);
266 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
267 struct sta_info *sta, void *ctx)
269 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
275 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
277 struct hostapd_data *hapd = ctx;
278 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0)
279 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
281 #endif /* EAP_SERVER */
284 static void handle_term(int sig, void *eloop_ctx, void *signal_ctx)
286 printf("Signal %d received - terminating\n", sig);
291 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
292 struct wpa_auth_config *wconf)
294 wconf->wpa = conf->wpa;
295 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
296 wconf->wpa_pairwise = conf->wpa_pairwise;
297 wconf->wpa_group = conf->wpa_group;
298 wconf->wpa_group_rekey = conf->wpa_group_rekey;
299 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
300 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
301 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
302 wconf->rsn_pairwise = conf->rsn_pairwise;
303 wconf->rsn_preauth = conf->rsn_preauth;
304 wconf->eapol_version = conf->eapol_version;
305 wconf->peerkey = conf->peerkey;
306 wconf->wme_enabled = conf->wme_enabled;
307 wconf->okc = conf->okc;
308 #ifdef CONFIG_IEEE80211W
309 wconf->ieee80211w = conf->ieee80211w;
310 #endif /* CONFIG_IEEE80211W */
311 #ifdef CONFIG_IEEE80211R
312 wconf->ssid_len = conf->ssid.ssid_len;
313 if (wconf->ssid_len > SSID_LEN)
314 wconf->ssid_len = SSID_LEN;
315 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
316 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
317 MOBILITY_DOMAIN_ID_LEN);
318 if (conf->nas_identifier &&
319 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
320 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
321 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
322 wconf->r0_key_holder_len);
324 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
325 wconf->r0_key_lifetime = conf->r0_key_lifetime;
326 wconf->reassociation_deadline = conf->reassociation_deadline;
327 wconf->r0kh_list = conf->r0kh_list;
328 wconf->r1kh_list = conf->r1kh_list;
329 wconf->pmk_r1_push = conf->pmk_r1_push;
330 #endif /* CONFIG_IEEE80211R */
334 int hostapd_reload_config(struct hostapd_iface *iface)
336 struct hostapd_data *hapd = iface->bss[0];
337 struct hostapd_config *newconf, *oldconf;
338 struct wpa_auth_config wpa_auth_conf;
340 newconf = hostapd_config_read(iface->config_fname);
345 * Deauthenticate all stations since the new configuration may not
346 * allow them to use the BSS anymore.
348 hostapd_flush_old_stations(hapd);
350 /* TODO: update dynamic data based on changed configuration
351 * items (e.g., open/close sockets, etc.) */
352 radius_client_flush(hapd->radius, 0);
354 oldconf = hapd->iconf;
355 hapd->iconf = newconf;
356 hapd->conf = &newconf->bss[0];
357 iface->conf = newconf;
359 if (hostapd_setup_wpa_psk(hapd->conf)) {
360 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
361 "after reloading configuration");
364 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
365 hostapd_setup_wpa(hapd);
366 else if (hapd->conf->wpa) {
367 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
368 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
369 } else if (hapd->wpa_auth) {
370 wpa_deinit(hapd->wpa_auth);
371 hapd->wpa_auth = NULL;
372 hostapd_set_privacy(hapd, 0);
373 hostapd_setup_encryption(hapd->conf->iface, hapd);
376 ieee802_11_set_beacon(hapd);
378 hostapd_config_free(oldconf);
380 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
386 #ifndef CONFIG_NATIVE_WINDOWS
387 static void handle_reload(int sig, void *eloop_ctx, void *signal_ctx)
389 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
392 printf("Signal %d received - reloading configuration\n", sig);
394 for (i = 0; i < hapds->count; i++) {
395 if (hostapd_reload_config(hapds->iface[i]) < 0) {
396 printf("Failed to read new configuration file - "
397 "continuing with old.\n");
404 #ifdef HOSTAPD_DUMP_STATE
405 static void hostapd_dump_state(struct hostapd_data *hapd)
409 struct sta_info *sta;
413 if (!hapd->conf->dump_log_name) {
414 printf("Dump file not defined - ignoring dump request\n");
418 printf("Dumping hostapd state to '%s'\n", hapd->conf->dump_log_name);
419 f = fopen(hapd->conf->dump_log_name, "w");
421 printf("Could not open dump file '%s' for writing.\n",
422 hapd->conf->dump_log_name);
427 fprintf(f, "hostapd state dump - %s", ctime(&now));
428 fprintf(f, "num_sta=%d num_sta_non_erp=%d "
429 "num_sta_no_short_slot_time=%d\n"
430 "num_sta_no_short_preamble=%d\n",
431 hapd->num_sta, hapd->iface->num_sta_non_erp,
432 hapd->iface->num_sta_no_short_slot_time,
433 hapd->iface->num_sta_no_short_preamble);
435 for (sta = hapd->sta_list; sta != NULL; sta = sta->next) {
436 fprintf(f, "\nSTA=" MACSTR "\n", MAC2STR(sta->addr));
439 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
440 " capability=0x%x listen_interval=%d\n",
443 (sta->flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
444 (sta->flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
445 (sta->flags & WLAN_STA_PS ? "[PS]" : ""),
446 (sta->flags & WLAN_STA_TIM ? "[TIM]" : ""),
447 (sta->flags & WLAN_STA_PERM ? "[PERM]" : ""),
448 (sta->flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" :
450 (sta->flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
452 (sta->flags & WLAN_STA_SHORT_PREAMBLE ?
453 "[SHORT_PREAMBLE]" : ""),
454 (sta->flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
455 (sta->flags & WLAN_STA_WME ? "[WME]" : ""),
456 (sta->flags & WLAN_STA_MFP ? "[MFP]" : ""),
457 (sta->flags & WLAN_STA_WPS ? "[WPS]" : ""),
458 (sta->flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
459 (sta->flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
461 sta->listen_interval);
463 fprintf(f, " supported_rates=");
464 for (i = 0; i < sta->supported_rates_len; i++)
465 fprintf(f, "%02x ", sta->supported_rates[i]);
469 " timeout_next=%s\n",
470 (sta->timeout_next == STA_NULLFUNC ? "NULLFUNC POLL" :
471 (sta->timeout_next == STA_DISASSOC ? "DISASSOC" :
474 ieee802_1x_dump_state(f, " ", sta);
477 buf = os_malloc(4096);
479 int count = radius_client_get_mib(hapd->radius, buf, 4096);
482 else if (count > 4095)
485 fprintf(f, "%s", buf);
487 count = radius_server_get_mib(hapd->radius_srv, buf, 4096);
490 else if (count > 4095)
493 fprintf(f, "%s", buf);
498 #endif /* HOSTAPD_DUMP_STATE */
501 static void handle_dump_state(int sig, void *eloop_ctx, void *signal_ctx)
503 #ifdef HOSTAPD_DUMP_STATE
504 struct hapd_interfaces *hapds = (struct hapd_interfaces *) eloop_ctx;
507 for (i = 0; i < hapds->count; i++) {
508 struct hostapd_iface *hapd_iface = hapds->iface[i];
509 for (j = 0; j < hapd_iface->num_bss; j++)
510 hostapd_dump_state(hapd_iface->bss[j]);
512 #endif /* HOSTAPD_DUMP_STATE */
514 #endif /* CONFIG_NATIVE_WINDOWS */
516 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
521 for (i = 0; i < NUM_WEP_KEYS; i++) {
522 if (hostapd_set_encryption(ifname, hapd, "none", NULL, i, NULL,
523 0, i == 0 ? 1 : 0)) {
524 printf("Failed to clear default encryption keys "
525 "(ifname=%s keyidx=%d)\n", ifname, i);
528 #ifdef CONFIG_IEEE80211W
529 if (hapd->conf->ieee80211w) {
530 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
531 if (hostapd_set_encryption(ifname, hapd, "none", NULL,
534 printf("Failed to clear default mgmt "
535 "encryption keys (ifname=%s keyidx=%d)"
540 #endif /* CONFIG_IEEE80211W */
544 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
546 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
551 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
554 struct hostapd_ssid *ssid = &hapd->conf->ssid;
557 if (ssid->wep.default_len &&
558 hostapd_set_encryption(hapd->conf->iface,
559 hapd, "WEP", NULL, idx,
562 idx == ssid->wep.idx)) {
563 printf("Could not set WEP encryption.\n");
567 if (ssid->dyn_vlan_keys) {
569 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
571 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
574 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
580 if (hostapd_set_encryption(ifname, hapd, "WEP", NULL,
584 printf("Could not set dynamic VLAN WEP "
595 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
596 * @hapd: Pointer to BSS data
598 * This function is used to free all per-BSS data structures and resources.
599 * This gets called in a loop for each BSS between calls to
600 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
601 * is deinitialized. Most of the modules that are initialized in
602 * hostapd_setup_bss() are deinitialized here.
604 static void hostapd_cleanup(struct hostapd_data *hapd)
606 hostapd_ctrl_iface_deinit(hapd);
608 os_free(hapd->default_wep_key);
609 hapd->default_wep_key = NULL;
610 iapp_deinit(hapd->iapp);
612 accounting_deinit(hapd);
613 rsn_preauth_iface_deinit(hapd);
614 if (hapd->wpa_auth) {
615 wpa_deinit(hapd->wpa_auth);
616 hapd->wpa_auth = NULL;
618 if (hostapd_set_privacy(hapd, 0)) {
619 wpa_printf(MSG_DEBUG, "Could not disable "
620 "PrivacyInvoked for interface %s",
624 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
625 wpa_printf(MSG_DEBUG, "Could not remove generic "
626 "information element from interface %s",
630 ieee802_1x_deinit(hapd);
632 hostapd_acl_deinit(hapd);
633 radius_client_deinit(hapd->radius);
635 radius_server_deinit(hapd->radius_srv);
636 hapd->radius_srv = NULL;
638 #ifdef CONFIG_IEEE80211R
639 l2_packet_deinit(hapd->l2);
640 #endif /* CONFIG_IEEE80211R */
642 hostapd_deinit_wps(hapd);
644 hostapd_wireless_event_deinit(hapd);
648 tls_deinit(hapd->ssl_ctx);
649 hapd->ssl_ctx = NULL;
651 #endif /* EAP_TLS_FUNCS */
654 if (hapd->eap_sim_db_priv) {
655 eap_sim_db_deinit(hapd->eap_sim_db_priv);
656 hapd->eap_sim_db_priv = NULL;
658 #endif /* EAP_SERVER */
660 if (hapd->interface_added &&
661 hostapd_bss_remove(hapd, hapd->conf->iface)) {
662 printf("Failed to remove BSS interface %s\n",
669 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
670 * @iface: Pointer to interface data
672 * This function is called before per-BSS data structures are deinitialized
673 * with hostapd_cleanup().
675 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
681 * hostapd_cleanup_iface - Complete per-interface cleanup
682 * @iface: Pointer to interface data
684 * This function is called after per-BSS data structures are deinitialized
685 * with hostapd_cleanup().
687 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
689 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
690 iface->hw_features = NULL;
691 os_free(iface->current_rates);
692 iface->current_rates = NULL;
693 ap_list_deinit(iface);
694 hostapd_config_free(iface->conf);
697 os_free(iface->config_fname);
703 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
707 hostapd_broadcast_wep_set(hapd);
709 if (hapd->conf->ssid.wep.default_len)
712 for (i = 0; i < 4; i++) {
713 if (hapd->conf->ssid.wep.key[i] &&
714 hostapd_set_encryption(iface, hapd, "WEP", NULL,
715 i, hapd->conf->ssid.wep.key[i],
716 hapd->conf->ssid.wep.len[i],
717 i == hapd->conf->ssid.wep.idx)) {
718 printf("Could not set WEP encryption.\n");
721 if (hapd->conf->ssid.wep.key[i] &&
722 i == hapd->conf->ssid.wep.idx)
723 hostapd_set_privacy(hapd, 1);
730 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
734 if (hostapd_drv_none(hapd))
737 wpa_printf(MSG_DEBUG, "Flushing old station entries");
738 if (hostapd_flush(hapd)) {
739 printf("Could not connect to kernel driver.\n");
742 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
743 hostapd_deauth_all_stas(hapd);
749 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
750 logger_level level, const char *txt)
752 struct hostapd_data *hapd = ctx;
757 hlevel = HOSTAPD_LEVEL_WARNING;
760 hlevel = HOSTAPD_LEVEL_INFO;
764 hlevel = HOSTAPD_LEVEL_DEBUG;
768 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
772 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
775 struct hostapd_data *hapd = ctx;
776 struct sta_info *sta;
778 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
779 "STA " MACSTR " reason %d",
780 __func__, MAC2STR(addr), reason);
782 sta = ap_get_sta(hapd, addr);
783 hostapd_sta_deauth(hapd, addr, reason);
786 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
787 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
788 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
789 sta->timeout_next = STA_REMOVE;
793 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
795 struct hostapd_data *hapd = ctx;
796 ieee80211_michael_mic_failure(hapd, addr, 0);
800 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
801 wpa_eapol_variable var, int value)
803 struct hostapd_data *hapd = ctx;
804 struct sta_info *sta = ap_get_sta(hapd, addr);
808 case WPA_EAPOL_portEnabled:
809 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
811 case WPA_EAPOL_portValid:
812 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
814 case WPA_EAPOL_authorized:
815 ieee802_1x_set_sta_authorized(hapd, sta, value);
817 case WPA_EAPOL_portControl_Auto:
819 sta->eapol_sm->portControl = Auto;
821 case WPA_EAPOL_keyRun:
823 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
825 case WPA_EAPOL_keyAvailable:
827 sta->eapol_sm->eap_if->eapKeyAvailable =
828 value ? TRUE : FALSE;
830 case WPA_EAPOL_keyDone:
832 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
834 case WPA_EAPOL_inc_EapolFramesTx:
836 sta->eapol_sm->dot1xAuthEapolFramesTx++;
842 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
843 wpa_eapol_variable var)
845 struct hostapd_data *hapd = ctx;
846 struct sta_info *sta = ap_get_sta(hapd, addr);
847 if (sta == NULL || sta->eapol_sm == NULL)
850 case WPA_EAPOL_keyRun:
851 return sta->eapol_sm->keyRun;
852 case WPA_EAPOL_keyAvailable:
853 return sta->eapol_sm->eap_if->eapKeyAvailable;
860 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
863 struct hostapd_data *hapd = ctx;
864 return hostapd_get_psk(hapd->conf, addr, prev_psk);
868 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
871 struct hostapd_data *hapd = ctx;
874 struct sta_info *sta;
876 sta = ap_get_sta(hapd, addr);
880 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
886 os_memcpy(msk, key, keylen);
893 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, const char *alg,
894 const u8 *addr, int idx, u8 *key,
897 struct hostapd_data *hapd = ctx;
898 const char *ifname = hapd->conf->iface;
901 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
906 return hostapd_set_encryption(ifname, hapd, alg, addr, idx,
911 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
914 struct hostapd_data *hapd = ctx;
915 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
919 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx, const u8 *addr, int idx,
922 struct hostapd_data *hapd = ctx;
923 return hostapd_get_seqnum_igtk(hapd->conf->iface, hapd, addr, idx,
928 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
929 const u8 *data, size_t data_len,
932 struct hostapd_data *hapd = ctx;
933 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
937 static int hostapd_wpa_auth_for_each_sta(
938 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
941 struct hostapd_data *hapd = ctx;
942 struct sta_info *sta;
944 for (sta = hapd->sta_list; sta; sta = sta->next) {
945 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
952 static int hostapd_wpa_auth_for_each_auth(
953 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
956 struct hostapd_data *ohapd;
958 struct hapd_interfaces *interfaces = eloop_get_user_data();
960 for (i = 0; i < interfaces->count; i++) {
961 for (j = 0; j < interfaces->iface[i]->num_bss; j++) {
962 ohapd = interfaces->iface[i]->bss[j];
963 if (cb(ohapd->wpa_auth, cb_ctx))
972 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
973 const u8 *data, size_t data_len)
975 struct hostapd_data *hapd = ctx;
977 if (hapd->driver && hapd->driver->send_ether)
978 return hapd->driver->send_ether(hapd->drv_priv, dst,
979 hapd->own_addr, proto,
981 if (hapd->l2 == NULL)
983 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
987 #ifdef CONFIG_IEEE80211R
989 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
990 const u8 *data, size_t data_len)
992 struct hostapd_data *hapd = ctx;
994 struct ieee80211_mgmt *m;
996 struct sta_info *sta;
998 sta = ap_get_sta(hapd, dst);
999 if (sta == NULL || sta->wpa_sm == NULL)
1002 m = os_zalloc(sizeof(*m) + data_len);
1005 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
1006 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
1007 WLAN_FC_STYPE_ACTION);
1008 os_memcpy(m->da, dst, ETH_ALEN);
1009 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
1010 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
1011 os_memcpy(&m->u, data, data_len);
1013 res = hostapd_send_mgmt_frame(hapd, (u8 *) m, mlen, 0);
1019 static struct wpa_state_machine *
1020 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
1022 struct hostapd_data *hapd = ctx;
1023 struct sta_info *sta;
1025 sta = ap_sta_add(hapd, sta_addr);
1031 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
1032 if (sta->wpa_sm == NULL) {
1033 ap_free_sta(hapd, sta);
1036 sta->auth_alg = WLAN_AUTH_FT;
1042 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
1045 struct hostapd_data *hapd = ctx;
1046 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
1049 #endif /* CONFIG_IEEE80211R */
1053 * hostapd_validate_bssid_configuration - Validate BSSID configuration
1054 * @iface: Pointer to interface data
1055 * Returns: 0 on success, -1 on failure
1057 * This function is used to validate that the configured BSSIDs are valid.
1059 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
1061 u8 mask[ETH_ALEN] = { 0 };
1062 struct hostapd_data *hapd = iface->bss[0];
1063 unsigned int i = iface->conf->num_bss, bits = 0, j;
1066 if (hostapd_drv_none(hapd))
1069 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1071 /* Determine the bits necessary to cover the number of BSSIDs. */
1072 for (i--; i; i >>= 1)
1075 /* Determine the bits necessary to any configured BSSIDs,
1076 if they are higher than the number of BSSIDs. */
1077 for (j = 0; j < iface->conf->num_bss; j++) {
1078 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0)
1081 for (i = 0; i < ETH_ALEN; i++) {
1083 iface->conf->bss[j].bssid[i] ^
1088 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
1094 while (mask[i] != 0) {
1106 os_memset(mask, 0xff, ETH_ALEN);
1108 for (i = 5; i > 5 - j; i--)
1114 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
1115 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
1117 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
1122 printf("Driver did not accept BSSID mask " MACSTR " for start "
1123 "address " MACSTR ".\n",
1124 MAC2STR(mask), MAC2STR(hapd->own_addr));
1128 for (i = 0; i < ETH_ALEN; i++) {
1129 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
1130 printf("Invalid BSSID mask " MACSTR " for start "
1131 "address " MACSTR ".\n"
1132 "Start address must be the first address in the"
1133 " block (i.e., addr AND mask == addr).\n",
1134 MAC2STR(mask), MAC2STR(hapd->own_addr));
1143 static int mac_in_conf(struct hostapd_config *conf, const void *a)
1147 for (i = 0; i < conf->num_bss; i++) {
1148 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
1157 static int hostapd_setup_wpa(struct hostapd_data *hapd)
1159 struct wpa_auth_config _conf;
1160 struct wpa_auth_callbacks cb;
1164 hostapd_wpa_auth_conf(hapd->conf, &_conf);
1165 os_memset(&cb, 0, sizeof(cb));
1167 cb.logger = hostapd_wpa_auth_logger;
1168 cb.disconnect = hostapd_wpa_auth_disconnect;
1169 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
1170 cb.set_eapol = hostapd_wpa_auth_set_eapol;
1171 cb.get_eapol = hostapd_wpa_auth_get_eapol;
1172 cb.get_psk = hostapd_wpa_auth_get_psk;
1173 cb.get_msk = hostapd_wpa_auth_get_msk;
1174 cb.set_key = hostapd_wpa_auth_set_key;
1175 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
1176 cb.get_seqnum_igtk = hostapd_wpa_auth_get_seqnum_igtk;
1177 cb.send_eapol = hostapd_wpa_auth_send_eapol;
1178 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
1179 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
1180 cb.send_ether = hostapd_wpa_auth_send_ether;
1181 #ifdef CONFIG_IEEE80211R
1182 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
1183 cb.add_sta = hostapd_wpa_auth_add_sta;
1184 #endif /* CONFIG_IEEE80211R */
1185 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
1186 if (hapd->wpa_auth == NULL) {
1187 printf("WPA initialization failed.\n");
1191 if (hostapd_set_privacy(hapd, 1)) {
1192 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
1193 "for interface %s", hapd->conf->iface);
1197 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
1198 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
1199 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
1200 "the kernel driver.");
1204 if (rsn_preauth_iface_init(hapd)) {
1205 printf("Initialization of RSN pre-authentication "
1215 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
1216 struct hostapd_bss_config *conf)
1218 struct radius_server_conf srv;
1219 os_memset(&srv, 0, sizeof(srv));
1220 srv.client_file = conf->radius_server_clients;
1221 srv.auth_port = conf->radius_server_auth_port;
1222 srv.conf_ctx = conf;
1223 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
1224 srv.ssl_ctx = hapd->ssl_ctx;
1225 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
1226 srv.eap_fast_a_id = conf->eap_fast_a_id;
1227 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
1228 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
1229 srv.eap_fast_prov = conf->eap_fast_prov;
1230 srv.pac_key_lifetime = conf->pac_key_lifetime;
1231 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
1232 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
1233 srv.tnc = conf->tnc;
1234 srv.wps = hapd->wps;
1235 srv.ipv6 = conf->radius_server_ipv6;
1236 srv.get_eap_user = hostapd_radius_get_eap_user;
1237 srv.eap_req_id_text = conf->eap_req_id_text;
1238 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
1240 hapd->radius_srv = radius_server_init(&srv);
1241 if (hapd->radius_srv == NULL) {
1242 printf("RADIUS server initialization failed.\n");
1251 * hostapd_setup_bss - Per-BSS setup (initialization)
1252 * @hapd: Pointer to BSS data
1253 * @first: Whether this BSS is the first BSS of an interface
1255 * This function is used to initialize all per-BSS data structures and
1256 * resources. This gets called in a loop for each BSS when an interface is
1257 * initialized. Most of the modules that are initialized here will be
1258 * deinitialized in hostapd_cleanup().
1260 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1262 struct hostapd_bss_config *conf = hapd->conf;
1263 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1264 int ssid_len, set_ssid;
1267 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1268 /* Allocate the next available BSSID. */
1270 inc_byte_array(hapd->own_addr, ETH_ALEN);
1271 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1273 /* Allocate the configured BSSID. */
1274 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1276 if (hostapd_mac_comp(hapd->own_addr,
1277 hapd->iface->bss[0]->own_addr) ==
1279 printf("BSS '%s' may not have BSSID "
1280 "set to the MAC address of the radio\n",
1286 hapd->interface_added = 1;
1287 if (hostapd_bss_add(hapd->iface->bss[0], hapd->conf->iface,
1289 printf("Failed to add BSS (BSSID=" MACSTR ")\n",
1290 MAC2STR(hapd->own_addr));
1296 * Fetch the SSID from the system and use it or,
1297 * if one was specified in the config file, verify they
1300 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1302 printf("Could not read SSID from system\n");
1305 if (conf->ssid.ssid_set) {
1307 * If SSID is specified in the config file and it differs
1308 * from what is being used then force installation of the
1311 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1312 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1315 * No SSID in the config file; just use the one we got
1319 conf->ssid.ssid_len = ssid_len;
1320 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1321 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1324 if (!hostapd_drv_none(hapd)) {
1325 printf("Using interface %s with hwaddr " MACSTR
1327 hapd->conf->iface, MAC2STR(hapd->own_addr),
1328 hapd->conf->ssid.ssid);
1331 if (hostapd_setup_wpa_psk(conf)) {
1332 printf("WPA-PSK setup failed.\n");
1336 /* Set flag for whether SSID is broadcast in beacons */
1337 if (hostapd_set_broadcast_ssid(hapd,
1338 !!hapd->conf->ignore_broadcast_ssid)) {
1339 printf("Could not set broadcast SSID flag for kernel "
1344 if (hostapd_set_dtim_period(hapd, hapd->conf->dtim_period)) {
1345 printf("Could not set DTIM period for kernel driver\n");
1349 /* Set SSID for the kernel driver (to be used in beacon and probe
1350 * response frames) */
1351 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1352 conf->ssid.ssid_len)) {
1353 printf("Could not set SSID for kernel driver\n");
1357 if (wpa_debug_level == MSG_MSGDUMP)
1358 conf->radius->msg_dumps = 1;
1359 hapd->radius = radius_client_init(hapd, conf->radius);
1360 if (hapd->radius == NULL) {
1361 printf("RADIUS client initialization failed.\n");
1365 if (hostapd_acl_init(hapd)) {
1366 printf("ACL initialization failed.\n");
1369 if (hostapd_init_wps(hapd, conf))
1372 if (ieee802_1x_init(hapd)) {
1373 printf("IEEE 802.1X initialization failed.\n");
1377 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1380 if (accounting_init(hapd)) {
1381 printf("Accounting initialization failed.\n");
1385 if (hapd->conf->ieee802_11f &&
1386 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1387 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1391 if (hostapd_ctrl_iface_init(hapd)) {
1392 printf("Failed to setup control interface\n");
1396 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1397 printf("VLAN initialization failed.\n");
1401 #ifdef CONFIG_IEEE80211R
1402 if (!hostapd_drv_none(hapd)) {
1403 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1404 hostapd_rrb_receive, hapd, 0);
1405 if (hapd->l2 == NULL &&
1406 (hapd->driver == NULL ||
1407 hapd->driver->send_ether == NULL)) {
1408 printf("Failed to open l2_packet interface\n");
1412 #endif /* CONFIG_IEEE80211R */
1414 ieee802_11_set_beacon(hapd);
1416 if (conf->radius_server_clients &&
1417 hostapd_setup_radius_srv(hapd, conf))
1424 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1426 struct hostapd_data *hapd = iface->bss[0];
1428 struct hostapd_tx_queue_params *p;
1430 for (i = 0; i < NUM_TX_QUEUES; i++) {
1431 p = &iface->conf->tx_queue[i];
1436 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1437 p->cwmax, p->burst)) {
1438 printf("Failed to set TX queue parameters for queue %d"
1440 /* Continue anyway */
1446 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
1447 size_t identity_len, int phase2,
1448 struct eap_user *user)
1450 const struct hostapd_eap_user *eap_user;
1453 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
1454 if (eap_user == NULL)
1460 os_memset(user, 0, sizeof(*user));
1461 count = EAP_USER_MAX_METHODS;
1462 if (count > EAP_MAX_METHODS)
1463 count = EAP_MAX_METHODS;
1464 for (i = 0; i < count; i++) {
1465 user->methods[i].vendor = eap_user->methods[i].vendor;
1466 user->methods[i].method = eap_user->methods[i].method;
1469 if (eap_user->password) {
1470 user->password = os_malloc(eap_user->password_len);
1471 if (user->password == NULL)
1473 os_memcpy(user->password, eap_user->password,
1474 eap_user->password_len);
1475 user->password_len = eap_user->password_len;
1476 user->password_hash = eap_user->password_hash;
1478 user->force_version = eap_user->force_version;
1479 user->ttls_auth = eap_user->ttls_auth;
1485 static int setup_interface(struct hostapd_iface *iface)
1487 struct hostapd_data *hapd = iface->bss[0];
1488 struct hostapd_bss_config *conf = hapd->conf;
1491 u8 *b = conf->bssid;
1498 * Initialize the driver interface and make sure that all BSSes get
1499 * configured with a pointer to this driver interface.
1501 if (b[0] | b[1] | b[2] | b[3] | b[4] | b[5]) {
1502 hapd->drv_priv = hostapd_driver_init_bssid(hapd, b);
1504 hapd->drv_priv = hostapd_driver_init(hapd);
1507 if (hapd->drv_priv == NULL) {
1508 printf("%s driver initialization failed.\n",
1509 hapd->driver ? hapd->driver->name : "Unknown");
1510 hapd->driver = NULL;
1513 for (i = 0; i < iface->num_bss; i++) {
1514 iface->bss[i]->driver = hapd->driver;
1515 iface->bss[i]->drv_priv = hapd->drv_priv;
1518 if (hostapd_validate_bssid_configuration(iface))
1521 #ifdef CONFIG_IEEE80211N
1522 SET_2BIT_LE16(&iface->ht_op_mode,
1523 HT_INFO_OPERATION_MODE_OP_MODE_OFFSET,
1525 #endif /* CONFIG_IEEE80211N */
1527 os_memcpy(country, hapd->iconf->country, 3);
1529 if (hostapd_set_country(hapd, country) < 0) {
1530 printf("Failed to set country code\n");
1534 if (hapd->iconf->ieee80211d &&
1535 hostapd_set_ieee80211d(hapd, 1) < 0) {
1536 printf("Failed to set ieee80211d (%d)\n",
1537 hapd->iconf->ieee80211d);
1541 if (hapd->iconf->bridge_packets != INTERNAL_BRIDGE_DO_NOT_CONTROL &&
1542 hostapd_set_internal_bridge(hapd, hapd->iconf->bridge_packets)) {
1543 printf("Failed to set bridge_packets for kernel driver\n");
1547 /* TODO: merge with hostapd_driver_init() ? */
1548 if (hostapd_wireless_event_init(hapd) < 0)
1551 if (hostapd_get_hw_features(iface)) {
1552 /* Not all drivers support this yet, so continue without hw
1555 int ret = hostapd_select_hw_mode(iface);
1557 printf("Could not select hw_mode and channel. (%d)\n",
1563 hostapd_flush_old_stations(hapd);
1564 hostapd_set_privacy(hapd, 0);
1566 if (hapd->iconf->channel) {
1567 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1568 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1569 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1570 hapd->iconf->channel, freq);
1572 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1573 hapd->iconf->ieee80211n,
1574 hapd->iconf->secondary_channel)) {
1575 printf("Could not set channel for kernel driver\n");
1580 hostapd_broadcast_wep_clear(hapd);
1581 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1584 hostapd_set_beacon_int(hapd, hapd->iconf->beacon_int);
1585 ieee802_11_set_beacon(hapd);
1587 if (hapd->iconf->rts_threshold > -1 &&
1588 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1589 printf("Could not set RTS threshold for kernel driver\n");
1593 if (hapd->iconf->fragm_threshold > -1 &&
1594 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1595 printf("Could not set fragmentation threshold for kernel "
1600 prev_addr = hapd->own_addr;
1602 for (j = 0; j < iface->num_bss; j++) {
1603 hapd = iface->bss[j];
1605 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1606 if (hostapd_setup_bss(hapd, j == 0))
1608 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1609 prev_addr = hapd->own_addr;
1612 hostapd_tx_queue_params(iface);
1614 ap_list_init(iface);
1616 if (hostapd_driver_commit(hapd) < 0) {
1617 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1618 "configuration", __func__);
1627 * hostapd_setup_interface - Setup of an interface
1628 * @iface: Pointer to interface data.
1629 * Returns: 0 on success, -1 on failure
1631 * Initializes the driver interface, validates the configuration,
1632 * and sets driver parameters based on the configuration.
1633 * Flushes old stations, sets the channel, encryption,
1634 * beacons, and WDS links based on the configuration.
1636 static int hostapd_setup_interface(struct hostapd_iface *iface)
1640 ret = setup_interface(iface);
1642 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1643 iface->bss[0]->conf->iface);
1646 } else if (!hostapd_drv_none(iface->bss[0])) {
1647 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1648 iface->bss[0]->conf->iface);
1655 static void show_version(void)
1658 "hostapd v" VERSION_STR "\n"
1659 "User space daemon for IEEE 802.11 AP management,\n"
1660 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1661 "Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> "
1662 "and contributors\n");
1666 static void usage(void)
1671 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1672 "<configuration file(s)>\n"
1675 " -h show this usage\n"
1676 " -d show more debug messages (-dd for even more)\n"
1677 " -B run daemon in the background\n"
1679 " -K include key data in debug messages\n"
1680 " -t include timestamps in some debug messages\n"
1681 " -v show hostapd version\n");
1688 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1689 * @hapd_iface: Pointer to interface data
1690 * @conf: Pointer to per-interface configuration
1691 * @bss: Pointer to per-BSS configuration for this BSS
1692 * Returns: Pointer to allocated BSS data
1694 * This function is used to allocate per-BSS data structure. This data will be
1695 * freed after hostapd_cleanup() is called for it during interface
1698 static struct hostapd_data *
1699 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1700 struct hostapd_config *conf,
1701 struct hostapd_bss_config *bss)
1703 struct hostapd_data *hapd;
1705 hapd = os_zalloc(sizeof(*hapd));
1711 hapd->iface = hapd_iface;
1713 if (hapd->conf->individual_wep_key_len > 0) {
1714 /* use key0 in individual key and key1 in broadcast key */
1715 hapd->default_wep_key_idx = 1;
1718 #ifdef EAP_TLS_FUNCS
1719 if (hapd->conf->eap_server &&
1720 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1721 hapd->conf->dh_file)) {
1722 struct tls_connection_params params;
1724 hapd->ssl_ctx = tls_init(NULL);
1725 if (hapd->ssl_ctx == NULL) {
1726 printf("Failed to initialize TLS\n");
1730 os_memset(¶ms, 0, sizeof(params));
1731 params.ca_cert = hapd->conf->ca_cert;
1732 params.client_cert = hapd->conf->server_cert;
1733 params.private_key = hapd->conf->private_key;
1734 params.private_key_passwd = hapd->conf->private_key_passwd;
1735 params.dh_file = hapd->conf->dh_file;
1737 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1738 printf("Failed to set TLS parameters\n");
1742 if (tls_global_set_verify(hapd->ssl_ctx,
1743 hapd->conf->check_crl)) {
1744 printf("Failed to enable check_crl\n");
1748 #endif /* EAP_TLS_FUNCS */
1751 if (hapd->conf->eap_sim_db) {
1752 hapd->eap_sim_db_priv =
1753 eap_sim_db_init(hapd->conf->eap_sim_db,
1754 hostapd_sim_db_cb, hapd);
1755 if (hapd->eap_sim_db_priv == NULL) {
1756 printf("Failed to initialize EAP-SIM database "
1761 #endif /* EAP_SERVER */
1763 hapd->driver = hapd->iconf->driver;
1767 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1770 /* TODO: cleanup allocated resources(?) */
1777 * hostapd_init - Allocate and initialize per-interface data
1778 * @config_file: Path to the configuration file
1779 * Returns: Pointer to the allocated interface data or %NULL on failure
1781 * This function is used to allocate main data structures for per-interface
1782 * data. The allocated data buffer will be freed by calling
1783 * hostapd_cleanup_iface().
1785 static struct hostapd_iface * hostapd_init(const char *config_file)
1787 struct hostapd_iface *hapd_iface = NULL;
1788 struct hostapd_config *conf = NULL;
1789 struct hostapd_data *hapd;
1792 hapd_iface = os_zalloc(sizeof(*hapd_iface));
1793 if (hapd_iface == NULL)
1796 hapd_iface->config_fname = os_strdup(config_file);
1797 if (hapd_iface->config_fname == NULL)
1800 conf = hostapd_config_read(hapd_iface->config_fname);
1803 hapd_iface->conf = conf;
1805 hapd_iface->num_bss = conf->num_bss;
1806 hapd_iface->bss = os_zalloc(conf->num_bss *
1807 sizeof(struct hostapd_data *));
1808 if (hapd_iface->bss == NULL)
1811 for (i = 0; i < conf->num_bss; i++) {
1812 hapd = hapd_iface->bss[i] =
1813 hostapd_alloc_bss_data(hapd_iface, conf,
1823 hostapd_config_free(conf);
1825 for (i = 0; hapd_iface->bss && i < hapd_iface->num_bss; i++) {
1826 hapd = hapd_iface->bss[i];
1827 if (hapd && hapd->ssl_ctx)
1828 tls_deinit(hapd->ssl_ctx);
1831 os_free(hapd_iface->config_fname);
1832 os_free(hapd_iface->bss);
1833 os_free(hapd_iface);
1839 int main(int argc, char *argv[])
1841 struct hapd_interfaces interfaces;
1844 int c, debug = 0, daemonize = 0, tnc = 0;
1845 const char *pid_file = NULL;
1847 hostapd_logger_register_cb(hostapd_logger_cb);
1850 c = getopt(argc, argv, "BdhKP:tv");
1859 if (wpa_debug_level > 0)
1866 wpa_debug_show_keys++;
1872 wpa_debug_timestamp++;
1888 if (eap_server_register_methods()) {
1889 wpa_printf(MSG_ERROR, "Failed to register EAP methods");
1893 interfaces.count = argc - optind;
1895 interfaces.iface = os_malloc(interfaces.count *
1896 sizeof(struct hostapd_iface *));
1897 if (interfaces.iface == NULL) {
1898 wpa_printf(MSG_ERROR, "malloc failed\n");
1902 if (eloop_init(&interfaces)) {
1903 wpa_printf(MSG_ERROR, "Failed to initialize event loop");
1907 #ifndef CONFIG_NATIVE_WINDOWS
1908 eloop_register_signal(SIGHUP, handle_reload, NULL);
1909 eloop_register_signal(SIGUSR1, handle_dump_state, NULL);
1910 #endif /* CONFIG_NATIVE_WINDOWS */
1911 eloop_register_signal_terminate(handle_term, NULL);
1913 /* Initialize interfaces */
1914 for (i = 0; i < interfaces.count; i++) {
1915 printf("Configuration file: %s\n", argv[optind + i]);
1916 interfaces.iface[i] = hostapd_init(argv[optind + i]);
1917 if (!interfaces.iface[i])
1919 for (k = 0; k < debug; k++) {
1920 if (interfaces.iface[i]->bss[0]->conf->
1921 logger_stdout_level > 0)
1922 interfaces.iface[i]->bss[0]->conf->
1923 logger_stdout_level--;
1926 ret = hostapd_setup_interface(interfaces.iface[i]);
1930 for (k = 0; k < (int) interfaces.iface[i]->num_bss; k++) {
1931 if (interfaces.iface[i]->bss[0]->conf->tnc)
1937 if (tnc && tncs_global_init() < 0) {
1938 wpa_printf(MSG_ERROR, "Failed to initialize TNCS");
1941 #endif /* EAP_TNC */
1943 if (daemonize && os_daemonize(pid_file)) {
1948 #ifndef CONFIG_NATIVE_WINDOWS
1949 openlog("hostapd", 0, LOG_DAEMON);
1950 #endif /* CONFIG_NATIVE_WINDOWS */
1954 /* Disconnect associated stations from all interfaces and BSSes */
1955 for (i = 0; i < interfaces.count; i++) {
1956 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1957 struct hostapd_data *hapd =
1958 interfaces.iface[i]->bss[j];
1959 hostapd_free_stas(hapd);
1960 hostapd_flush_old_stations(hapd);
1967 /* Deinitialize all interfaces */
1968 for (i = 0; i < interfaces.count; i++) {
1969 if (!interfaces.iface[i])
1971 hostapd_cleanup_iface_pre(interfaces.iface[i]);
1972 for (j = 0; j < interfaces.iface[i]->num_bss; j++) {
1973 struct hostapd_data *hapd =
1974 interfaces.iface[i]->bss[j];
1975 hostapd_cleanup(hapd);
1976 if (j == interfaces.iface[i]->num_bss - 1 &&
1978 hostapd_driver_deinit(hapd);
1980 for (j = 0; j < interfaces.iface[i]->num_bss; j++)
1981 os_free(interfaces.iface[i]->bss[j]);
1982 hostapd_cleanup_iface(interfaces.iface[i]);
1984 os_free(interfaces.iface);
1987 tncs_global_deinit();
1988 #endif /* EAP_TNC */
1992 #ifndef CONFIG_NATIVE_WINDOWS
1994 #endif /* CONFIG_NATIVE_WINDOWS */
1996 eap_server_unregister_methods();
1998 os_daemonize_terminate(pid_file);