2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2007, 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_client.h"
35 #include "radius_server.h"
39 #include "vlan_init.h"
40 #include "ctrl_iface.h"
42 #include "eap_sim_db.h"
47 struct hapd_interfaces
{
49 struct hostapd_iface
**iface
;
52 unsigned char rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
55 extern int wpa_debug_level
;
56 extern int wpa_debug_show_keys
;
57 extern int wpa_debug_timestamp
;
60 void hostapd_logger(struct hostapd_data
*hapd
, const u8
*addr
,
61 unsigned int module
, int level
, const char *fmt
, ...)
63 char *format
, *module_str
;
66 int conf_syslog_level
, conf_stdout_level
;
67 unsigned int conf_syslog
, conf_stdout
;
69 maxlen
= strlen(fmt
) + 100;
70 format
= malloc(maxlen
);
74 if (hapd
&& hapd
->conf
) {
75 conf_syslog_level
= hapd
->conf
->logger_syslog_level
;
76 conf_stdout_level
= hapd
->conf
->logger_stdout_level
;
77 conf_syslog
= hapd
->conf
->logger_syslog
;
78 conf_stdout
= hapd
->conf
->logger_stdout
;
80 conf_syslog_level
= conf_stdout_level
= 0;
81 conf_syslog
= conf_stdout
= (unsigned int) -1;
85 case HOSTAPD_MODULE_IEEE80211
:
86 module_str
= "IEEE 802.11";
88 case HOSTAPD_MODULE_IEEE8021X
:
89 module_str
= "IEEE 802.1X";
91 case HOSTAPD_MODULE_RADIUS
:
92 module_str
= "RADIUS";
94 case HOSTAPD_MODULE_WPA
:
97 case HOSTAPD_MODULE_DRIVER
:
98 module_str
= "DRIVER";
100 case HOSTAPD_MODULE_IAPP
:
103 case HOSTAPD_MODULE_MLME
:
111 if (hapd
&& hapd
->conf
&& addr
)
112 snprintf(format
, maxlen
, "%s: STA " MACSTR
"%s%s: %s",
113 hapd
->conf
->iface
, MAC2STR(addr
),
114 module_str
? " " : "", module_str
, fmt
);
115 else if (hapd
&& hapd
->conf
)
116 snprintf(format
, maxlen
, "%s:%s%s %s",
117 hapd
->conf
->iface
, module_str
? " " : "",
120 snprintf(format
, maxlen
, "STA " MACSTR
"%s%s: %s",
121 MAC2STR(addr
), module_str
? " " : "",
124 snprintf(format
, maxlen
, "%s%s%s",
125 module_str
, module_str
? ": " : "", fmt
);
127 if ((conf_stdout
& module
) && level
>= conf_stdout_level
) {
128 wpa_debug_print_timestamp();
135 #ifndef CONFIG_NATIVE_WINDOWS
136 if ((conf_syslog
& module
) && level
>= conf_syslog_level
) {
139 case HOSTAPD_LEVEL_DEBUG_VERBOSE
:
140 case HOSTAPD_LEVEL_DEBUG
:
141 priority
= LOG_DEBUG
;
143 case HOSTAPD_LEVEL_INFO
:
146 case HOSTAPD_LEVEL_NOTICE
:
147 priority
= LOG_NOTICE
;
149 case HOSTAPD_LEVEL_WARNING
:
150 priority
= LOG_WARNING
;
157 vsyslog(priority
, format
, ap
);
160 #endif /* CONFIG_NATIVE_WINDOWS */
166 const char * hostapd_ip_txt(const struct hostapd_ip_addr
*addr
, char *buf
,
169 if (buflen
== 0 || addr
== NULL
)
172 if (addr
->af
== AF_INET
) {
173 snprintf(buf
, buflen
, "%s", inet_ntoa(addr
->u
.v4
));
178 if (addr
->af
== AF_INET6
) {
179 if (inet_ntop(AF_INET6
, &addr
->u
.v6
, buf
, buflen
) == NULL
)
182 #endif /* CONFIG_IPV6 */
188 int hostapd_ip_diff(struct hostapd_ip_addr
*a
, struct hostapd_ip_addr
*b
)
190 if (a
== NULL
&& b
== NULL
)
192 if (a
== NULL
|| b
== NULL
)
197 if (a
->u
.v4
.s_addr
!= b
->u
.v4
.s_addr
)
202 if (memcpy(&a
->u
.v6
, &b
->u
.v6
, sizeof(a
->u
.v6
))
206 #endif /* CONFIG_IPV6 */
213 static void hostapd_deauth_all_stas(struct hostapd_data
*hapd
)
218 memset(addr
, 0xff, ETH_ALEN
);
219 hostapd_sta_deauth(hapd
, addr
, WLAN_REASON_PREV_AUTH_NOT_VALID
);
221 /* New Prism2.5/3 STA firmware versions seem to have issues with this
222 * broadcast deauth frame. This gets the firmware in odd state where
223 * nothing works correctly, so let's skip sending this for a while
224 * until the issue has been resolved. */
230 * hostapd_prune_associations - Remove extraneous associations
231 * @hapd: Pointer to BSS data for the most recent association
232 * @sta: Pointer to the associated STA data
234 * This function looks through all radios and BSS's for previous
235 * (stale) associations of STA. If any are found they are removed.
237 static void hostapd_prune_associations(struct hostapd_data
*hapd
,
238 struct sta_info
*sta
)
240 struct sta_info
*osta
;
241 struct hostapd_data
*ohapd
;
243 struct hapd_interfaces
*interfaces
= eloop_get_user_data();
245 for (i
= 0; i
< interfaces
->count
; i
++) {
246 for (j
= 0; j
< interfaces
->iface
[i
]->num_bss
; j
++) {
247 ohapd
= interfaces
->iface
[i
]->bss
[j
];
250 osta
= ap_get_sta(ohapd
, sta
->addr
);
254 ap_sta_disassociate(ohapd
, osta
,
255 WLAN_REASON_UNSPECIFIED
);
262 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
263 * @hapd: Pointer to BSS data
264 * @sta: Pointer to the associated STA data
265 * @reassoc: 1 to indicate this was a re-association; 0 = first association
267 * This function will be called whenever a station associates with the AP. It
268 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
269 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
270 * authentication and association) internally.
272 void hostapd_new_assoc_sta(struct hostapd_data
*hapd
, struct sta_info
*sta
,
275 if (hapd
->tkip_countermeasures
) {
276 hostapd_sta_deauth(hapd
, sta
->addr
,
277 WLAN_REASON_MICHAEL_MIC_FAILURE
);
281 hostapd_prune_associations(hapd
, sta
);
283 /* IEEE 802.11F (IAPP) */
284 if (hapd
->conf
->ieee802_11f
)
285 iapp_new_station(hapd
->iapp
, sta
);
287 /* Start accounting here, if IEEE 802.1X and WPA are not used.
288 * IEEE 802.1X/WPA code will start accounting after the station has
289 * been authorized. */
290 if (!hapd
->conf
->ieee802_1x
&& !hapd
->conf
->wpa
)
291 accounting_sta_start(hapd
, sta
);
293 hostapd_wme_sta_config(hapd
, sta
);
295 /* Start IEEE 802.1X authentication process for new stations */
296 ieee802_1x_new_station(hapd
, sta
);
298 wpa_auth_sm_event(sta
->wpa_sm
, WPA_REAUTH
);
300 wpa_auth_sta_associated(hapd
->wpa_auth
, sta
->wpa_sm
);
305 static int hostapd_sim_db_cb_sta(struct hostapd_data
*hapd
,
306 struct sta_info
*sta
, void *ctx
)
308 if (eapol_sm_eap_pending_cb(sta
->eapol_sm
, ctx
) == 0)
314 static void hostapd_sim_db_cb(void *ctx
, void *session_ctx
)
316 struct hostapd_data
*hapd
= ctx
;
317 if (ap_for_each_sta(hapd
, hostapd_sim_db_cb_sta
, session_ctx
) == 0)
318 radius_server_eap_pending_cb(hapd
->radius_srv
, session_ctx
);
320 #endif /* EAP_SERVER */
323 static void handle_term(int sig
, void *eloop_ctx
, void *signal_ctx
)
325 printf("Signal %d received - terminating\n", sig
);
330 static void hostapd_wpa_auth_conf(struct hostapd_bss_config
*conf
,
331 struct wpa_auth_config
*wconf
)
333 wconf
->wpa
= conf
->wpa
;
334 wconf
->wpa_key_mgmt
= conf
->wpa_key_mgmt
;
335 wconf
->wpa_pairwise
= conf
->wpa_pairwise
;
336 wconf
->wpa_group
= conf
->wpa_group
;
337 wconf
->wpa_group_rekey
= conf
->wpa_group_rekey
;
338 wconf
->wpa_strict_rekey
= conf
->wpa_strict_rekey
;
339 wconf
->wpa_gmk_rekey
= conf
->wpa_gmk_rekey
;
340 wconf
->rsn_preauth
= conf
->rsn_preauth
;
341 wconf
->eapol_version
= conf
->eapol_version
;
342 wconf
->peerkey
= conf
->peerkey
;
343 wconf
->wme_enabled
= conf
->wme_enabled
;
344 #ifdef CONFIG_IEEE80211W
345 wconf
->ieee80211w
= conf
->ieee80211w
;
346 #endif /* CONFIG_IEEE80211W */
350 #ifndef CONFIG_NATIVE_WINDOWS
351 static void handle_reload(int sig
, void *eloop_ctx
, void *signal_ctx
)
353 struct hapd_interfaces
*hapds
= (struct hapd_interfaces
*) eloop_ctx
;
354 struct hostapd_config
*newconf
;
356 struct wpa_auth_config wpa_auth_conf
;
358 printf("Signal %d received - reloading configuration\n", sig
);
360 for (i
= 0; i
< hapds
->count
; i
++) {
361 struct hostapd_data
*hapd
= hapds
->iface
[i
]->bss
[0];
362 newconf
= hostapd_config_read(hapds
->iface
[i
]->config_fname
);
363 if (newconf
== NULL
) {
364 printf("Failed to read new configuration file - "
365 "continuing with old.\n");
368 /* TODO: update dynamic data based on changed configuration
369 * items (e.g., open/close sockets, remove stations added to
370 * deny list, etc.) */
371 radius_client_flush(hapd
->radius
, 0);
372 hostapd_config_free(hapd
->iconf
);
374 hostapd_wpa_auth_conf(&newconf
->bss
[0], &wpa_auth_conf
);
375 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
377 hapd
->iconf
= newconf
;
378 hapd
->conf
= &newconf
->bss
[0];
379 hapds
->iface
[i
]->conf
= newconf
;
381 if (hostapd_setup_wpa_psk(hapd
->conf
)) {
382 wpa_printf(MSG_ERROR
, "Failed to re-configure WPA PSK "
383 "after reloading configuration");
389 #ifdef HOSTAPD_DUMP_STATE
390 static void hostapd_dump_state(struct hostapd_data
*hapd
)
394 struct sta_info
*sta
;
398 if (!hapd
->conf
->dump_log_name
) {
399 printf("Dump file not defined - ignoring dump request\n");
403 printf("Dumping hostapd state to '%s'\n", hapd
->conf
->dump_log_name
);
404 f
= fopen(hapd
->conf
->dump_log_name
, "w");
406 printf("Could not open dump file '%s' for writing.\n",
407 hapd
->conf
->dump_log_name
);
412 fprintf(f
, "hostapd state dump - %s", ctime(&now
));
413 fprintf(f
, "num_sta=%d num_sta_non_erp=%d "
414 "num_sta_no_short_slot_time=%d\n"
415 "num_sta_no_short_preamble=%d\n",
416 hapd
->num_sta
, hapd
->iface
->num_sta_non_erp
,
417 hapd
->iface
->num_sta_no_short_slot_time
,
418 hapd
->iface
->num_sta_no_short_preamble
);
420 for (sta
= hapd
->sta_list
; sta
!= NULL
; sta
= sta
->next
) {
421 fprintf(f
, "\nSTA=" MACSTR
"\n", MAC2STR(sta
->addr
));
424 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s\n"
425 " capability=0x%x listen_interval=%d\n",
428 (sta
->flags
& WLAN_STA_AUTH
? "[AUTH]" : ""),
429 (sta
->flags
& WLAN_STA_ASSOC
? "[ASSOC]" : ""),
430 (sta
->flags
& WLAN_STA_PS
? "[PS]" : ""),
431 (sta
->flags
& WLAN_STA_TIM
? "[TIM]" : ""),
432 (sta
->flags
& WLAN_STA_PERM
? "[PERM]" : ""),
433 (sta
->flags
& WLAN_STA_AUTHORIZED
? "[AUTHORIZED]" :
435 (sta
->flags
& WLAN_STA_PENDING_POLL
? "[PENDING_POLL" :
437 (sta
->flags
& WLAN_STA_SHORT_PREAMBLE
?
438 "[SHORT_PREAMBLE]" : ""),
439 (sta
->flags
& WLAN_STA_PREAUTH
? "[PREAUTH]" : ""),
440 (sta
->flags
& WLAN_STA_NONERP
? "[NonERP]" : ""),
442 sta
->listen_interval
);
444 fprintf(f
, " supported_rates=");
445 for (i
= 0; i
< sta
->supported_rates_len
; i
++)
446 fprintf(f
, "%02x ", sta
->supported_rates
[i
]);
450 " timeout_next=%s\n",
451 (sta
->timeout_next
== STA_NULLFUNC
? "NULLFUNC POLL" :
452 (sta
->timeout_next
== STA_DISASSOC
? "DISASSOC" :
455 ieee802_1x_dump_state(f
, " ", sta
);
460 int count
= radius_client_get_mib(hapd
->radius
, buf
, 4096);
463 else if (count
> 4095)
466 fprintf(f
, "%s", buf
);
468 count
= radius_server_get_mib(hapd
->radius_srv
, buf
, 4096);
471 else if (count
> 4095)
474 fprintf(f
, "%s", buf
);
479 #endif /* HOSTAPD_DUMP_STATE */
482 static void handle_dump_state(int sig
, void *eloop_ctx
, void *signal_ctx
)
484 #ifdef HOSTAPD_DUMP_STATE
485 struct hapd_interfaces
*hapds
= (struct hapd_interfaces
*) eloop_ctx
;
488 for (i
= 0; i
< hapds
->count
; i
++) {
489 struct hostapd_iface
*hapd_iface
= hapds
->iface
[i
];
490 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
491 hostapd_dump_state(hapd_iface
->bss
[j
]);
493 #endif /* HOSTAPD_DUMP_STATE */
495 #endif /* CONFIG_NATIVE_WINDOWS */
497 static void hostapd_broadcast_key_clear_iface(struct hostapd_data
*hapd
,
502 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
503 if (hostapd_set_encryption(ifname
, hapd
, "none", NULL
, i
, NULL
,
504 0, i
== 0 ? 1 : 0)) {
505 printf("Failed to clear default encryption keys "
506 "(ifname=%s keyidx=%d)\n", ifname
, i
);
512 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
)
514 hostapd_broadcast_key_clear_iface(hapd
, hapd
->conf
->iface
);
519 static int hostapd_broadcast_wep_set(struct hostapd_data
*hapd
)
522 struct hostapd_ssid
*ssid
= &hapd
->conf
->ssid
;
525 if (ssid
->wep
.default_len
&&
526 hostapd_set_encryption(hapd
->conf
->iface
,
527 hapd
, "WEP", NULL
, idx
,
530 idx
== ssid
->wep
.idx
)) {
531 printf("Could not set WEP encryption.\n");
535 if (ssid
->dyn_vlan_keys
) {
537 for (i
= 0; i
<= ssid
->max_dyn_vlan_keys
; i
++) {
539 struct hostapd_wep_keys
*key
= ssid
->dyn_vlan_keys
[i
];
542 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
,
548 if (hostapd_set_encryption(ifname
, hapd
, "WEP", NULL
,
552 printf("Could not set dynamic VLAN WEP "
563 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
564 * @hapd: Pointer to BSS data
566 * This function is used to free all per-BSS data structures and resources.
567 * This gets called in a loop for each BSS between calls to
568 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
569 * is deinitialized. Most of the modules that are initialized in
570 * hostapd_setup_bss() are deinitialized here.
572 static void hostapd_cleanup(struct hostapd_data
*hapd
)
574 hostapd_ctrl_iface_deinit(hapd
);
576 free(hapd
->default_wep_key
);
577 hapd
->default_wep_key
= NULL
;
578 iapp_deinit(hapd
->iapp
);
580 accounting_deinit(hapd
);
581 rsn_preauth_iface_deinit(hapd
);
582 if (hapd
->wpa_auth
) {
583 wpa_deinit(hapd
->wpa_auth
);
584 hapd
->wpa_auth
= NULL
;
586 if (hostapd_set_privacy(hapd
, 0)) {
587 wpa_printf(MSG_DEBUG
, "Could not disable "
588 "PrivacyInvoked for interface %s",
592 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
593 wpa_printf(MSG_DEBUG
, "Could not remove generic "
594 "information element from interface %s",
598 ieee802_1x_deinit(hapd
);
600 hostapd_acl_deinit(hapd
);
601 radius_client_deinit(hapd
->radius
);
603 radius_server_deinit(hapd
->radius_srv
);
604 hapd
->radius_srv
= NULL
;
606 hostapd_wireless_event_deinit(hapd
);
610 tls_deinit(hapd
->ssl_ctx
);
611 hapd
->ssl_ctx
= NULL
;
613 #endif /* EAP_TLS_FUNCS */
616 if (hapd
->eap_sim_db_priv
) {
617 eap_sim_db_deinit(hapd
->eap_sim_db_priv
);
618 hapd
->eap_sim_db_priv
= NULL
;
620 #endif /* EAP_SERVER */
622 if (hapd
->interface_added
&&
623 hostapd_bss_remove(hapd
, hapd
->conf
->iface
)) {
624 printf("Failed to remove BSS interface %s\n",
631 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
632 * @iface: Pointer to interface data
634 * This function is called before per-BSS data structures are deinitialized
635 * with hostapd_cleanup().
637 static void hostapd_cleanup_iface_pre(struct hostapd_iface
*iface
)
643 * hostapd_cleanup_iface - Complete per-interface cleanup
644 * @iface: Pointer to interface data
646 * This function is called after per-BSS data structures are deinitialized
647 * with hostapd_cleanup().
649 static void hostapd_cleanup_iface(struct hostapd_iface
*iface
)
651 hostapd_free_hw_features(iface
->hw_features
, iface
->num_hw_features
);
652 iface
->hw_features
= NULL
;
653 free(iface
->current_rates
);
654 iface
->current_rates
= NULL
;
655 ap_list_deinit(iface
);
656 hostapd_config_free(iface
->conf
);
659 free(iface
->config_fname
);
665 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
)
669 hostapd_broadcast_wep_set(hapd
);
671 if (hapd
->conf
->ssid
.wep
.default_len
)
674 for (i
= 0; i
< 4; i
++) {
675 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
676 hostapd_set_encryption(iface
, hapd
, "WEP", NULL
,
677 i
, hapd
->conf
->ssid
.wep
.key
[i
],
678 hapd
->conf
->ssid
.wep
.len
[i
],
679 i
== hapd
->conf
->ssid
.wep
.idx
)) {
680 printf("Could not set WEP encryption.\n");
683 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
684 i
== hapd
->conf
->ssid
.wep
.idx
)
685 hostapd_set_privacy(hapd
, 1);
692 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
)
696 wpa_printf(MSG_DEBUG
, "Flushing old station entries");
697 if (hostapd_flush(hapd
)) {
698 printf("Could not connect to kernel driver.\n");
701 wpa_printf(MSG_DEBUG
, "Deauthenticate all stations");
702 hostapd_deauth_all_stas(hapd
);
708 static void hostapd_wpa_auth_logger(void *ctx
, const u8
*addr
,
709 logger_level level
, const char *txt
)
711 struct hostapd_data
*hapd
= ctx
;
716 hlevel
= HOSTAPD_LEVEL_WARNING
;
719 hlevel
= HOSTAPD_LEVEL_INFO
;
723 hlevel
= HOSTAPD_LEVEL_DEBUG
;
727 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_WPA
, hlevel
, "%s", txt
);
731 static void hostapd_wpa_auth_disconnect(void *ctx
, const u8
*addr
,
734 struct hostapd_data
*hapd
= ctx
;
735 struct sta_info
*sta
;
737 wpa_printf(MSG_DEBUG
, "%s: WPA authenticator requests disconnect: "
738 "STA " MACSTR
" reason %d",
739 __func__
, MAC2STR(addr
), reason
);
741 sta
= ap_get_sta(hapd
, addr
);
742 hostapd_sta_deauth(hapd
, addr
, reason
);
745 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
| WLAN_STA_AUTHORIZED
);
746 eloop_cancel_timeout(ap_handle_timer
, hapd
, sta
);
747 eloop_register_timeout(0, 0, ap_handle_timer
, hapd
, sta
);
748 sta
->timeout_next
= STA_REMOVE
;
752 static void hostapd_wpa_auth_mic_failure_report(void *ctx
, const u8
*addr
)
754 struct hostapd_data
*hapd
= ctx
;
755 ieee80211_michael_mic_failure(hapd
, addr
, 0);
759 static void hostapd_wpa_auth_set_eapol(void *ctx
, const u8
*addr
,
760 wpa_eapol_variable var
, int value
)
762 struct hostapd_data
*hapd
= ctx
;
763 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
767 case WPA_EAPOL_portEnabled
:
768 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, value
);
770 case WPA_EAPOL_portValid
:
771 ieee802_1x_notify_port_valid(sta
->eapol_sm
, value
);
773 case WPA_EAPOL_authorized
:
774 ieee802_1x_set_sta_authorized(hapd
, sta
, value
);
776 case WPA_EAPOL_portControl_Auto
:
778 sta
->eapol_sm
->portControl
= Auto
;
780 case WPA_EAPOL_keyRun
:
782 sta
->eapol_sm
->keyRun
= value
? TRUE
: FALSE
;
784 case WPA_EAPOL_keyAvailable
:
786 sta
->eapol_sm
->keyAvailable
= value
? TRUE
: FALSE
;
788 case WPA_EAPOL_keyDone
:
790 sta
->eapol_sm
->keyDone
= value
? TRUE
: FALSE
;
792 case WPA_EAPOL_inc_EapolFramesTx
:
794 sta
->eapol_sm
->dot1xAuthEapolFramesTx
++;
800 static int hostapd_wpa_auth_get_eapol(void *ctx
, const u8
*addr
,
801 wpa_eapol_variable var
)
803 struct hostapd_data
*hapd
= ctx
;
804 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
805 if (sta
== NULL
|| sta
->eapol_sm
== NULL
)
808 case WPA_EAPOL_keyRun
:
809 return sta
->eapol_sm
->keyRun
;
810 case WPA_EAPOL_keyAvailable
:
811 return sta
->eapol_sm
->keyAvailable
;
818 static const u8
* hostapd_wpa_auth_get_psk(void *ctx
, const u8
*addr
,
821 struct hostapd_data
*hapd
= ctx
;
822 return hostapd_get_psk(hapd
->conf
, addr
, prev_psk
);
826 static int hostapd_wpa_auth_get_pmk(void *ctx
, const u8
*addr
, u8
*pmk
,
829 struct hostapd_data
*hapd
= ctx
;
832 struct sta_info
*sta
;
834 sta
= ap_get_sta(hapd
, addr
);
838 key
= ieee802_1x_get_key_crypt(sta
->eapol_sm
, &keylen
);
843 keylen
= WPA_PMK_LEN
;
844 memcpy(pmk
, key
, keylen
);
850 static int hostapd_wpa_auth_set_key(void *ctx
, int vlan_id
, const char *alg
,
851 const u8
*addr
, int idx
, u8
*key
,
854 struct hostapd_data
*hapd
= ctx
;
855 const char *ifname
= hapd
->conf
->iface
;
858 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
, vlan_id
);
863 return hostapd_set_encryption(ifname
, hapd
, alg
, addr
, idx
,
868 static int hostapd_wpa_auth_get_seqnum(void *ctx
, const u8
*addr
, int idx
,
871 struct hostapd_data
*hapd
= ctx
;
872 return hostapd_get_seqnum(hapd
->conf
->iface
, hapd
, addr
, idx
, seq
);
876 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx
, const u8
*addr
, int idx
,
879 struct hostapd_data
*hapd
= ctx
;
880 return hostapd_get_seqnum_igtk(hapd
->conf
->iface
, hapd
, addr
, idx
,
885 static int hostapd_wpa_auth_send_eapol(void *ctx
, const u8
*addr
,
886 const u8
*data
, size_t data_len
,
889 struct hostapd_data
*hapd
= ctx
;
890 return hostapd_send_eapol(hapd
, addr
, data
, data_len
, encrypt
);
894 static int hostapd_wpa_auth_for_each_sta(
895 void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
898 struct hostapd_data
*hapd
= ctx
;
899 struct sta_info
*sta
;
901 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
902 if (sta
->wpa_sm
&& cb(sta
->wpa_sm
, cb_ctx
))
910 * hostapd_validate_bssid_configuration - Validate BSSID configuration
911 * @iface: Pointer to interface data
912 * Returns: 0 on success, -1 on failure
914 * This function is used to validate that the configured BSSIDs are valid.
916 static int hostapd_validate_bssid_configuration(struct hostapd_iface
*iface
)
918 u8 mask
[ETH_ALEN
] = { 0 };
919 struct hostapd_data
*hapd
= iface
->bss
[0];
920 unsigned int i
= iface
->conf
->num_bss
, bits
= 0, j
;
923 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
925 /* Determine the bits necessary to cover the number of BSSIDs. */
926 for (i
--; i
; i
>>= 1)
929 /* Determine the bits necessary to any configured BSSIDs,
930 if they are higher than the number of BSSIDs. */
931 for (j
= 0; j
< iface
->conf
->num_bss
; j
++) {
932 if (hostapd_mac_comp_empty(iface
->conf
->bss
[j
].bssid
) == 0)
935 for (i
= 0; i
< ETH_ALEN
; i
++) {
937 iface
->conf
->bss
[j
].bssid
[i
] ^
942 for (i
= 0; i
< ETH_ALEN
&& mask
[i
] == 0; i
++)
948 while (mask
[i
] != 0) {
960 memset(mask
, 0xff, ETH_ALEN
);
962 for (i
= 5; i
> 5 - j
; i
--)
968 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL
, "BSS count %lu, BSSID mask "
969 MACSTR
" (%d bits)\n",
970 (unsigned long) iface
->conf
->num_bss
, MAC2STR(mask
),
973 res
= hostapd_valid_bss_mask(hapd
, hapd
->own_addr
, mask
);
978 printf("Driver did not accept BSSID mask " MACSTR
" for start "
979 "address " MACSTR
".\n",
980 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
984 for (i
= 0; i
< ETH_ALEN
; i
++) {
985 if ((hapd
->own_addr
[i
] & mask
[i
]) != hapd
->own_addr
[i
]) {
986 printf("Invalid BSSID mask " MACSTR
" for start "
987 "address " MACSTR
".\n"
988 "Start address must be the first address in the"
989 " block (i.e., addr AND mask == addr).\n",
990 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
999 static int mac_in_conf(struct hostapd_config
*conf
, const void *a
)
1003 for (i
= 0; i
< conf
->num_bss
; i
++) {
1004 if (hostapd_mac_comp(conf
->bss
[i
].bssid
, a
) == 0) {
1014 * hostapd_setup_bss - Per-BSS setup (initialization)
1015 * @hapd: Pointer to BSS data
1016 * @first: Whether this BSS is the first BSS of an interface
1018 * This function is used to initialize all per-BSS data structures and
1019 * resources. This gets called in a loop for each BSS when an interface is
1020 * initialized. Most of the modules that are initialized here will be
1021 * deinitialized in hostapd_cleanup().
1023 static int hostapd_setup_bss(struct hostapd_data
*hapd
, int first
)
1025 struct hostapd_bss_config
*conf
= hapd
->conf
;
1026 u8 ssid
[HOSTAPD_MAX_SSID_LEN
+ 1];
1027 int ssid_len
, set_ssid
;
1030 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0) {
1031 /* Allocate the next available BSSID. */
1033 inc_byte_array(hapd
->own_addr
, ETH_ALEN
);
1034 } while (mac_in_conf(hapd
->iconf
, hapd
->own_addr
));
1036 /* Allocate the configured BSSID. */
1037 memcpy(hapd
->own_addr
, hapd
->conf
->bssid
, ETH_ALEN
);
1039 if (hostapd_mac_comp(hapd
->own_addr
,
1040 hapd
->iface
->bss
[0]->own_addr
) ==
1042 printf("BSS '%s' may not have BSSID "
1043 "set to the MAC address of the radio\n",
1049 hapd
->interface_added
= 1;
1050 if (hostapd_bss_add(hapd
->iface
->bss
[0], hapd
->conf
->iface
,
1052 printf("Failed to add BSS (BSSID=" MACSTR
")\n",
1053 MAC2STR(hapd
->own_addr
));
1059 * Fetch the SSID from the system and use it or,
1060 * if one was specified in the config file, verify they
1063 ssid_len
= hostapd_get_ssid(hapd
, ssid
, sizeof(ssid
));
1065 printf("Could not read SSID from system\n");
1068 if (conf
->ssid
.ssid_set
) {
1070 * If SSID is specified in the config file and it differs
1071 * from what is being used then force installation of the
1074 set_ssid
= (conf
->ssid
.ssid_len
!= (size_t) ssid_len
||
1075 memcmp(conf
->ssid
.ssid
, ssid
, ssid_len
) != 0);
1078 * No SSID in the config file; just use the one we got
1082 conf
->ssid
.ssid_len
= ssid_len
;
1083 memcpy(conf
->ssid
.ssid
, ssid
, conf
->ssid
.ssid_len
);
1084 conf
->ssid
.ssid
[conf
->ssid
.ssid_len
] = '\0';
1087 printf("Using interface %s with hwaddr " MACSTR
" and ssid '%s'\n",
1088 hapd
->conf
->iface
, MAC2STR(hapd
->own_addr
),
1089 hapd
->conf
->ssid
.ssid
);
1091 if (hostapd_setup_wpa_psk(conf
)) {
1092 printf("WPA-PSK setup failed.\n");
1096 /* Set flag for whether SSID is broadcast in beacons */
1097 if (hostapd_set_broadcast_ssid(hapd
,
1098 !!hapd
->conf
->ignore_broadcast_ssid
)) {
1099 printf("Could not set broadcast SSID flag for kernel "
1104 if (hostapd_set_dtim_period(hapd
, hapd
->conf
->dtim_period
)) {
1105 printf("Could not set DTIM period for kernel driver\n");
1109 /* Set SSID for the kernel driver (to be used in beacon and probe
1110 * response frames) */
1111 if (set_ssid
&& hostapd_set_ssid(hapd
, (u8
*) conf
->ssid
.ssid
,
1112 conf
->ssid
.ssid_len
)) {
1113 printf("Could not set SSID for kernel driver\n");
1117 if (HOSTAPD_DEBUG_COND(HOSTAPD_DEBUG_MSGDUMPS
))
1118 conf
->radius
->msg_dumps
= 1;
1119 hapd
->radius
= radius_client_init(hapd
, conf
->radius
);
1120 if (hapd
->radius
== NULL
) {
1121 printf("RADIUS client initialization failed.\n");
1125 if (hostapd_acl_init(hapd
)) {
1126 printf("ACL initialization failed.\n");
1129 if (ieee802_1x_init(hapd
)) {
1130 printf("IEEE 802.1X initialization failed.\n");
1134 if (hapd
->conf
->wpa
) {
1135 struct wpa_auth_config _conf
;
1136 struct wpa_auth_callbacks cb
;
1140 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
1141 memset(&cb
, 0, sizeof(cb
));
1143 cb
.logger
= hostapd_wpa_auth_logger
;
1144 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
1145 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
1146 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
1147 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
1148 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
1149 cb
.get_pmk
= hostapd_wpa_auth_get_pmk
;
1150 cb
.set_key
= hostapd_wpa_auth_set_key
;
1151 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
1152 cb
.get_seqnum_igtk
= hostapd_wpa_auth_get_seqnum_igtk
;
1153 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
1154 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
1155 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
1156 if (hapd
->wpa_auth
== NULL
) {
1157 printf("WPA initialization failed.\n");
1161 if (hostapd_set_privacy(hapd
, 1)) {
1162 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
1163 "for interface %s", hapd
->conf
->iface
);
1167 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
1168 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
1169 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
1170 "the kernel driver.");
1174 if (rsn_preauth_iface_init(hapd
)) {
1175 printf("Initialization of RSN pre-authentication "
1181 if (accounting_init(hapd
)) {
1182 printf("Accounting initialization failed.\n");
1186 if (hapd
->conf
->ieee802_11f
&&
1187 (hapd
->iapp
= iapp_init(hapd
, hapd
->conf
->iapp_iface
)) == NULL
) {
1188 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1192 if (hostapd_ctrl_iface_init(hapd
)) {
1193 printf("Failed to setup control interface\n");
1197 ieee802_11_set_beacon(hapd
);
1199 if (vlan_init(hapd
)) {
1200 printf("VLAN initialization failed.\n");
1209 * setup_interface2 - Setup (initialize) an interface (part 2)
1210 * @iface: Pointer to interface data.
1211 * Returns: 0 on success; -1 on failure.
1213 * Flushes old stations, sets the channel, DFS parameters, encryption,
1214 * beacons, and WDS links based on the configuration.
1216 static int setup_interface2(struct hostapd_iface
*iface
)
1218 struct hostapd_data
*hapd
= iface
->bss
[0];
1224 hostapd_flush_old_stations(hapd
);
1225 hostapd_set_privacy(hapd
, 0);
1227 if (hapd
->iconf
->channel
) {
1228 freq
= hostapd_hw_get_freq(hapd
, hapd
->iconf
->channel
);
1229 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1230 hostapd_hw_mode_txt(hapd
->iconf
->hw_mode
),
1231 hapd
->iconf
->channel
, freq
);
1233 if (hostapd_set_freq(hapd
, hapd
->iconf
->hw_mode
, freq
)) {
1234 printf("Could not set channel for kernel driver\n");
1239 hostapd_broadcast_wep_clear(hapd
);
1240 if (hostapd_setup_encryption(hapd
->conf
->iface
, hapd
))
1243 hostapd_set_beacon_int(hapd
, hapd
->iconf
->beacon_int
);
1244 ieee802_11_set_beacon(hapd
);
1246 if (hapd
->iconf
->rts_threshold
> -1 &&
1247 hostapd_set_rts(hapd
, hapd
->iconf
->rts_threshold
)) {
1248 printf("Could not set RTS threshold for kernel driver\n");
1252 if (hapd
->iconf
->fragm_threshold
> -1 &&
1253 hostapd_set_frag(hapd
, hapd
->iconf
->fragm_threshold
)) {
1254 printf("Could not set fragmentation threshold for kernel "
1259 prev_addr
= hapd
->own_addr
;
1261 for (j
= 0; j
< iface
->num_bss
; j
++) {
1262 hapd
= iface
->bss
[j
];
1264 memcpy(hapd
->own_addr
, prev_addr
, ETH_ALEN
);
1265 if (hostapd_setup_bss(hapd
, j
== 0))
1267 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0)
1268 prev_addr
= hapd
->own_addr
;
1271 ap_list_init(iface
);
1273 if (hostapd_driver_commit(hapd
) < 0) {
1274 wpa_printf(MSG_ERROR
, "%s: Failed to commit driver "
1275 "configuration", __func__
);
1283 static void setup_interface_start(void *eloop_data
, void *user_ctx
);
1284 static void setup_interface2_handler(void *eloop_data
, void *user_ctx
);
1287 * setup_interface_finalize - Finish setup interface & call the callback
1288 * @iface: Pointer to interface data.
1289 * @status: Status of the setup interface (0 on success; -1 on failure).
1290 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1292 static int setup_interface_finalize(struct hostapd_iface
*iface
, int status
)
1294 hostapd_iface_cb cb
;
1296 if (!iface
->setup_cb
)
1299 eloop_cancel_timeout(setup_interface_start
, iface
, NULL
);
1300 eloop_cancel_timeout(setup_interface2_handler
, iface
, NULL
);
1301 hostapd_select_hw_mode_stop(iface
);
1303 cb
= iface
->setup_cb
;
1305 iface
->setup_cb
= NULL
;
1314 * setup_interface2_wrapper - Wrapper for setup_interface2()
1315 * @iface: Pointer to interface data.
1316 * @status: Status of the hw mode select.
1318 * Wrapper for setup_interface2() to calls finalize function upon completion.
1320 static void setup_interface2_wrapper(struct hostapd_iface
*iface
, int status
)
1324 printf("Could not select hw_mode and channel. (%d)\n", ret
);
1326 ret
= setup_interface2(iface
);
1328 setup_interface_finalize(iface
, ret
);
1333 * setup_interface2_handler - Used for immediate call of setup_interface2
1334 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1335 * @user_ctx: Unused.
1337 static void setup_interface2_handler(void *eloop_data
, void *user_ctx
)
1339 struct hostapd_iface
*iface
= eloop_data
;
1341 setup_interface2_wrapper(iface
, 0);
1346 * setup_interface1 - Setup (initialize) an interface (part 1)
1347 * @iface: Pointer to interface data
1348 * Returns: 0 on success, -1 on failure
1350 * Initializes the driver interface, validates the configuration,
1351 * and sets driver parameters based on the configuration.
1352 * Schedules setup_interface2() to be called immediately or after
1353 * hardware mode setup takes place.
1355 static int setup_interface1(struct hostapd_iface
*iface
)
1357 struct hostapd_data
*hapd
= iface
->bss
[0];
1358 struct hostapd_bss_config
*conf
= hapd
->conf
;
1363 * Initialize the driver interface and make sure that all BSSes get
1364 * configured with a pointer to this driver interface.
1366 if (hostapd_driver_init(hapd
)) {
1367 printf("%s driver initialization failed.\n",
1368 hapd
->driver
? hapd
->driver
->name
: "Unknown");
1369 hapd
->driver
= NULL
;
1372 for (i
= 0; i
< iface
->num_bss
; i
++)
1373 iface
->bss
[i
]->driver
= hapd
->driver
;
1375 if (hostapd_validate_bssid_configuration(iface
))
1378 memcpy(country
, hapd
->iconf
->country
, 3);
1380 if (hostapd_set_country(hapd
, country
) < 0) {
1381 printf("Failed to set country code\n");
1385 if (hapd
->iconf
->ieee80211d
|| hapd
->iconf
->ieee80211h
) {
1386 if (hostapd_set_ieee80211d(hapd
, 1) < 0) {
1387 printf("Failed to set ieee80211d (%d)\n",
1388 hapd
->iconf
->ieee80211d
);
1393 if (hapd
->iconf
->bridge_packets
!= INTERNAL_BRIDGE_DO_NOT_CONTROL
&&
1394 hostapd_set_internal_bridge(hapd
, hapd
->iconf
->bridge_packets
)) {
1395 printf("Failed to set bridge_packets for kernel driver\n");
1399 if (conf
->radius_server_clients
) {
1400 struct radius_server_conf srv
;
1401 memset(&srv
, 0, sizeof(srv
));
1402 srv
.client_file
= conf
->radius_server_clients
;
1403 srv
.auth_port
= conf
->radius_server_auth_port
;
1404 srv
.hostapd_conf
= conf
;
1405 srv
.eap_sim_db_priv
= hapd
->eap_sim_db_priv
;
1406 srv
.ssl_ctx
= hapd
->ssl_ctx
;
1407 srv
.ipv6
= conf
->radius_server_ipv6
;
1408 hapd
->radius_srv
= radius_server_init(&srv
);
1409 if (hapd
->radius_srv
== NULL
) {
1410 printf("RADIUS server initialization failed.\n");
1415 /* TODO: merge with hostapd_driver_init() ? */
1416 if (hostapd_wireless_event_init(hapd
) < 0)
1419 if (hostapd_get_hw_features(iface
)) {
1420 /* Not all drivers support this yet, so continue without hw
1423 return hostapd_select_hw_mode_start(iface
,
1424 setup_interface2_wrapper
);
1427 eloop_register_timeout(0, 0, setup_interface2_handler
, iface
, NULL
);
1433 * setup_interface_start - Handler to start setup interface
1434 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1435 * @user_ctx: Unused.
1437 * An eloop handler is used so that all errors can be processed by the
1438 * callback without introducing stack recursion.
1440 static void setup_interface_start(void *eloop_data
, void *user_ctx
)
1442 struct hostapd_iface
*iface
= eloop_data
;
1446 ret
= setup_interface1(iface
);
1448 setup_interface_finalize(iface
, ret
);
1453 * hostapd_setup_interface_start - Start the setup of an interface
1454 * @iface: Pointer to interface data.
1455 * @cb: The function to callback when done.
1456 * Returns: 0 if it starts successfully; cb will be called when done.
1457 * -1 on failure; cb will not be called.
1459 * Initializes the driver interface, validates the configuration,
1460 * and sets driver parameters based on the configuration.
1461 * Flushes old stations, sets the channel, DFS parameters, encryption,
1462 * beacons, and WDS links based on the configuration.
1464 int hostapd_setup_interface_start(struct hostapd_iface
*iface
,
1465 hostapd_iface_cb cb
)
1467 if (iface
->setup_cb
) {
1468 wpa_printf(MSG_DEBUG
,
1469 "%s: Interface setup already in progress.\n",
1470 iface
->bss
[0]->conf
->iface
);
1474 iface
->setup_cb
= cb
;
1476 eloop_register_timeout(0, 0, setup_interface_start
, iface
, NULL
);
1483 * hostapd_setup_interace_stop - Stops the setup of an interface
1484 * @iface: Pointer to interface data
1485 * Returns: 0 if successfully stopped;
1486 * -1 on failure (i.e., was not in progress)
1488 int hostapd_setup_interface_stop(struct hostapd_iface
*iface
)
1490 return setup_interface_finalize(iface
, -1);
1495 struct driver
*next
;
1497 const struct driver_ops
*ops
;
1499 static struct driver
*drivers
= NULL
;
1501 void driver_register(const char *name
, const struct driver_ops
*ops
)
1505 d
= malloc(sizeof(struct driver
));
1507 printf("Failed to register driver %s!\n", name
);
1510 d
->name
= strdup(name
);
1511 if (d
->name
== NULL
) {
1512 printf("Failed to register driver %s!\n", name
);
1523 void driver_unregister(const char *name
)
1525 struct driver
*p
, **pp
;
1527 for (pp
= &drivers
; (p
= *pp
) != NULL
; pp
= &p
->next
) {
1528 if (strcasecmp(p
->name
, name
) == 0) {
1539 static void driver_unregister_all(void)
1541 struct driver
*p
, *pp
;
1553 const struct driver_ops
* driver_lookup(const char *name
)
1557 if (strcmp(name
, "default") == 0) {
1559 while (p
&& p
->next
)
1564 for (p
= drivers
; p
!= NULL
; p
= p
->next
) {
1565 if (strcasecmp(p
->name
, name
) == 0)
1573 static void show_version(void)
1576 "hostapd v" VERSION_STR
"\n"
1577 "User space daemon for IEEE 802.11 AP management,\n"
1578 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1579 "Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi> "
1580 "and contributors\n");
1584 static void usage(void)
1589 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1590 "<configuration file(s)>\n"
1593 " -h show this usage\n"
1594 " -d show more debug messages (-dd for even more)\n"
1595 " -B run daemon in the background\n"
1597 " -K include key data in debug messages\n"
1598 " -t include timestamps in some debug messages\n"
1599 " -v show hostapd version\n");
1606 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1607 * @hapd_iface: Pointer to interface data
1608 * @conf: Pointer to per-interface configuration
1609 * @bss: Pointer to per-BSS configuration for this BSS
1610 * Returns: Pointer to allocated BSS data
1612 * This function is used to allocate per-BSS data structure. This data will be
1613 * freed after hostapd_cleanup() is called for it during interface
1616 static struct hostapd_data
*
1617 hostapd_alloc_bss_data(struct hostapd_iface
*hapd_iface
,
1618 struct hostapd_config
*conf
,
1619 struct hostapd_bss_config
*bss
)
1621 struct hostapd_data
*hapd
;
1623 hapd
= wpa_zalloc(sizeof(*hapd
));
1629 hapd
->iface
= hapd_iface
;
1631 if (hapd
->conf
->individual_wep_key_len
> 0) {
1632 /* use key0 in individual key and key1 in broadcast key */
1633 hapd
->default_wep_key_idx
= 1;
1636 #ifdef EAP_TLS_FUNCS
1637 if (hapd
->conf
->eap_server
&&
1638 (hapd
->conf
->ca_cert
|| hapd
->conf
->server_cert
)) {
1639 struct tls_connection_params params
;
1641 hapd
->ssl_ctx
= tls_init(NULL
);
1642 if (hapd
->ssl_ctx
== NULL
) {
1643 printf("Failed to initialize TLS\n");
1647 memset(¶ms
, 0, sizeof(params
));
1648 params
.ca_cert
= hapd
->conf
->ca_cert
;
1649 params
.client_cert
= hapd
->conf
->server_cert
;
1650 params
.private_key
= hapd
->conf
->private_key
;
1651 params
.private_key_passwd
= hapd
->conf
->private_key_passwd
;
1653 if (tls_global_set_params(hapd
->ssl_ctx
, ¶ms
)) {
1654 printf("Failed to set TLS parameters\n");
1658 if (tls_global_set_verify(hapd
->ssl_ctx
,
1659 hapd
->conf
->check_crl
)) {
1660 printf("Failed to enable check_crl\n");
1664 #endif /* EAP_TLS_FUNCS */
1667 if (hapd
->conf
->eap_sim_db
) {
1668 hapd
->eap_sim_db_priv
=
1669 eap_sim_db_init(hapd
->conf
->eap_sim_db
,
1670 hostapd_sim_db_cb
, hapd
);
1671 if (hapd
->eap_sim_db_priv
== NULL
) {
1672 printf("Failed to initialize EAP-SIM database "
1677 #endif /* EAP_SERVER */
1679 if (hapd
->conf
->assoc_ap
)
1680 hapd
->assoc_ap_state
= WAIT_BEACON
;
1682 /* FIX: need to fix this const vs. not */
1683 hapd
->driver
= (struct driver_ops
*) hapd
->iconf
->driver
;
1687 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1690 /* TODO: cleanup allocated resources(?) */
1697 * hostapd_init - Allocate and initialize per-interface data
1698 * @config_file: Path to the configuration file
1699 * Returns: Pointer to the allocated interface data or %NULL on failure
1701 * This function is used to allocate main data structures for per-interface
1702 * data. The allocated data buffer will be freed by calling
1703 * hostapd_cleanup_iface().
1705 static struct hostapd_iface
* hostapd_init(const char *config_file
)
1707 struct hostapd_iface
*hapd_iface
= NULL
;
1708 struct hostapd_config
*conf
= NULL
;
1709 struct hostapd_data
*hapd
;
1712 hapd_iface
= wpa_zalloc(sizeof(*hapd_iface
));
1713 if (hapd_iface
== NULL
)
1716 hapd_iface
->config_fname
= strdup(config_file
);
1717 if (hapd_iface
->config_fname
== NULL
)
1720 conf
= hostapd_config_read(hapd_iface
->config_fname
);
1723 hapd_iface
->conf
= conf
;
1725 hapd_iface
->num_bss
= conf
->num_bss
;
1726 hapd_iface
->bss
= wpa_zalloc(conf
->num_bss
*
1727 sizeof(struct hostapd_data
*));
1728 if (hapd_iface
->bss
== NULL
)
1731 for (i
= 0; i
< conf
->num_bss
; i
++) {
1732 hapd
= hapd_iface
->bss
[i
] =
1733 hostapd_alloc_bss_data(hapd_iface
, conf
,
1743 hostapd_config_free(conf
);
1745 for (i
= 0; hapd_iface
->bss
&& i
< hapd_iface
->num_bss
; i
++) {
1746 hapd
= hapd_iface
->bss
[i
];
1747 if (hapd
&& hapd
->ssl_ctx
)
1748 tls_deinit(hapd
->ssl_ctx
);
1751 free(hapd_iface
->config_fname
);
1752 free(hapd_iface
->bss
);
1760 * register_drivers - Register driver interfaces
1762 * This function is generated by Makefile (into driver_conf.c) to call all
1763 * configured driver interfaces to register them to core hostapd.
1765 void register_drivers(void);
1769 * setup_interface_done - Callback when an interface is done being setup.
1770 * @iface: Pointer to interface data.
1771 * @status: Status of the interface setup (0 on success; -1 on failure).
1773 static void setup_interface_done(struct hostapd_iface
*iface
, int status
)
1776 wpa_printf(MSG_DEBUG
, "%s: Unable to setup interface.",
1777 iface
->bss
[0]->conf
->iface
);
1780 wpa_printf(MSG_DEBUG
, "%s: Setup of interface done.",
1781 iface
->bss
[0]->conf
->iface
);
1785 int main(int argc
, char *argv
[])
1787 struct hapd_interfaces interfaces
;
1790 int c
, debug
= 0, daemonize
= 0;
1791 const char *pid_file
= NULL
;
1794 c
= getopt(argc
, argv
, "BdhKP:tv");
1803 if (wpa_debug_level
> 0)
1810 wpa_debug_show_keys
++;
1816 wpa_debug_timestamp
++;
1832 register_drivers(); /* NB: generated by Makefile */
1834 if (eap_server_register_methods()) {
1835 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
1839 interfaces
.count
= argc
- optind
;
1841 interfaces
.iface
= malloc(interfaces
.count
*
1842 sizeof(struct hostapd_iface
*));
1843 if (interfaces
.iface
== NULL
) {
1844 printf("malloc failed\n");
1848 if (eloop_init(&interfaces
)) {
1849 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1853 #ifndef CONFIG_NATIVE_WINDOWS
1854 eloop_register_signal(SIGHUP
, handle_reload
, NULL
);
1855 eloop_register_signal(SIGUSR1
, handle_dump_state
, NULL
);
1856 #endif /* CONFIG_NATIVE_WINDOWS */
1857 eloop_register_signal_terminate(handle_term
, NULL
);
1859 /* Initialize interfaces */
1860 for (i
= 0; i
< interfaces
.count
; i
++) {
1861 printf("Configuration file: %s\n", argv
[optind
+ i
]);
1862 interfaces
.iface
[i
] = hostapd_init(argv
[optind
+ i
]);
1863 if (!interfaces
.iface
[i
])
1865 for (k
= 0; k
< debug
; k
++) {
1866 if (interfaces
.iface
[i
]->bss
[0]->conf
->
1867 logger_stdout_level
> 0)
1868 interfaces
.iface
[i
]->bss
[0]->conf
->
1869 logger_stdout_level
--;
1870 interfaces
.iface
[i
]->bss
[0]->conf
->debug
++;
1873 ret
= hostapd_setup_interface_start(interfaces
.iface
[i
],
1874 setup_interface_done
);
1879 if (daemonize
&& os_daemonize(pid_file
)) {
1884 #ifndef CONFIG_NATIVE_WINDOWS
1885 openlog("hostapd", 0, LOG_DAEMON
);
1886 #endif /* CONFIG_NATIVE_WINDOWS */
1890 /* Disconnect associated stations from all interfaces and BSSes */
1891 for (i
= 0; i
< interfaces
.count
; i
++) {
1892 for (j
= 0; j
< interfaces
.iface
[i
]->num_bss
; j
++) {
1893 struct hostapd_data
*hapd
=
1894 interfaces
.iface
[i
]->bss
[j
];
1895 hostapd_free_stas(hapd
);
1896 hostapd_flush_old_stations(hapd
);
1903 /* Deinitialize all interfaces */
1904 for (i
= 0; i
< interfaces
.count
; i
++) {
1905 if (!interfaces
.iface
[i
])
1907 hostapd_setup_interface_stop(interfaces
.iface
[i
]);
1908 hostapd_cleanup_iface_pre(interfaces
.iface
[i
]);
1909 for (j
= 0; j
< interfaces
.iface
[i
]->num_bss
; j
++) {
1910 struct hostapd_data
*hapd
=
1911 interfaces
.iface
[i
]->bss
[j
];
1912 hostapd_cleanup(hapd
);
1913 if (j
== interfaces
.iface
[i
]->num_bss
- 1 &&
1915 hostapd_driver_deinit(hapd
);
1917 for (j
= 0; j
< interfaces
.iface
[i
]->num_bss
; j
++)
1918 free(interfaces
.iface
[i
]->bss
[j
]);
1919 hostapd_cleanup_iface(interfaces
.iface
[i
]);
1921 free(interfaces
.iface
);
1925 #ifndef CONFIG_NATIVE_WINDOWS
1927 #endif /* CONFIG_NATIVE_WINDOWS */
1929 eap_server_unregister_methods();
1931 driver_unregister_all();
1933 os_daemonize_terminate(pid_file
);