2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
16 #ifndef CONFIG_NATIVE_WINDOWS
18 #endif /* CONFIG_NATIVE_WINDOWS */
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
25 #include "hw_features.h"
26 #include "accounting.h"
30 #include "ieee802_11_auth.h"
34 #include "radius/radius_client.h"
35 #include "radius/radius_server.h"
39 #include "vlan_init.h"
40 #include "ctrl_iface.h"
42 #include "eap_server/eap_sim_db.h"
43 #include "eap_server/eap.h"
44 #include "eap_server/tncs.h"
46 #include "l2_packet/l2_packet.h"
49 static int hostapd_radius_get_eap_user(void *ctx
, const u8
*identity
,
50 size_t identity_len
, int phase2
,
51 struct eap_user
*user
);
53 struct hapd_interfaces
{
55 struct hostapd_iface
**iface
;
58 unsigned char rfc1042_header
[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
61 extern int wpa_debug_level
;
62 extern int wpa_debug_show_keys
;
63 extern int wpa_debug_timestamp
;
66 static void hostapd_logger_cb(void *ctx
, const u8
*addr
, unsigned int module
,
67 int level
, const char *txt
, size_t len
)
69 struct hostapd_data
*hapd
= ctx
;
70 char *format
, *module_str
;
72 int conf_syslog_level
, conf_stdout_level
;
73 unsigned int conf_syslog
, conf_stdout
;
76 format
= os_malloc(maxlen
);
80 if (hapd
&& hapd
->conf
) {
81 conf_syslog_level
= hapd
->conf
->logger_syslog_level
;
82 conf_stdout_level
= hapd
->conf
->logger_stdout_level
;
83 conf_syslog
= hapd
->conf
->logger_syslog
;
84 conf_stdout
= hapd
->conf
->logger_stdout
;
86 conf_syslog_level
= conf_stdout_level
= 0;
87 conf_syslog
= conf_stdout
= (unsigned int) -1;
91 case HOSTAPD_MODULE_IEEE80211
:
92 module_str
= "IEEE 802.11";
94 case HOSTAPD_MODULE_IEEE8021X
:
95 module_str
= "IEEE 802.1X";
97 case HOSTAPD_MODULE_RADIUS
:
98 module_str
= "RADIUS";
100 case HOSTAPD_MODULE_WPA
:
103 case HOSTAPD_MODULE_DRIVER
:
104 module_str
= "DRIVER";
106 case HOSTAPD_MODULE_IAPP
:
109 case HOSTAPD_MODULE_MLME
:
117 if (hapd
&& hapd
->conf
&& addr
)
118 os_snprintf(format
, maxlen
, "%s: STA " MACSTR
"%s%s: %s",
119 hapd
->conf
->iface
, MAC2STR(addr
),
120 module_str
? " " : "", module_str
, txt
);
121 else if (hapd
&& hapd
->conf
)
122 os_snprintf(format
, maxlen
, "%s:%s%s %s",
123 hapd
->conf
->iface
, module_str
? " " : "",
126 os_snprintf(format
, maxlen
, "STA " MACSTR
"%s%s: %s",
127 MAC2STR(addr
), module_str
? " " : "",
130 os_snprintf(format
, maxlen
, "%s%s%s",
131 module_str
, module_str
? ": " : "", txt
);
133 if ((conf_stdout
& module
) && level
>= conf_stdout_level
) {
134 wpa_debug_print_timestamp();
135 printf("%s\n", format
);
138 #ifndef CONFIG_NATIVE_WINDOWS
139 if ((conf_syslog
& module
) && level
>= conf_syslog_level
) {
142 case HOSTAPD_LEVEL_DEBUG_VERBOSE
:
143 case HOSTAPD_LEVEL_DEBUG
:
144 priority
= LOG_DEBUG
;
146 case HOSTAPD_LEVEL_INFO
:
149 case HOSTAPD_LEVEL_NOTICE
:
150 priority
= LOG_NOTICE
;
152 case HOSTAPD_LEVEL_WARNING
:
153 priority
= LOG_WARNING
;
159 syslog(priority
, "%s", format
);
161 #endif /* CONFIG_NATIVE_WINDOWS */
167 static void hostapd_deauth_all_stas(struct hostapd_data
*hapd
)
171 /* New Prism2.5/3 STA firmware versions seem to have issues with this
172 * broadcast deauth frame. This gets the firmware in odd state where
173 * nothing works correctly, so let's skip sending this for the hostap
176 if (hapd
->driver
&& os_strcmp(hapd
->driver
->name
, "hostap") != 0) {
177 os_memset(addr
, 0xff, ETH_ALEN
);
178 hostapd_sta_deauth(hapd
, addr
,
179 WLAN_REASON_PREV_AUTH_NOT_VALID
);
185 * hostapd_prune_associations - Remove extraneous associations
186 * @hapd: Pointer to BSS data for the most recent association
187 * @sta: Pointer to the associated STA data
189 * This function looks through all radios and BSS's for previous
190 * (stale) associations of STA. If any are found they are removed.
192 static void hostapd_prune_associations(struct hostapd_data
*hapd
,
193 struct sta_info
*sta
)
195 struct sta_info
*osta
;
196 struct hostapd_data
*ohapd
;
198 struct hapd_interfaces
*interfaces
= eloop_get_user_data();
200 for (i
= 0; i
< interfaces
->count
; i
++) {
201 for (j
= 0; j
< interfaces
->iface
[i
]->num_bss
; j
++) {
202 ohapd
= interfaces
->iface
[i
]->bss
[j
];
205 osta
= ap_get_sta(ohapd
, sta
->addr
);
209 ap_sta_disassociate(ohapd
, osta
,
210 WLAN_REASON_UNSPECIFIED
);
217 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
218 * @hapd: Pointer to BSS data
219 * @sta: Pointer to the associated STA data
220 * @reassoc: 1 to indicate this was a re-association; 0 = first association
222 * This function will be called whenever a station associates with the AP. It
223 * can be called for ieee802_11.c for drivers that export MLME to hostapd and
224 * from driver_*.c for drivers that take care of management frames (IEEE 802.11
225 * authentication and association) internally.
227 void hostapd_new_assoc_sta(struct hostapd_data
*hapd
, struct sta_info
*sta
,
230 if (hapd
->tkip_countermeasures
) {
231 hostapd_sta_deauth(hapd
, sta
->addr
,
232 WLAN_REASON_MICHAEL_MIC_FAILURE
);
236 hostapd_prune_associations(hapd
, sta
);
238 /* IEEE 802.11F (IAPP) */
239 if (hapd
->conf
->ieee802_11f
)
240 iapp_new_station(hapd
->iapp
, sta
);
242 /* Start accounting here, if IEEE 802.1X and WPA are not used.
243 * IEEE 802.1X/WPA code will start accounting after the station has
244 * been authorized. */
245 if (!hapd
->conf
->ieee802_1x
&& !hapd
->conf
->wpa
)
246 accounting_sta_start(hapd
, sta
);
248 hostapd_wme_sta_config(hapd
, sta
);
250 /* Start IEEE 802.1X authentication process for new stations */
251 ieee802_1x_new_station(hapd
, sta
);
253 if (sta
->auth_alg
!= WLAN_AUTH_FT
)
254 wpa_auth_sm_event(sta
->wpa_sm
, WPA_REAUTH
);
256 wpa_auth_sta_associated(hapd
->wpa_auth
, sta
->wpa_sm
);
261 static int hostapd_sim_db_cb_sta(struct hostapd_data
*hapd
,
262 struct sta_info
*sta
, void *ctx
)
264 if (eapol_auth_eap_pending_cb(sta
->eapol_sm
, ctx
) == 0)
270 static void hostapd_sim_db_cb(void *ctx
, void *session_ctx
)
272 struct hostapd_data
*hapd
= ctx
;
273 if (ap_for_each_sta(hapd
, hostapd_sim_db_cb_sta
, session_ctx
) == 0)
274 radius_server_eap_pending_cb(hapd
->radius_srv
, session_ctx
);
276 #endif /* EAP_SERVER */
279 static void handle_term(int sig
, void *eloop_ctx
, void *signal_ctx
)
281 printf("Signal %d received - terminating\n", sig
);
286 static void hostapd_wpa_auth_conf(struct hostapd_bss_config
*conf
,
287 struct wpa_auth_config
*wconf
)
289 wconf
->wpa
= conf
->wpa
;
290 wconf
->wpa_key_mgmt
= conf
->wpa_key_mgmt
;
291 wconf
->wpa_pairwise
= conf
->wpa_pairwise
;
292 wconf
->wpa_group
= conf
->wpa_group
;
293 wconf
->wpa_group_rekey
= conf
->wpa_group_rekey
;
294 wconf
->wpa_strict_rekey
= conf
->wpa_strict_rekey
;
295 wconf
->wpa_gmk_rekey
= conf
->wpa_gmk_rekey
;
296 wconf
->rsn_pairwise
= conf
->rsn_pairwise
;
297 wconf
->rsn_preauth
= conf
->rsn_preauth
;
298 wconf
->eapol_version
= conf
->eapol_version
;
299 wconf
->peerkey
= conf
->peerkey
;
300 wconf
->wme_enabled
= conf
->wme_enabled
;
301 wconf
->okc
= conf
->okc
;
302 #ifdef CONFIG_IEEE80211W
303 wconf
->ieee80211w
= conf
->ieee80211w
;
304 #endif /* CONFIG_IEEE80211W */
305 #ifdef CONFIG_IEEE80211R
306 wconf
->ssid_len
= conf
->ssid
.ssid_len
;
307 if (wconf
->ssid_len
> SSID_LEN
)
308 wconf
->ssid_len
= SSID_LEN
;
309 os_memcpy(wconf
->ssid
, conf
->ssid
.ssid
, wconf
->ssid_len
);
310 os_memcpy(wconf
->mobility_domain
, conf
->mobility_domain
,
311 MOBILITY_DOMAIN_ID_LEN
);
312 if (conf
->nas_identifier
&&
313 os_strlen(conf
->nas_identifier
) <= FT_R0KH_ID_MAX_LEN
) {
314 wconf
->r0_key_holder_len
= os_strlen(conf
->nas_identifier
);
315 os_memcpy(wconf
->r0_key_holder
, conf
->nas_identifier
,
316 wconf
->r0_key_holder_len
);
318 os_memcpy(wconf
->r1_key_holder
, conf
->r1_key_holder
, FT_R1KH_ID_LEN
);
319 wconf
->r0_key_lifetime
= conf
->r0_key_lifetime
;
320 wconf
->reassociation_deadline
= conf
->reassociation_deadline
;
321 wconf
->r0kh_list
= conf
->r0kh_list
;
322 wconf
->r1kh_list
= conf
->r1kh_list
;
323 wconf
->pmk_r1_push
= conf
->pmk_r1_push
;
324 #endif /* CONFIG_IEEE80211R */
328 #ifndef CONFIG_NATIVE_WINDOWS
329 static void handle_reload(int sig
, void *eloop_ctx
, void *signal_ctx
)
331 struct hapd_interfaces
*hapds
= (struct hapd_interfaces
*) eloop_ctx
;
332 struct hostapd_config
*newconf
;
334 struct wpa_auth_config wpa_auth_conf
;
336 printf("Signal %d received - reloading configuration\n", sig
);
338 for (i
= 0; i
< hapds
->count
; i
++) {
339 struct hostapd_data
*hapd
= hapds
->iface
[i
]->bss
[0];
340 newconf
= hostapd_config_read(hapds
->iface
[i
]->config_fname
);
341 if (newconf
== NULL
) {
342 printf("Failed to read new configuration file - "
343 "continuing with old.\n");
346 /* TODO: update dynamic data based on changed configuration
347 * items (e.g., open/close sockets, remove stations added to
348 * deny list, etc.) */
349 radius_client_flush(hapd
->radius
, 0);
350 hostapd_config_free(hapd
->iconf
);
352 hostapd_wpa_auth_conf(&newconf
->bss
[0], &wpa_auth_conf
);
353 wpa_reconfig(hapd
->wpa_auth
, &wpa_auth_conf
);
355 hapd
->iconf
= newconf
;
356 hapd
->conf
= &newconf
->bss
[0];
357 hapds
->iface
[i
]->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");
367 #ifdef HOSTAPD_DUMP_STATE
368 static void hostapd_dump_state(struct hostapd_data
*hapd
)
372 struct sta_info
*sta
;
376 if (!hapd
->conf
->dump_log_name
) {
377 printf("Dump file not defined - ignoring dump request\n");
381 printf("Dumping hostapd state to '%s'\n", hapd
->conf
->dump_log_name
);
382 f
= fopen(hapd
->conf
->dump_log_name
, "w");
384 printf("Could not open dump file '%s' for writing.\n",
385 hapd
->conf
->dump_log_name
);
390 fprintf(f
, "hostapd state dump - %s", ctime(&now
));
391 fprintf(f
, "num_sta=%d num_sta_non_erp=%d "
392 "num_sta_no_short_slot_time=%d\n"
393 "num_sta_no_short_preamble=%d\n",
394 hapd
->num_sta
, hapd
->iface
->num_sta_non_erp
,
395 hapd
->iface
->num_sta_no_short_slot_time
,
396 hapd
->iface
->num_sta_no_short_preamble
);
398 for (sta
= hapd
->sta_list
; sta
!= NULL
; sta
= sta
->next
) {
399 fprintf(f
, "\nSTA=" MACSTR
"\n", MAC2STR(sta
->addr
));
402 " AID=%d flags=0x%x %s%s%s%s%s%s%s%s%s%s%s%s\n"
403 " capability=0x%x listen_interval=%d\n",
406 (sta
->flags
& WLAN_STA_AUTH
? "[AUTH]" : ""),
407 (sta
->flags
& WLAN_STA_ASSOC
? "[ASSOC]" : ""),
408 (sta
->flags
& WLAN_STA_PS
? "[PS]" : ""),
409 (sta
->flags
& WLAN_STA_TIM
? "[TIM]" : ""),
410 (sta
->flags
& WLAN_STA_PERM
? "[PERM]" : ""),
411 (sta
->flags
& WLAN_STA_AUTHORIZED
? "[AUTHORIZED]" :
413 (sta
->flags
& WLAN_STA_PENDING_POLL
? "[PENDING_POLL" :
415 (sta
->flags
& WLAN_STA_SHORT_PREAMBLE
?
416 "[SHORT_PREAMBLE]" : ""),
417 (sta
->flags
& WLAN_STA_PREAUTH
? "[PREAUTH]" : ""),
418 (sta
->flags
& WLAN_STA_WME
? "[WME]" : ""),
419 (sta
->flags
& WLAN_STA_MFP
? "[MFP]" : ""),
420 (sta
->flags
& WLAN_STA_NONERP
? "[NonERP]" : ""),
422 sta
->listen_interval
);
424 fprintf(f
, " supported_rates=");
425 for (i
= 0; i
< sta
->supported_rates_len
; i
++)
426 fprintf(f
, "%02x ", sta
->supported_rates
[i
]);
430 " timeout_next=%s\n",
431 (sta
->timeout_next
== STA_NULLFUNC
? "NULLFUNC POLL" :
432 (sta
->timeout_next
== STA_DISASSOC
? "DISASSOC" :
435 ieee802_1x_dump_state(f
, " ", sta
);
438 buf
= os_malloc(4096);
440 int count
= radius_client_get_mib(hapd
->radius
, buf
, 4096);
443 else if (count
> 4095)
446 fprintf(f
, "%s", buf
);
448 count
= radius_server_get_mib(hapd
->radius_srv
, buf
, 4096);
451 else if (count
> 4095)
454 fprintf(f
, "%s", buf
);
459 #endif /* HOSTAPD_DUMP_STATE */
462 static void handle_dump_state(int sig
, void *eloop_ctx
, void *signal_ctx
)
464 #ifdef HOSTAPD_DUMP_STATE
465 struct hapd_interfaces
*hapds
= (struct hapd_interfaces
*) eloop_ctx
;
468 for (i
= 0; i
< hapds
->count
; i
++) {
469 struct hostapd_iface
*hapd_iface
= hapds
->iface
[i
];
470 for (j
= 0; j
< hapd_iface
->num_bss
; j
++)
471 hostapd_dump_state(hapd_iface
->bss
[j
]);
473 #endif /* HOSTAPD_DUMP_STATE */
475 #endif /* CONFIG_NATIVE_WINDOWS */
477 static void hostapd_broadcast_key_clear_iface(struct hostapd_data
*hapd
,
482 for (i
= 0; i
< NUM_WEP_KEYS
; i
++) {
483 if (hostapd_set_encryption(ifname
, hapd
, "none", NULL
, i
, NULL
,
484 0, i
== 0 ? 1 : 0)) {
485 printf("Failed to clear default encryption keys "
486 "(ifname=%s keyidx=%d)\n", ifname
, i
);
492 static int hostapd_broadcast_wep_clear(struct hostapd_data
*hapd
)
494 hostapd_broadcast_key_clear_iface(hapd
, hapd
->conf
->iface
);
499 static int hostapd_broadcast_wep_set(struct hostapd_data
*hapd
)
502 struct hostapd_ssid
*ssid
= &hapd
->conf
->ssid
;
505 if (ssid
->wep
.default_len
&&
506 hostapd_set_encryption(hapd
->conf
->iface
,
507 hapd
, "WEP", NULL
, idx
,
510 idx
== ssid
->wep
.idx
)) {
511 printf("Could not set WEP encryption.\n");
515 if (ssid
->dyn_vlan_keys
) {
517 for (i
= 0; i
<= ssid
->max_dyn_vlan_keys
; i
++) {
519 struct hostapd_wep_keys
*key
= ssid
->dyn_vlan_keys
[i
];
522 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
,
528 if (hostapd_set_encryption(ifname
, hapd
, "WEP", NULL
,
532 printf("Could not set dynamic VLAN WEP "
543 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
544 * @hapd: Pointer to BSS data
546 * This function is used to free all per-BSS data structures and resources.
547 * This gets called in a loop for each BSS between calls to
548 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
549 * is deinitialized. Most of the modules that are initialized in
550 * hostapd_setup_bss() are deinitialized here.
552 static void hostapd_cleanup(struct hostapd_data
*hapd
)
554 hostapd_ctrl_iface_deinit(hapd
);
556 os_free(hapd
->default_wep_key
);
557 hapd
->default_wep_key
= NULL
;
558 iapp_deinit(hapd
->iapp
);
560 accounting_deinit(hapd
);
561 rsn_preauth_iface_deinit(hapd
);
562 if (hapd
->wpa_auth
) {
563 wpa_deinit(hapd
->wpa_auth
);
564 hapd
->wpa_auth
= NULL
;
566 if (hostapd_set_privacy(hapd
, 0)) {
567 wpa_printf(MSG_DEBUG
, "Could not disable "
568 "PrivacyInvoked for interface %s",
572 if (hostapd_set_generic_elem(hapd
, (u8
*) "", 0)) {
573 wpa_printf(MSG_DEBUG
, "Could not remove generic "
574 "information element from interface %s",
578 ieee802_1x_deinit(hapd
);
580 hostapd_acl_deinit(hapd
);
581 radius_client_deinit(hapd
->radius
);
583 radius_server_deinit(hapd
->radius_srv
);
584 hapd
->radius_srv
= NULL
;
586 #ifdef CONFIG_IEEE80211R
587 l2_packet_deinit(hapd
->l2
);
588 #endif /* CONFIG_IEEE80211R */
590 hostapd_wireless_event_deinit(hapd
);
594 tls_deinit(hapd
->ssl_ctx
);
595 hapd
->ssl_ctx
= NULL
;
597 #endif /* EAP_TLS_FUNCS */
600 if (hapd
->eap_sim_db_priv
) {
601 eap_sim_db_deinit(hapd
->eap_sim_db_priv
);
602 hapd
->eap_sim_db_priv
= NULL
;
604 #endif /* EAP_SERVER */
606 if (hapd
->interface_added
&&
607 hostapd_bss_remove(hapd
, hapd
->conf
->iface
)) {
608 printf("Failed to remove BSS interface %s\n",
615 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
616 * @iface: Pointer to interface data
618 * This function is called before per-BSS data structures are deinitialized
619 * with hostapd_cleanup().
621 static void hostapd_cleanup_iface_pre(struct hostapd_iface
*iface
)
627 * hostapd_cleanup_iface - Complete per-interface cleanup
628 * @iface: Pointer to interface data
630 * This function is called after per-BSS data structures are deinitialized
631 * with hostapd_cleanup().
633 static void hostapd_cleanup_iface(struct hostapd_iface
*iface
)
635 hostapd_free_hw_features(iface
->hw_features
, iface
->num_hw_features
);
636 iface
->hw_features
= NULL
;
637 os_free(iface
->current_rates
);
638 iface
->current_rates
= NULL
;
639 ap_list_deinit(iface
);
640 hostapd_config_free(iface
->conf
);
643 os_free(iface
->config_fname
);
649 static int hostapd_setup_encryption(char *iface
, struct hostapd_data
*hapd
)
653 hostapd_broadcast_wep_set(hapd
);
655 if (hapd
->conf
->ssid
.wep
.default_len
)
658 for (i
= 0; i
< 4; i
++) {
659 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
660 hostapd_set_encryption(iface
, hapd
, "WEP", NULL
,
661 i
, hapd
->conf
->ssid
.wep
.key
[i
],
662 hapd
->conf
->ssid
.wep
.len
[i
],
663 i
== hapd
->conf
->ssid
.wep
.idx
)) {
664 printf("Could not set WEP encryption.\n");
667 if (hapd
->conf
->ssid
.wep
.key
[i
] &&
668 i
== hapd
->conf
->ssid
.wep
.idx
)
669 hostapd_set_privacy(hapd
, 1);
676 static int hostapd_flush_old_stations(struct hostapd_data
*hapd
)
680 wpa_printf(MSG_DEBUG
, "Flushing old station entries");
681 if (hostapd_flush(hapd
)) {
682 printf("Could not connect to kernel driver.\n");
685 wpa_printf(MSG_DEBUG
, "Deauthenticate all stations");
686 hostapd_deauth_all_stas(hapd
);
692 static void hostapd_wpa_auth_logger(void *ctx
, const u8
*addr
,
693 logger_level level
, const char *txt
)
695 struct hostapd_data
*hapd
= ctx
;
700 hlevel
= HOSTAPD_LEVEL_WARNING
;
703 hlevel
= HOSTAPD_LEVEL_INFO
;
707 hlevel
= HOSTAPD_LEVEL_DEBUG
;
711 hostapd_logger(hapd
, addr
, HOSTAPD_MODULE_WPA
, hlevel
, "%s", txt
);
715 static void hostapd_wpa_auth_disconnect(void *ctx
, const u8
*addr
,
718 struct hostapd_data
*hapd
= ctx
;
719 struct sta_info
*sta
;
721 wpa_printf(MSG_DEBUG
, "%s: WPA authenticator requests disconnect: "
722 "STA " MACSTR
" reason %d",
723 __func__
, MAC2STR(addr
), reason
);
725 sta
= ap_get_sta(hapd
, addr
);
726 hostapd_sta_deauth(hapd
, addr
, reason
);
729 sta
->flags
&= ~(WLAN_STA_AUTH
| WLAN_STA_ASSOC
| WLAN_STA_AUTHORIZED
);
730 eloop_cancel_timeout(ap_handle_timer
, hapd
, sta
);
731 eloop_register_timeout(0, 0, ap_handle_timer
, hapd
, sta
);
732 sta
->timeout_next
= STA_REMOVE
;
736 static void hostapd_wpa_auth_mic_failure_report(void *ctx
, const u8
*addr
)
738 struct hostapd_data
*hapd
= ctx
;
739 ieee80211_michael_mic_failure(hapd
, addr
, 0);
743 static void hostapd_wpa_auth_set_eapol(void *ctx
, const u8
*addr
,
744 wpa_eapol_variable var
, int value
)
746 struct hostapd_data
*hapd
= ctx
;
747 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
751 case WPA_EAPOL_portEnabled
:
752 ieee802_1x_notify_port_enabled(sta
->eapol_sm
, value
);
754 case WPA_EAPOL_portValid
:
755 ieee802_1x_notify_port_valid(sta
->eapol_sm
, value
);
757 case WPA_EAPOL_authorized
:
758 ieee802_1x_set_sta_authorized(hapd
, sta
, value
);
760 case WPA_EAPOL_portControl_Auto
:
762 sta
->eapol_sm
->portControl
= Auto
;
764 case WPA_EAPOL_keyRun
:
766 sta
->eapol_sm
->keyRun
= value
? TRUE
: FALSE
;
768 case WPA_EAPOL_keyAvailable
:
770 sta
->eapol_sm
->eap_if
->eapKeyAvailable
=
771 value
? TRUE
: FALSE
;
773 case WPA_EAPOL_keyDone
:
775 sta
->eapol_sm
->keyDone
= value
? TRUE
: FALSE
;
777 case WPA_EAPOL_inc_EapolFramesTx
:
779 sta
->eapol_sm
->dot1xAuthEapolFramesTx
++;
785 static int hostapd_wpa_auth_get_eapol(void *ctx
, const u8
*addr
,
786 wpa_eapol_variable var
)
788 struct hostapd_data
*hapd
= ctx
;
789 struct sta_info
*sta
= ap_get_sta(hapd
, addr
);
790 if (sta
== NULL
|| sta
->eapol_sm
== NULL
)
793 case WPA_EAPOL_keyRun
:
794 return sta
->eapol_sm
->keyRun
;
795 case WPA_EAPOL_keyAvailable
:
796 return sta
->eapol_sm
->eap_if
->eapKeyAvailable
;
803 static const u8
* hostapd_wpa_auth_get_psk(void *ctx
, const u8
*addr
,
806 struct hostapd_data
*hapd
= ctx
;
807 return hostapd_get_psk(hapd
->conf
, addr
, prev_psk
);
811 static int hostapd_wpa_auth_get_msk(void *ctx
, const u8
*addr
, u8
*msk
,
814 struct hostapd_data
*hapd
= ctx
;
817 struct sta_info
*sta
;
819 sta
= ap_get_sta(hapd
, addr
);
823 key
= ieee802_1x_get_key(sta
->eapol_sm
, &keylen
);
829 os_memcpy(msk
, key
, keylen
);
836 static int hostapd_wpa_auth_set_key(void *ctx
, int vlan_id
, const char *alg
,
837 const u8
*addr
, int idx
, u8
*key
,
840 struct hostapd_data
*hapd
= ctx
;
841 const char *ifname
= hapd
->conf
->iface
;
844 ifname
= hostapd_get_vlan_id_ifname(hapd
->conf
->vlan
, vlan_id
);
849 return hostapd_set_encryption(ifname
, hapd
, alg
, addr
, idx
,
854 static int hostapd_wpa_auth_get_seqnum(void *ctx
, const u8
*addr
, int idx
,
857 struct hostapd_data
*hapd
= ctx
;
858 return hostapd_get_seqnum(hapd
->conf
->iface
, hapd
, addr
, idx
, seq
);
862 static int hostapd_wpa_auth_get_seqnum_igtk(void *ctx
, const u8
*addr
, int idx
,
865 struct hostapd_data
*hapd
= ctx
;
866 return hostapd_get_seqnum_igtk(hapd
->conf
->iface
, hapd
, addr
, idx
,
871 static int hostapd_wpa_auth_send_eapol(void *ctx
, const u8
*addr
,
872 const u8
*data
, size_t data_len
,
875 struct hostapd_data
*hapd
= ctx
;
876 return hostapd_send_eapol(hapd
, addr
, data
, data_len
, encrypt
);
880 static int hostapd_wpa_auth_for_each_sta(
881 void *ctx
, int (*cb
)(struct wpa_state_machine
*sm
, void *ctx
),
884 struct hostapd_data
*hapd
= ctx
;
885 struct sta_info
*sta
;
887 for (sta
= hapd
->sta_list
; sta
; sta
= sta
->next
) {
888 if (sta
->wpa_sm
&& cb(sta
->wpa_sm
, cb_ctx
))
895 static int hostapd_wpa_auth_for_each_auth(
896 void *ctx
, int (*cb
)(struct wpa_authenticator
*sm
, void *ctx
),
899 struct hostapd_data
*ohapd
;
901 struct hapd_interfaces
*interfaces
= eloop_get_user_data();
903 for (i
= 0; i
< interfaces
->count
; i
++) {
904 for (j
= 0; j
< interfaces
->iface
[i
]->num_bss
; j
++) {
905 ohapd
= interfaces
->iface
[i
]->bss
[j
];
906 if (cb(ohapd
->wpa_auth
, cb_ctx
))
915 static int hostapd_wpa_auth_send_ether(void *ctx
, const u8
*dst
, u16 proto
,
916 const u8
*data
, size_t data_len
)
918 struct hostapd_data
*hapd
= ctx
;
920 if (hapd
->driver
&& hapd
->driver
->send_ether
)
921 return hapd
->driver
->send_ether(hapd
->drv_priv
, dst
,
922 hapd
->own_addr
, proto
,
924 if (hapd
->l2
== NULL
)
926 return l2_packet_send(hapd
->l2
, dst
, proto
, data
, data_len
);
930 #ifdef CONFIG_IEEE80211R
932 static int hostapd_wpa_auth_send_ft_action(void *ctx
, const u8
*dst
,
933 const u8
*data
, size_t data_len
)
935 struct hostapd_data
*hapd
= ctx
;
937 struct ieee80211_mgmt
*m
;
939 struct sta_info
*sta
;
941 sta
= ap_get_sta(hapd
, dst
);
942 if (sta
== NULL
|| sta
->wpa_sm
== NULL
)
945 m
= os_zalloc(sizeof(*m
) + data_len
);
948 mlen
= ((u8
*) &m
->u
- (u8
*) m
) + data_len
;
949 m
->frame_control
= IEEE80211_FC(WLAN_FC_TYPE_MGMT
,
950 WLAN_FC_STYPE_ACTION
);
951 os_memcpy(m
->da
, dst
, ETH_ALEN
);
952 os_memcpy(m
->sa
, hapd
->own_addr
, ETH_ALEN
);
953 os_memcpy(m
->bssid
, hapd
->own_addr
, ETH_ALEN
);
954 os_memcpy(&m
->u
, data
, data_len
);
956 res
= hostapd_send_mgmt_frame(hapd
, (u8
*) m
, mlen
, 0);
962 static struct wpa_state_machine
*
963 hostapd_wpa_auth_add_sta(void *ctx
, const u8
*sta_addr
)
965 struct hostapd_data
*hapd
= ctx
;
966 struct sta_info
*sta
;
968 sta
= ap_sta_add(hapd
, sta_addr
);
974 sta
->wpa_sm
= wpa_auth_sta_init(hapd
->wpa_auth
, sta
->addr
);
975 if (sta
->wpa_sm
== NULL
) {
976 ap_free_sta(hapd
, sta
);
979 sta
->auth_alg
= WLAN_AUTH_FT
;
985 static void hostapd_rrb_receive(void *ctx
, const u8
*src_addr
, const u8
*buf
,
988 struct hostapd_data
*hapd
= ctx
;
989 wpa_ft_rrb_rx(hapd
->wpa_auth
, src_addr
, buf
, len
);
992 #endif /* CONFIG_IEEE80211R */
996 * hostapd_validate_bssid_configuration - Validate BSSID configuration
997 * @iface: Pointer to interface data
998 * Returns: 0 on success, -1 on failure
1000 * This function is used to validate that the configured BSSIDs are valid.
1002 static int hostapd_validate_bssid_configuration(struct hostapd_iface
*iface
)
1004 u8 mask
[ETH_ALEN
] = { 0 };
1005 struct hostapd_data
*hapd
= iface
->bss
[0];
1006 unsigned int i
= iface
->conf
->num_bss
, bits
= 0, j
;
1009 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
1011 /* Determine the bits necessary to cover the number of BSSIDs. */
1012 for (i
--; i
; i
>>= 1)
1015 /* Determine the bits necessary to any configured BSSIDs,
1016 if they are higher than the number of BSSIDs. */
1017 for (j
= 0; j
< iface
->conf
->num_bss
; j
++) {
1018 if (hostapd_mac_comp_empty(iface
->conf
->bss
[j
].bssid
) == 0)
1021 for (i
= 0; i
< ETH_ALEN
; i
++) {
1023 iface
->conf
->bss
[j
].bssid
[i
] ^
1028 for (i
= 0; i
< ETH_ALEN
&& mask
[i
] == 0; i
++)
1034 while (mask
[i
] != 0) {
1046 os_memset(mask
, 0xff, ETH_ALEN
);
1048 for (i
= 5; i
> 5 - j
; i
--)
1054 wpa_printf(MSG_DEBUG
, "BSS count %lu, BSSID mask " MACSTR
" (%d bits)",
1055 (unsigned long) iface
->conf
->num_bss
, MAC2STR(mask
), bits
);
1057 res
= hostapd_valid_bss_mask(hapd
, hapd
->own_addr
, mask
);
1062 printf("Driver did not accept BSSID mask " MACSTR
" for start "
1063 "address " MACSTR
".\n",
1064 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
1068 for (i
= 0; i
< ETH_ALEN
; i
++) {
1069 if ((hapd
->own_addr
[i
] & mask
[i
]) != hapd
->own_addr
[i
]) {
1070 printf("Invalid BSSID mask " MACSTR
" for start "
1071 "address " MACSTR
".\n"
1072 "Start address must be the first address in the"
1073 " block (i.e., addr AND mask == addr).\n",
1074 MAC2STR(mask
), MAC2STR(hapd
->own_addr
));
1083 static int mac_in_conf(struct hostapd_config
*conf
, const void *a
)
1087 for (i
= 0; i
< conf
->num_bss
; i
++) {
1088 if (hostapd_mac_comp(conf
->bss
[i
].bssid
, a
) == 0) {
1097 static int hostapd_setup_wpa(struct hostapd_data
*hapd
)
1099 struct wpa_auth_config _conf
;
1100 struct wpa_auth_callbacks cb
;
1104 hostapd_wpa_auth_conf(hapd
->conf
, &_conf
);
1105 os_memset(&cb
, 0, sizeof(cb
));
1107 cb
.logger
= hostapd_wpa_auth_logger
;
1108 cb
.disconnect
= hostapd_wpa_auth_disconnect
;
1109 cb
.mic_failure_report
= hostapd_wpa_auth_mic_failure_report
;
1110 cb
.set_eapol
= hostapd_wpa_auth_set_eapol
;
1111 cb
.get_eapol
= hostapd_wpa_auth_get_eapol
;
1112 cb
.get_psk
= hostapd_wpa_auth_get_psk
;
1113 cb
.get_msk
= hostapd_wpa_auth_get_msk
;
1114 cb
.set_key
= hostapd_wpa_auth_set_key
;
1115 cb
.get_seqnum
= hostapd_wpa_auth_get_seqnum
;
1116 cb
.get_seqnum_igtk
= hostapd_wpa_auth_get_seqnum_igtk
;
1117 cb
.send_eapol
= hostapd_wpa_auth_send_eapol
;
1118 cb
.for_each_sta
= hostapd_wpa_auth_for_each_sta
;
1119 cb
.for_each_auth
= hostapd_wpa_auth_for_each_auth
;
1120 cb
.send_ether
= hostapd_wpa_auth_send_ether
;
1121 #ifdef CONFIG_IEEE80211R
1122 cb
.send_ft_action
= hostapd_wpa_auth_send_ft_action
;
1123 cb
.add_sta
= hostapd_wpa_auth_add_sta
;
1124 #endif /* CONFIG_IEEE80211R */
1125 hapd
->wpa_auth
= wpa_init(hapd
->own_addr
, &_conf
, &cb
);
1126 if (hapd
->wpa_auth
== NULL
) {
1127 printf("WPA initialization failed.\n");
1131 if (hostapd_set_privacy(hapd
, 1)) {
1132 wpa_printf(MSG_ERROR
, "Could not set PrivacyInvoked "
1133 "for interface %s", hapd
->conf
->iface
);
1137 wpa_ie
= wpa_auth_get_wpa_ie(hapd
->wpa_auth
, &wpa_ie_len
);
1138 if (hostapd_set_generic_elem(hapd
, wpa_ie
, wpa_ie_len
)) {
1139 wpa_printf(MSG_ERROR
, "Failed to configure WPA IE for "
1140 "the kernel driver.");
1144 if (rsn_preauth_iface_init(hapd
)) {
1145 printf("Initialization of RSN pre-authentication "
1155 static int hostapd_setup_radius_srv(struct hostapd_data
*hapd
,
1156 struct hostapd_bss_config
*conf
)
1158 struct radius_server_conf srv
;
1159 os_memset(&srv
, 0, sizeof(srv
));
1160 srv
.client_file
= conf
->radius_server_clients
;
1161 srv
.auth_port
= conf
->radius_server_auth_port
;
1162 srv
.conf_ctx
= conf
;
1163 srv
.eap_sim_db_priv
= hapd
->eap_sim_db_priv
;
1164 srv
.ssl_ctx
= hapd
->ssl_ctx
;
1165 srv
.pac_opaque_encr_key
= conf
->pac_opaque_encr_key
;
1166 srv
.eap_fast_a_id
= conf
->eap_fast_a_id
;
1167 srv
.eap_sim_aka_result_ind
= conf
->eap_sim_aka_result_ind
;
1168 srv
.tnc
= conf
->tnc
;
1169 srv
.ipv6
= conf
->radius_server_ipv6
;
1170 srv
.get_eap_user
= hostapd_radius_get_eap_user
;
1172 hapd
->radius_srv
= radius_server_init(&srv
);
1173 if (hapd
->radius_srv
== NULL
) {
1174 printf("RADIUS server initialization failed.\n");
1183 * hostapd_setup_bss - Per-BSS setup (initialization)
1184 * @hapd: Pointer to BSS data
1185 * @first: Whether this BSS is the first BSS of an interface
1187 * This function is used to initialize all per-BSS data structures and
1188 * resources. This gets called in a loop for each BSS when an interface is
1189 * initialized. Most of the modules that are initialized here will be
1190 * deinitialized in hostapd_cleanup().
1192 static int hostapd_setup_bss(struct hostapd_data
*hapd
, int first
)
1194 struct hostapd_bss_config
*conf
= hapd
->conf
;
1195 u8 ssid
[HOSTAPD_MAX_SSID_LEN
+ 1];
1196 int ssid_len
, set_ssid
;
1199 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0) {
1200 /* Allocate the next available BSSID. */
1202 inc_byte_array(hapd
->own_addr
, ETH_ALEN
);
1203 } while (mac_in_conf(hapd
->iconf
, hapd
->own_addr
));
1205 /* Allocate the configured BSSID. */
1206 os_memcpy(hapd
->own_addr
, hapd
->conf
->bssid
, ETH_ALEN
);
1208 if (hostapd_mac_comp(hapd
->own_addr
,
1209 hapd
->iface
->bss
[0]->own_addr
) ==
1211 printf("BSS '%s' may not have BSSID "
1212 "set to the MAC address of the radio\n",
1218 hapd
->interface_added
= 1;
1219 if (hostapd_bss_add(hapd
->iface
->bss
[0], hapd
->conf
->iface
,
1221 printf("Failed to add BSS (BSSID=" MACSTR
")\n",
1222 MAC2STR(hapd
->own_addr
));
1228 * Fetch the SSID from the system and use it or,
1229 * if one was specified in the config file, verify they
1232 ssid_len
= hostapd_get_ssid(hapd
, ssid
, sizeof(ssid
));
1234 printf("Could not read SSID from system\n");
1237 if (conf
->ssid
.ssid_set
) {
1239 * If SSID is specified in the config file and it differs
1240 * from what is being used then force installation of the
1243 set_ssid
= (conf
->ssid
.ssid_len
!= (size_t) ssid_len
||
1244 os_memcmp(conf
->ssid
.ssid
, ssid
, ssid_len
) != 0);
1247 * No SSID in the config file; just use the one we got
1251 conf
->ssid
.ssid_len
= ssid_len
;
1252 os_memcpy(conf
->ssid
.ssid
, ssid
, conf
->ssid
.ssid_len
);
1253 conf
->ssid
.ssid
[conf
->ssid
.ssid_len
] = '\0';
1256 printf("Using interface %s with hwaddr " MACSTR
" and ssid '%s'\n",
1257 hapd
->conf
->iface
, MAC2STR(hapd
->own_addr
),
1258 hapd
->conf
->ssid
.ssid
);
1260 if (hostapd_setup_wpa_psk(conf
)) {
1261 printf("WPA-PSK setup failed.\n");
1265 /* Set flag for whether SSID is broadcast in beacons */
1266 if (hostapd_set_broadcast_ssid(hapd
,
1267 !!hapd
->conf
->ignore_broadcast_ssid
)) {
1268 printf("Could not set broadcast SSID flag for kernel "
1273 if (hostapd_set_dtim_period(hapd
, hapd
->conf
->dtim_period
)) {
1274 printf("Could not set DTIM period for kernel driver\n");
1278 /* Set SSID for the kernel driver (to be used in beacon and probe
1279 * response frames) */
1280 if (set_ssid
&& hostapd_set_ssid(hapd
, (u8
*) conf
->ssid
.ssid
,
1281 conf
->ssid
.ssid_len
)) {
1282 printf("Could not set SSID for kernel driver\n");
1286 if (wpa_debug_level
== MSG_MSGDUMP
)
1287 conf
->radius
->msg_dumps
= 1;
1288 hapd
->radius
= radius_client_init(hapd
, conf
->radius
);
1289 if (hapd
->radius
== NULL
) {
1290 printf("RADIUS client initialization failed.\n");
1294 if (hostapd_acl_init(hapd
)) {
1295 printf("ACL initialization failed.\n");
1299 if (ieee802_1x_init(hapd
)) {
1300 printf("IEEE 802.1X initialization failed.\n");
1304 if (hapd
->conf
->wpa
&& hostapd_setup_wpa(hapd
))
1307 if (accounting_init(hapd
)) {
1308 printf("Accounting initialization failed.\n");
1312 if (hapd
->conf
->ieee802_11f
&&
1313 (hapd
->iapp
= iapp_init(hapd
, hapd
->conf
->iapp_iface
)) == NULL
) {
1314 printf("IEEE 802.11F (IAPP) initialization failed.\n");
1318 if (hostapd_ctrl_iface_init(hapd
)) {
1319 printf("Failed to setup control interface\n");
1323 if (vlan_init(hapd
)) {
1324 printf("VLAN initialization failed.\n");
1328 #ifdef CONFIG_IEEE80211R
1329 hapd
->l2
= l2_packet_init(hapd
->conf
->iface
, NULL
, ETH_P_RRB
,
1330 hostapd_rrb_receive
, hapd
, 0);
1331 if (hapd
->l2
== NULL
&&
1332 (hapd
->driver
== NULL
|| hapd
->driver
->send_ether
== NULL
)) {
1333 printf("Failed to open l2_packet interface\n");
1336 #endif /* CONFIG_IEEE80211R */
1338 ieee802_11_set_beacon(hapd
);
1340 if (conf
->radius_server_clients
&&
1341 hostapd_setup_radius_srv(hapd
, conf
))
1349 * setup_interface2 - Setup (initialize) an interface (part 2)
1350 * @iface: Pointer to interface data.
1351 * Returns: 0 on success; -1 on failure.
1353 * Flushes old stations, sets the channel, DFS parameters, encryption,
1354 * beacons, and WDS links based on the configuration.
1356 static int setup_interface2(struct hostapd_iface
*iface
)
1358 struct hostapd_data
*hapd
= iface
->bss
[0];
1364 hostapd_flush_old_stations(hapd
);
1365 hostapd_set_privacy(hapd
, 0);
1367 if (hapd
->iconf
->channel
) {
1368 freq
= hostapd_hw_get_freq(hapd
, hapd
->iconf
->channel
);
1369 printf("Mode: %s Channel: %d Frequency: %d MHz\n",
1370 hostapd_hw_mode_txt(hapd
->iconf
->hw_mode
),
1371 hapd
->iconf
->channel
, freq
);
1373 if (hostapd_set_freq(hapd
, hapd
->iconf
->hw_mode
, freq
)) {
1374 printf("Could not set channel for kernel driver\n");
1379 hostapd_broadcast_wep_clear(hapd
);
1380 if (hostapd_setup_encryption(hapd
->conf
->iface
, hapd
))
1383 hostapd_set_beacon_int(hapd
, hapd
->iconf
->beacon_int
);
1384 ieee802_11_set_beacon(hapd
);
1386 if (hapd
->iconf
->rts_threshold
> -1 &&
1387 hostapd_set_rts(hapd
, hapd
->iconf
->rts_threshold
)) {
1388 printf("Could not set RTS threshold for kernel driver\n");
1392 if (hapd
->iconf
->fragm_threshold
> -1 &&
1393 hostapd_set_frag(hapd
, hapd
->iconf
->fragm_threshold
)) {
1394 printf("Could not set fragmentation threshold for kernel "
1399 prev_addr
= hapd
->own_addr
;
1401 for (j
= 0; j
< iface
->num_bss
; j
++) {
1402 hapd
= iface
->bss
[j
];
1404 os_memcpy(hapd
->own_addr
, prev_addr
, ETH_ALEN
);
1405 if (hostapd_setup_bss(hapd
, j
== 0))
1407 if (hostapd_mac_comp_empty(hapd
->conf
->bssid
) == 0)
1408 prev_addr
= hapd
->own_addr
;
1411 ap_list_init(iface
);
1413 if (hostapd_driver_commit(hapd
) < 0) {
1414 wpa_printf(MSG_ERROR
, "%s: Failed to commit driver "
1415 "configuration", __func__
);
1423 static void setup_interface_start(void *eloop_data
, void *user_ctx
);
1424 static void setup_interface2_handler(void *eloop_data
, void *user_ctx
);
1427 * setup_interface_finalize - Finish setup interface & call the callback
1428 * @iface: Pointer to interface data.
1429 * @status: Status of the setup interface (0 on success; -1 on failure).
1430 * Returns: 0 on success; -1 on failure (e.g., was not in progress).
1432 static int setup_interface_finalize(struct hostapd_iface
*iface
, int status
)
1434 hostapd_iface_cb cb
;
1436 if (!iface
->setup_cb
)
1439 eloop_cancel_timeout(setup_interface_start
, iface
, NULL
);
1440 eloop_cancel_timeout(setup_interface2_handler
, iface
, NULL
);
1441 hostapd_select_hw_mode_stop(iface
);
1443 cb
= iface
->setup_cb
;
1445 iface
->setup_cb
= NULL
;
1454 * setup_interface2_wrapper - Wrapper for setup_interface2()
1455 * @iface: Pointer to interface data.
1456 * @status: Status of the hw mode select.
1458 * Wrapper for setup_interface2() to calls finalize function upon completion.
1460 static void setup_interface2_wrapper(struct hostapd_iface
*iface
, int status
)
1464 printf("Could not select hw_mode and channel. (%d)\n", ret
);
1466 ret
= setup_interface2(iface
);
1468 setup_interface_finalize(iface
, ret
);
1473 * setup_interface2_handler - Used for immediate call of setup_interface2
1474 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1475 * @user_ctx: Unused.
1477 static void setup_interface2_handler(void *eloop_data
, void *user_ctx
)
1479 struct hostapd_iface
*iface
= eloop_data
;
1481 setup_interface2_wrapper(iface
, 0);
1485 static int hostapd_radius_get_eap_user(void *ctx
, const u8
*identity
,
1486 size_t identity_len
, int phase2
,
1487 struct eap_user
*user
)
1489 const struct hostapd_eap_user
*eap_user
;
1492 eap_user
= hostapd_get_eap_user(ctx
, identity
, identity_len
, phase2
);
1493 if (eap_user
== NULL
)
1499 os_memset(user
, 0, sizeof(*user
));
1500 count
= EAP_USER_MAX_METHODS
;
1501 if (count
> EAP_MAX_METHODS
)
1502 count
= EAP_MAX_METHODS
;
1503 for (i
= 0; i
< count
; i
++) {
1504 user
->methods
[i
].vendor
= eap_user
->methods
[i
].vendor
;
1505 user
->methods
[i
].method
= eap_user
->methods
[i
].method
;
1508 if (eap_user
->password
) {
1509 user
->password
= os_malloc(eap_user
->password_len
);
1510 if (user
->password
== NULL
)
1512 os_memcpy(user
->password
, eap_user
->password
,
1513 eap_user
->password_len
);
1514 user
->password_len
= eap_user
->password_len
;
1515 user
->password_hash
= eap_user
->password_hash
;
1517 user
->force_version
= eap_user
->force_version
;
1518 user
->ttls_auth
= eap_user
->ttls_auth
;
1525 * setup_interface1 - Setup (initialize) an interface (part 1)
1526 * @iface: Pointer to interface data
1527 * Returns: 0 on success, -1 on failure
1529 * Initializes the driver interface, validates the configuration,
1530 * and sets driver parameters based on the configuration.
1531 * Schedules setup_interface2() to be called immediately or after
1532 * hardware mode setup takes place.
1534 static int setup_interface1(struct hostapd_iface
*iface
)
1536 struct hostapd_data
*hapd
= iface
->bss
[0];
1537 struct hostapd_bss_config
*conf
= hapd
->conf
;
1540 u8
*b
= conf
->bssid
;
1543 * Initialize the driver interface and make sure that all BSSes get
1544 * configured with a pointer to this driver interface.
1546 if (b
[0] | b
[1] | b
[2] | b
[3] | b
[4] | b
[5]) {
1547 hapd
->drv_priv
= hostapd_driver_init_bssid(hapd
, b
);
1549 hapd
->drv_priv
= hostapd_driver_init(hapd
);
1552 if (hapd
->drv_priv
== NULL
) {
1553 printf("%s driver initialization failed.\n",
1554 hapd
->driver
? hapd
->driver
->name
: "Unknown");
1555 hapd
->driver
= NULL
;
1558 for (i
= 0; i
< iface
->num_bss
; i
++) {
1559 iface
->bss
[i
]->driver
= hapd
->driver
;
1560 iface
->bss
[i
]->drv_priv
= hapd
->drv_priv
;
1563 if (hostapd_validate_bssid_configuration(iface
))
1566 os_memcpy(country
, hapd
->iconf
->country
, 3);
1568 if (hostapd_set_country(hapd
, country
) < 0) {
1569 printf("Failed to set country code\n");
1573 if (hapd
->iconf
->ieee80211d
|| hapd
->iconf
->ieee80211h
) {
1574 if (hostapd_set_ieee80211d(hapd
, 1) < 0) {
1575 printf("Failed to set ieee80211d (%d)\n",
1576 hapd
->iconf
->ieee80211d
);
1581 if (hapd
->iconf
->bridge_packets
!= INTERNAL_BRIDGE_DO_NOT_CONTROL
&&
1582 hostapd_set_internal_bridge(hapd
, hapd
->iconf
->bridge_packets
)) {
1583 printf("Failed to set bridge_packets for kernel driver\n");
1587 /* TODO: merge with hostapd_driver_init() ? */
1588 if (hostapd_wireless_event_init(hapd
) < 0)
1591 if (hostapd_get_hw_features(iface
)) {
1592 /* Not all drivers support this yet, so continue without hw
1595 return hostapd_select_hw_mode_start(iface
,
1596 setup_interface2_wrapper
);
1599 eloop_register_timeout(0, 0, setup_interface2_handler
, iface
, NULL
);
1605 * setup_interface_start - Handler to start setup interface
1606 * @eloop_data: Stores the struct hostapd_iface * for the interface.
1607 * @user_ctx: Unused.
1609 * An eloop handler is used so that all errors can be processed by the
1610 * callback without introducing stack recursion.
1612 static void setup_interface_start(void *eloop_data
, void *user_ctx
)
1614 struct hostapd_iface
*iface
= eloop_data
;
1618 ret
= setup_interface1(iface
);
1620 setup_interface_finalize(iface
, ret
);
1625 * hostapd_setup_interface_start - Start the setup of an interface
1626 * @iface: Pointer to interface data.
1627 * @cb: The function to callback when done.
1628 * Returns: 0 if it starts successfully; cb will be called when done.
1629 * -1 on failure; cb will not be called.
1631 * Initializes the driver interface, validates the configuration,
1632 * and sets driver parameters based on the configuration.
1633 * Flushes old stations, sets the channel, DFS parameters, encryption,
1634 * beacons, and WDS links based on the configuration.
1636 int hostapd_setup_interface_start(struct hostapd_iface
*iface
,
1637 hostapd_iface_cb cb
)
1639 if (iface
->setup_cb
) {
1640 wpa_printf(MSG_DEBUG
,
1641 "%s: Interface setup already in progress.\n",
1642 iface
->bss
[0]->conf
->iface
);
1646 iface
->setup_cb
= cb
;
1648 eloop_register_timeout(0, 0, setup_interface_start
, iface
, NULL
);
1655 * hostapd_setup_interace_stop - Stops the setup of an interface
1656 * @iface: Pointer to interface data
1657 * Returns: 0 if successfully stopped;
1658 * -1 on failure (i.e., was not in progress)
1660 int hostapd_setup_interface_stop(struct hostapd_iface
*iface
)
1662 return setup_interface_finalize(iface
, -1);
1666 static void show_version(void)
1669 "hostapd v" VERSION_STR
"\n"
1670 "User space daemon for IEEE 802.11 AP management,\n"
1671 "IEEE 802.1X/WPA/WPA2/EAP/RADIUS Authenticator\n"
1672 "Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi> "
1673 "and contributors\n");
1677 static void usage(void)
1682 "usage: hostapd [-hdBKtv] [-P <PID file>] "
1683 "<configuration file(s)>\n"
1686 " -h show this usage\n"
1687 " -d show more debug messages (-dd for even more)\n"
1688 " -B run daemon in the background\n"
1690 " -K include key data in debug messages\n"
1691 " -t include timestamps in some debug messages\n"
1692 " -v show hostapd version\n");
1699 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1700 * @hapd_iface: Pointer to interface data
1701 * @conf: Pointer to per-interface configuration
1702 * @bss: Pointer to per-BSS configuration for this BSS
1703 * Returns: Pointer to allocated BSS data
1705 * This function is used to allocate per-BSS data structure. This data will be
1706 * freed after hostapd_cleanup() is called for it during interface
1709 static struct hostapd_data
*
1710 hostapd_alloc_bss_data(struct hostapd_iface
*hapd_iface
,
1711 struct hostapd_config
*conf
,
1712 struct hostapd_bss_config
*bss
)
1714 struct hostapd_data
*hapd
;
1716 hapd
= os_zalloc(sizeof(*hapd
));
1722 hapd
->iface
= hapd_iface
;
1724 if (hapd
->conf
->individual_wep_key_len
> 0) {
1725 /* use key0 in individual key and key1 in broadcast key */
1726 hapd
->default_wep_key_idx
= 1;
1729 #ifdef EAP_TLS_FUNCS
1730 if (hapd
->conf
->eap_server
&&
1731 (hapd
->conf
->ca_cert
|| hapd
->conf
->server_cert
||
1732 hapd
->conf
->dh_file
)) {
1733 struct tls_connection_params params
;
1735 hapd
->ssl_ctx
= tls_init(NULL
);
1736 if (hapd
->ssl_ctx
== NULL
) {
1737 printf("Failed to initialize TLS\n");
1741 os_memset(¶ms
, 0, sizeof(params
));
1742 params
.ca_cert
= hapd
->conf
->ca_cert
;
1743 params
.client_cert
= hapd
->conf
->server_cert
;
1744 params
.private_key
= hapd
->conf
->private_key
;
1745 params
.private_key_passwd
= hapd
->conf
->private_key_passwd
;
1746 params
.dh_file
= hapd
->conf
->dh_file
;
1748 if (tls_global_set_params(hapd
->ssl_ctx
, ¶ms
)) {
1749 printf("Failed to set TLS parameters\n");
1753 if (tls_global_set_verify(hapd
->ssl_ctx
,
1754 hapd
->conf
->check_crl
)) {
1755 printf("Failed to enable check_crl\n");
1759 #endif /* EAP_TLS_FUNCS */
1762 if (hapd
->conf
->eap_sim_db
) {
1763 hapd
->eap_sim_db_priv
=
1764 eap_sim_db_init(hapd
->conf
->eap_sim_db
,
1765 hostapd_sim_db_cb
, hapd
);
1766 if (hapd
->eap_sim_db_priv
== NULL
) {
1767 printf("Failed to initialize EAP-SIM database "
1772 #endif /* EAP_SERVER */
1774 if (hapd
->conf
->assoc_ap
)
1775 hapd
->assoc_ap_state
= WAIT_BEACON
;
1777 hapd
->driver
= hapd
->iconf
->driver
;
1781 #if defined(EAP_TLS_FUNCS) || defined(EAP_SERVER)
1784 /* TODO: cleanup allocated resources(?) */
1791 * hostapd_init - Allocate and initialize per-interface data
1792 * @config_file: Path to the configuration file
1793 * Returns: Pointer to the allocated interface data or %NULL on failure
1795 * This function is used to allocate main data structures for per-interface
1796 * data. The allocated data buffer will be freed by calling
1797 * hostapd_cleanup_iface().
1799 static struct hostapd_iface
* hostapd_init(const char *config_file
)
1801 struct hostapd_iface
*hapd_iface
= NULL
;
1802 struct hostapd_config
*conf
= NULL
;
1803 struct hostapd_data
*hapd
;
1806 hapd_iface
= os_zalloc(sizeof(*hapd_iface
));
1807 if (hapd_iface
== NULL
)
1810 hapd_iface
->config_fname
= os_strdup(config_file
);
1811 if (hapd_iface
->config_fname
== NULL
)
1814 conf
= hostapd_config_read(hapd_iface
->config_fname
);
1817 hapd_iface
->conf
= conf
;
1819 hapd_iface
->num_bss
= conf
->num_bss
;
1820 hapd_iface
->bss
= os_zalloc(conf
->num_bss
*
1821 sizeof(struct hostapd_data
*));
1822 if (hapd_iface
->bss
== NULL
)
1825 for (i
= 0; i
< conf
->num_bss
; i
++) {
1826 hapd
= hapd_iface
->bss
[i
] =
1827 hostapd_alloc_bss_data(hapd_iface
, conf
,
1837 hostapd_config_free(conf
);
1839 for (i
= 0; hapd_iface
->bss
&& i
< hapd_iface
->num_bss
; i
++) {
1840 hapd
= hapd_iface
->bss
[i
];
1841 if (hapd
&& hapd
->ssl_ctx
)
1842 tls_deinit(hapd
->ssl_ctx
);
1845 os_free(hapd_iface
->config_fname
);
1846 os_free(hapd_iface
->bss
);
1847 os_free(hapd_iface
);
1854 * register_drivers - Register driver interfaces
1856 * This function is generated by Makefile (into driver_conf.c) to call all
1857 * configured driver interfaces to register them to core hostapd.
1859 void register_drivers(void);
1863 * setup_interface_done - Callback when an interface is done being setup.
1864 * @iface: Pointer to interface data.
1865 * @status: Status of the interface setup (0 on success; -1 on failure).
1867 static void setup_interface_done(struct hostapd_iface
*iface
, int status
)
1870 wpa_printf(MSG_DEBUG
, "%s: Unable to setup interface.",
1871 iface
->bss
[0]->conf
->iface
);
1874 wpa_printf(MSG_DEBUG
, "%s: Setup of interface done.",
1875 iface
->bss
[0]->conf
->iface
);
1879 int main(int argc
, char *argv
[])
1881 struct hapd_interfaces interfaces
;
1884 int c
, debug
= 0, daemonize
= 0, tnc
= 0;
1885 const char *pid_file
= NULL
;
1887 hostapd_logger_register_cb(hostapd_logger_cb
);
1890 c
= getopt(argc
, argv
, "BdhKP:tv");
1899 if (wpa_debug_level
> 0)
1906 wpa_debug_show_keys
++;
1912 wpa_debug_timestamp
++;
1928 if (eap_server_register_methods()) {
1929 wpa_printf(MSG_ERROR
, "Failed to register EAP methods");
1933 interfaces
.count
= argc
- optind
;
1935 interfaces
.iface
= os_malloc(interfaces
.count
*
1936 sizeof(struct hostapd_iface
*));
1937 if (interfaces
.iface
== NULL
) {
1938 wpa_printf(MSG_ERROR
, "malloc failed\n");
1942 if (eloop_init(&interfaces
)) {
1943 wpa_printf(MSG_ERROR
, "Failed to initialize event loop");
1947 #ifndef CONFIG_NATIVE_WINDOWS
1948 eloop_register_signal(SIGHUP
, handle_reload
, NULL
);
1949 eloop_register_signal(SIGUSR1
, handle_dump_state
, NULL
);
1950 #endif /* CONFIG_NATIVE_WINDOWS */
1951 eloop_register_signal_terminate(handle_term
, NULL
);
1953 /* Initialize interfaces */
1954 for (i
= 0; i
< interfaces
.count
; i
++) {
1955 printf("Configuration file: %s\n", argv
[optind
+ i
]);
1956 interfaces
.iface
[i
] = hostapd_init(argv
[optind
+ i
]);
1957 if (!interfaces
.iface
[i
])
1959 for (k
= 0; k
< debug
; k
++) {
1960 if (interfaces
.iface
[i
]->bss
[0]->conf
->
1961 logger_stdout_level
> 0)
1962 interfaces
.iface
[i
]->bss
[0]->conf
->
1963 logger_stdout_level
--;
1966 ret
= hostapd_setup_interface_start(interfaces
.iface
[i
],
1967 setup_interface_done
);
1971 for (k
= 0; k
< (int) interfaces
.iface
[i
]->num_bss
; k
++) {
1972 if (interfaces
.iface
[i
]->bss
[0]->conf
->tnc
)
1978 if (tnc
&& tncs_global_init() < 0) {
1979 wpa_printf(MSG_ERROR
, "Failed to initialize TNCS");
1982 #endif /* EAP_TNC */
1984 if (daemonize
&& os_daemonize(pid_file
)) {
1989 #ifndef CONFIG_NATIVE_WINDOWS
1990 openlog("hostapd", 0, LOG_DAEMON
);
1991 #endif /* CONFIG_NATIVE_WINDOWS */
1995 /* Disconnect associated stations from all interfaces and BSSes */
1996 for (i
= 0; i
< interfaces
.count
; i
++) {
1997 for (j
= 0; j
< interfaces
.iface
[i
]->num_bss
; j
++) {
1998 struct hostapd_data
*hapd
=
1999 interfaces
.iface
[i
]->bss
[j
];
2000 hostapd_free_stas(hapd
);
2001 hostapd_flush_old_stations(hapd
);
2008 /* Deinitialize all interfaces */
2009 for (i
= 0; i
< interfaces
.count
; i
++) {
2010 if (!interfaces
.iface
[i
])
2012 hostapd_setup_interface_stop(interfaces
.iface
[i
]);
2013 hostapd_cleanup_iface_pre(interfaces
.iface
[i
]);
2014 for (j
= 0; j
< interfaces
.iface
[i
]->num_bss
; j
++) {
2015 struct hostapd_data
*hapd
=
2016 interfaces
.iface
[i
]->bss
[j
];
2017 hostapd_cleanup(hapd
);
2018 if (j
== interfaces
.iface
[i
]->num_bss
- 1 &&
2020 hostapd_driver_deinit(hapd
);
2022 for (j
= 0; j
< interfaces
.iface
[i
]->num_bss
; j
++)
2023 os_free(interfaces
.iface
[i
]->bss
[j
]);
2024 hostapd_cleanup_iface(interfaces
.iface
[i
]);
2026 os_free(interfaces
.iface
);
2029 tncs_global_deinit();
2030 #endif /* EAP_TNC */
2034 #ifndef CONFIG_NATIVE_WINDOWS
2036 #endif /* CONFIG_NATIVE_WINDOWS */
2038 eap_server_unregister_methods();
2040 os_daemonize_terminate(pid_file
);