2 * hostapd / Initialization and configuration
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "crypto/tls.h"
20 #include "common/ieee802_11_defs.h"
21 #include "eapol_auth/eapol_auth_sm.h"
22 #include "eapol_auth/eapol_auth_sm_i.h"
23 #include "radius/radius_client.h"
24 #include "radius/radius_server.h"
25 #include "eap_server/eap_sim_db.h"
26 #include "eap_server/eap.h"
27 #include "eap_server/tncs.h"
28 #include "l2_packet/l2_packet.h"
30 #include "ieee802_1x.h"
32 #include "hw_features.h"
33 #include "accounting.h"
35 #include "ieee802_11_auth.h"
41 #include "vlan_init.h"
42 #include "ctrl_iface.h"
43 #include "wps_hostapd.h"
44 #include "tkip_countermeasures.h"
47 static int hostapd_flush_old_stations(struct hostapd_data *hapd);
48 static int hostapd_setup_wpa(struct hostapd_data *hapd);
49 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd);
51 extern int wpa_debug_level;
53 #if defined(EAP_SERVER_SIM) || defined(EAP_SERVER_AKA)
55 #endif /* EAP_SERVER_SIM || EAP_SERVER_AKA */
59 static int hostapd_sim_db_cb_sta(struct hostapd_data *hapd,
60 struct sta_info *sta, void *ctx)
62 if (eapol_auth_eap_pending_cb(sta->eapol_sm, ctx) == 0)
68 static void hostapd_sim_db_cb(void *ctx, void *session_ctx)
70 struct hostapd_data *hapd = ctx;
71 if (ap_for_each_sta(hapd, hostapd_sim_db_cb_sta, session_ctx) == 0) {
73 radius_server_eap_pending_cb(hapd->radius_srv, session_ctx);
74 #endif /* RADIUS_SERVER */
77 #endif /* EAP_SIM_DB */
80 static void hostapd_wpa_auth_conf(struct hostapd_bss_config *conf,
81 struct wpa_auth_config *wconf)
83 wconf->wpa = conf->wpa;
84 wconf->wpa_key_mgmt = conf->wpa_key_mgmt;
85 wconf->wpa_pairwise = conf->wpa_pairwise;
86 wconf->wpa_group = conf->wpa_group;
87 wconf->wpa_group_rekey = conf->wpa_group_rekey;
88 wconf->wpa_strict_rekey = conf->wpa_strict_rekey;
89 wconf->wpa_gmk_rekey = conf->wpa_gmk_rekey;
90 wconf->wpa_ptk_rekey = conf->wpa_ptk_rekey;
91 wconf->rsn_pairwise = conf->rsn_pairwise;
92 wconf->rsn_preauth = conf->rsn_preauth;
93 wconf->eapol_version = conf->eapol_version;
94 wconf->peerkey = conf->peerkey;
95 wconf->wmm_enabled = conf->wmm_enabled;
96 wconf->okc = conf->okc;
97 #ifdef CONFIG_IEEE80211W
98 wconf->ieee80211w = conf->ieee80211w;
99 #endif /* CONFIG_IEEE80211W */
100 #ifdef CONFIG_IEEE80211R
101 wconf->ssid_len = conf->ssid.ssid_len;
102 if (wconf->ssid_len > SSID_LEN)
103 wconf->ssid_len = SSID_LEN;
104 os_memcpy(wconf->ssid, conf->ssid.ssid, wconf->ssid_len);
105 os_memcpy(wconf->mobility_domain, conf->mobility_domain,
106 MOBILITY_DOMAIN_ID_LEN);
107 if (conf->nas_identifier &&
108 os_strlen(conf->nas_identifier) <= FT_R0KH_ID_MAX_LEN) {
109 wconf->r0_key_holder_len = os_strlen(conf->nas_identifier);
110 os_memcpy(wconf->r0_key_holder, conf->nas_identifier,
111 wconf->r0_key_holder_len);
113 os_memcpy(wconf->r1_key_holder, conf->r1_key_holder, FT_R1KH_ID_LEN);
114 wconf->r0_key_lifetime = conf->r0_key_lifetime;
115 wconf->reassociation_deadline = conf->reassociation_deadline;
116 wconf->r0kh_list = conf->r0kh_list;
117 wconf->r1kh_list = conf->r1kh_list;
118 wconf->pmk_r1_push = conf->pmk_r1_push;
119 #endif /* CONFIG_IEEE80211R */
123 int hostapd_reload_config(struct hostapd_iface *iface)
125 struct hostapd_data *hapd = iface->bss[0];
126 struct hostapd_config *newconf, *oldconf;
127 struct wpa_auth_config wpa_auth_conf;
130 newconf = hostapd_config_read(iface->config_fname);
135 * Deauthenticate all stations since the new configuration may not
136 * allow them to use the BSS anymore.
138 for (j = 0; j < iface->num_bss; j++)
139 hostapd_flush_old_stations(iface->bss[j]);
141 #ifndef CONFIG_NO_RADIUS
142 /* TODO: update dynamic data based on changed configuration
143 * items (e.g., open/close sockets, etc.) */
144 radius_client_flush(hapd->radius, 0);
145 #endif /* CONFIG_NO_RADIUS */
147 oldconf = hapd->iconf;
148 hapd->iconf = newconf;
149 hapd->conf = &newconf->bss[0];
150 iface->conf = newconf;
152 if (hostapd_setup_wpa_psk(hapd->conf)) {
153 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
154 "after reloading configuration");
157 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
158 hostapd_setup_wpa(hapd);
159 else if (hapd->conf->wpa) {
160 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
161 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
162 } else if (hapd->wpa_auth) {
163 wpa_deinit(hapd->wpa_auth);
164 hapd->wpa_auth = NULL;
165 hostapd_set_privacy(hapd, 0);
166 hostapd_setup_encryption(hapd->conf->iface, hapd);
169 ieee802_11_set_beacon(hapd);
171 if (hapd->conf->ssid.ssid_set &&
172 hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
173 hapd->conf->ssid.ssid_len)) {
174 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
175 /* try to continue */
178 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
179 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
181 hostapd_set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
183 hostapd_config_free(oldconf);
185 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
191 int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
193 if (hostapd_reload_config(iface) < 0) {
194 wpa_printf(MSG_WARNING, "Failed to read new configuration "
195 "file - continuing with old.");
201 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
206 for (i = 0; i < NUM_WEP_KEYS; i++) {
207 if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
208 i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
209 wpa_printf(MSG_DEBUG, "Failed to clear default "
210 "encryption keys (ifname=%s keyidx=%d)",
214 #ifdef CONFIG_IEEE80211W
215 if (hapd->conf->ieee80211w) {
216 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
217 if (hostapd_set_key(ifname, hapd, WPA_ALG_NONE, NULL,
218 i, i == 0 ? 1 : 0, NULL, 0,
220 wpa_printf(MSG_DEBUG, "Failed to clear "
221 "default mgmt encryption keys "
222 "(ifname=%s keyidx=%d)", ifname, i);
226 #endif /* CONFIG_IEEE80211W */
230 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
232 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
237 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
240 struct hostapd_ssid *ssid = &hapd->conf->ssid;
243 if (ssid->wep.default_len &&
244 hostapd_set_key(hapd->conf->iface,
245 hapd, WPA_ALG_WEP, NULL, idx, idx == ssid->wep.idx,
246 NULL, 0, ssid->wep.key[idx], ssid->wep.len[idx])) {
247 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
251 if (ssid->dyn_vlan_keys) {
253 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
255 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
258 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
264 if (hostapd_set_key(ifname, hapd, WPA_ALG_WEP, NULL,
265 idx, idx == key->idx, NULL, 0,
266 key->key[idx], key->len[idx])) {
267 wpa_printf(MSG_WARNING, "Could not set "
268 "dynamic VLAN WEP encryption.");
278 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
279 * @hapd: Pointer to BSS data
281 * This function is used to free all per-BSS data structures and resources.
282 * This gets called in a loop for each BSS between calls to
283 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
284 * is deinitialized. Most of the modules that are initialized in
285 * hostapd_setup_bss() are deinitialized here.
287 static void hostapd_cleanup(struct hostapd_data *hapd)
289 hostapd_ctrl_iface_deinit(hapd);
291 iapp_deinit(hapd->iapp);
293 accounting_deinit(hapd);
294 rsn_preauth_iface_deinit(hapd);
295 if (hapd->wpa_auth) {
296 wpa_deinit(hapd->wpa_auth);
297 hapd->wpa_auth = NULL;
299 if (hostapd_set_privacy(hapd, 0)) {
300 wpa_printf(MSG_DEBUG, "Could not disable "
301 "PrivacyInvoked for interface %s",
305 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
306 wpa_printf(MSG_DEBUG, "Could not remove generic "
307 "information element from interface %s",
311 ieee802_1x_deinit(hapd);
313 hostapd_acl_deinit(hapd);
314 #ifndef CONFIG_NO_RADIUS
315 radius_client_deinit(hapd->radius);
317 #endif /* CONFIG_NO_RADIUS */
319 radius_server_deinit(hapd->radius_srv);
320 hapd->radius_srv = NULL;
321 #endif /* RADIUS_SERVER */
323 #ifdef CONFIG_IEEE80211R
324 l2_packet_deinit(hapd->l2);
325 #endif /* CONFIG_IEEE80211R */
327 hostapd_deinit_wps(hapd);
331 tls_deinit(hapd->ssl_ctx);
332 hapd->ssl_ctx = NULL;
334 #endif /* EAP_TLS_FUNCS */
336 #if defined(EAP_SERVER_SIM) || defined(EAP_SERVER_AKA)
337 if (hapd->eap_sim_db_priv) {
338 eap_sim_db_deinit(hapd->eap_sim_db_priv);
339 hapd->eap_sim_db_priv = NULL;
341 #endif /* EAP_SERVER_SIM || EAP_SERVER_AKA */
343 if (hapd->interface_added &&
344 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
345 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
349 os_free(hapd->probereq_cb);
350 hapd->probereq_cb = NULL;
355 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
356 * @iface: Pointer to interface data
358 * This function is called before per-BSS data structures are deinitialized
359 * with hostapd_cleanup().
361 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
367 * hostapd_cleanup_iface - Complete per-interface cleanup
368 * @iface: Pointer to interface data
370 * This function is called after per-BSS data structures are deinitialized
371 * with hostapd_cleanup().
373 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
375 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
376 iface->hw_features = NULL;
377 os_free(iface->current_rates);
378 iface->current_rates = NULL;
379 ap_list_deinit(iface);
380 hostapd_config_free(iface->conf);
383 os_free(iface->config_fname);
389 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
393 hostapd_broadcast_wep_set(hapd);
395 if (hapd->conf->ssid.wep.default_len)
398 for (i = 0; i < 4; i++) {
399 if (hapd->conf->ssid.wep.key[i] &&
400 hostapd_set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
401 i == hapd->conf->ssid.wep.idx, NULL, 0,
402 hapd->conf->ssid.wep.key[i],
403 hapd->conf->ssid.wep.len[i])) {
404 wpa_printf(MSG_WARNING, "Could not set WEP "
408 if (hapd->conf->ssid.wep.key[i] &&
409 i == hapd->conf->ssid.wep.idx)
410 hostapd_set_privacy(hapd, 1);
417 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
421 if (hostapd_drv_none(hapd))
424 wpa_printf(MSG_DEBUG, "Flushing old station entries");
425 if (hostapd_flush(hapd)) {
426 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
429 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
431 /* New Prism2.5/3 STA firmware versions seem to have issues with this
432 * broadcast deauth frame. This gets the firmware in odd state where
433 * nothing works correctly, so let's skip sending this for the hostap
435 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
437 os_memset(addr, 0xff, ETH_ALEN);
438 hostapd_sta_deauth(hapd, addr,
439 WLAN_REASON_PREV_AUTH_NOT_VALID);
446 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
447 logger_level level, const char *txt)
449 #ifndef CONFIG_NO_HOSTAPD_LOGGER
450 struct hostapd_data *hapd = ctx;
455 hlevel = HOSTAPD_LEVEL_WARNING;
458 hlevel = HOSTAPD_LEVEL_INFO;
462 hlevel = HOSTAPD_LEVEL_DEBUG;
466 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
467 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
471 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
474 struct hostapd_data *hapd = ctx;
475 struct sta_info *sta;
477 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
478 "STA " MACSTR " reason %d",
479 __func__, MAC2STR(addr), reason);
481 sta = ap_get_sta(hapd, addr);
482 hostapd_sta_deauth(hapd, addr, reason);
485 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
486 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
487 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
488 sta->timeout_next = STA_REMOVE;
492 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
494 struct hostapd_data *hapd = ctx;
495 michael_mic_failure(hapd, addr, 0);
499 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
500 wpa_eapol_variable var, int value)
502 struct hostapd_data *hapd = ctx;
503 struct sta_info *sta = ap_get_sta(hapd, addr);
507 case WPA_EAPOL_portEnabled:
508 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
510 case WPA_EAPOL_portValid:
511 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
513 case WPA_EAPOL_authorized:
514 ieee802_1x_set_sta_authorized(hapd, sta, value);
516 case WPA_EAPOL_portControl_Auto:
518 sta->eapol_sm->portControl = Auto;
520 case WPA_EAPOL_keyRun:
522 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
524 case WPA_EAPOL_keyAvailable:
526 sta->eapol_sm->eap_if->eapKeyAvailable =
527 value ? TRUE : FALSE;
529 case WPA_EAPOL_keyDone:
531 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
533 case WPA_EAPOL_inc_EapolFramesTx:
535 sta->eapol_sm->dot1xAuthEapolFramesTx++;
541 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
542 wpa_eapol_variable var)
544 struct hostapd_data *hapd = ctx;
545 struct sta_info *sta = ap_get_sta(hapd, addr);
546 if (sta == NULL || sta->eapol_sm == NULL)
549 case WPA_EAPOL_keyRun:
550 return sta->eapol_sm->keyRun;
551 case WPA_EAPOL_keyAvailable:
552 return sta->eapol_sm->eap_if->eapKeyAvailable;
559 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
562 struct hostapd_data *hapd = ctx;
563 return hostapd_get_psk(hapd->conf, addr, prev_psk);
567 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
570 struct hostapd_data *hapd = ctx;
573 struct sta_info *sta;
575 sta = ap_get_sta(hapd, addr);
579 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
585 os_memcpy(msk, key, keylen);
592 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
593 const u8 *addr, int idx, u8 *key,
596 struct hostapd_data *hapd = ctx;
597 const char *ifname = hapd->conf->iface;
600 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
605 return hostapd_set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
610 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
613 struct hostapd_data *hapd = ctx;
614 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
618 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
619 const u8 *data, size_t data_len,
622 struct hostapd_data *hapd = ctx;
623 return hostapd_send_eapol(hapd, addr, data, data_len, encrypt);
627 static int hostapd_wpa_auth_for_each_sta(
628 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
631 struct hostapd_data *hapd = ctx;
632 struct sta_info *sta;
634 for (sta = hapd->sta_list; sta; sta = sta->next) {
635 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
642 struct wpa_auth_iface_iter_data {
643 int (*cb)(struct wpa_authenticator *sm, void *ctx);
647 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
649 struct wpa_auth_iface_iter_data *data = ctx;
651 for (i = 0; i < iface->num_bss; i++) {
652 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
659 static int hostapd_wpa_auth_for_each_auth(
660 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
663 struct hostapd_data *hapd = ctx;
664 struct wpa_auth_iface_iter_data data;
666 data.cb_ctx = cb_ctx;
667 return hostapd_for_each_interface(hapd->iface->interfaces,
668 wpa_auth_iface_iter, &data);
672 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
673 const u8 *data, size_t data_len)
675 struct hostapd_data *hapd = ctx;
677 if (hapd->driver && hapd->driver->send_ether)
678 return hapd->driver->send_ether(hapd->drv_priv, dst,
679 hapd->own_addr, proto,
681 if (hapd->l2 == NULL)
683 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
687 #ifdef CONFIG_IEEE80211R
689 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
690 const u8 *data, size_t data_len)
692 struct hostapd_data *hapd = ctx;
694 struct ieee80211_mgmt *m;
696 struct sta_info *sta;
698 sta = ap_get_sta(hapd, dst);
699 if (sta == NULL || sta->wpa_sm == NULL)
702 m = os_zalloc(sizeof(*m) + data_len);
705 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
706 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
707 WLAN_FC_STYPE_ACTION);
708 os_memcpy(m->da, dst, ETH_ALEN);
709 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
710 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
711 os_memcpy(&m->u, data, data_len);
713 res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
719 static struct wpa_state_machine *
720 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
722 struct hostapd_data *hapd = ctx;
723 struct sta_info *sta;
725 sta = ap_sta_add(hapd, sta_addr);
731 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
732 if (sta->wpa_sm == NULL) {
733 ap_free_sta(hapd, sta);
736 sta->auth_alg = WLAN_AUTH_FT;
742 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
745 struct hostapd_data *hapd = ctx;
746 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
749 #endif /* CONFIG_IEEE80211R */
753 * hostapd_validate_bssid_configuration - Validate BSSID configuration
754 * @iface: Pointer to interface data
755 * Returns: 0 on success, -1 on failure
757 * This function is used to validate that the configured BSSIDs are valid.
759 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
761 u8 mask[ETH_ALEN] = { 0 };
762 struct hostapd_data *hapd = iface->bss[0];
763 unsigned int i = iface->conf->num_bss, bits = 0, j;
767 if (hostapd_drv_none(hapd))
770 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
772 /* Determine the bits necessary to cover the number of BSSIDs. */
773 for (i--; i; i >>= 1)
776 /* Determine the bits necessary to any configured BSSIDs,
777 if they are higher than the number of BSSIDs. */
778 for (j = 0; j < iface->conf->num_bss; j++) {
779 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
785 for (i = 0; i < ETH_ALEN; i++) {
787 iface->conf->bss[j].bssid[i] ^
795 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
801 while (mask[i] != 0) {
811 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
816 os_memset(mask, 0xff, ETH_ALEN);
818 for (i = 5; i > 5 - j; i--)
825 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
826 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
828 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
833 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
834 MACSTR " for start address " MACSTR ".",
835 MAC2STR(mask), MAC2STR(hapd->own_addr));
842 for (i = 0; i < ETH_ALEN; i++) {
843 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
844 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
845 " for start address " MACSTR ".",
846 MAC2STR(mask), MAC2STR(hapd->own_addr));
847 wpa_printf(MSG_ERROR, "Start address must be the "
848 "first address in the block (i.e., addr "
849 "AND mask == addr).");
858 static int mac_in_conf(struct hostapd_config *conf, const void *a)
862 for (i = 0; i < conf->num_bss; i++) {
863 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
872 static int hostapd_setup_wpa(struct hostapd_data *hapd)
874 struct wpa_auth_config _conf;
875 struct wpa_auth_callbacks cb;
879 hostapd_wpa_auth_conf(hapd->conf, &_conf);
880 os_memset(&cb, 0, sizeof(cb));
882 cb.logger = hostapd_wpa_auth_logger;
883 cb.disconnect = hostapd_wpa_auth_disconnect;
884 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
885 cb.set_eapol = hostapd_wpa_auth_set_eapol;
886 cb.get_eapol = hostapd_wpa_auth_get_eapol;
887 cb.get_psk = hostapd_wpa_auth_get_psk;
888 cb.get_msk = hostapd_wpa_auth_get_msk;
889 cb.set_key = hostapd_wpa_auth_set_key;
890 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
891 cb.send_eapol = hostapd_wpa_auth_send_eapol;
892 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
893 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
894 cb.send_ether = hostapd_wpa_auth_send_ether;
895 #ifdef CONFIG_IEEE80211R
896 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
897 cb.add_sta = hostapd_wpa_auth_add_sta;
898 #endif /* CONFIG_IEEE80211R */
899 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
900 if (hapd->wpa_auth == NULL) {
901 wpa_printf(MSG_ERROR, "WPA initialization failed.");
905 if (hostapd_set_privacy(hapd, 1)) {
906 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
907 "for interface %s", hapd->conf->iface);
911 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
912 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
913 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
914 "the kernel driver.");
918 if (rsn_preauth_iface_init(hapd)) {
919 wpa_printf(MSG_ERROR, "Initialization of RSN "
920 "pre-authentication failed.");
931 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
932 size_t identity_len, int phase2,
933 struct eap_user *user)
935 const struct hostapd_eap_user *eap_user;
938 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
939 if (eap_user == NULL)
945 os_memset(user, 0, sizeof(*user));
946 count = EAP_USER_MAX_METHODS;
947 if (count > EAP_MAX_METHODS)
948 count = EAP_MAX_METHODS;
949 for (i = 0; i < count; i++) {
950 user->methods[i].vendor = eap_user->methods[i].vendor;
951 user->methods[i].method = eap_user->methods[i].method;
954 if (eap_user->password) {
955 user->password = os_malloc(eap_user->password_len);
956 if (user->password == NULL)
958 os_memcpy(user->password, eap_user->password,
959 eap_user->password_len);
960 user->password_len = eap_user->password_len;
961 user->password_hash = eap_user->password_hash;
963 user->force_version = eap_user->force_version;
964 user->ttls_auth = eap_user->ttls_auth;
970 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
971 struct hostapd_bss_config *conf)
973 struct radius_server_conf srv;
974 os_memset(&srv, 0, sizeof(srv));
975 srv.client_file = conf->radius_server_clients;
976 srv.auth_port = conf->radius_server_auth_port;
978 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
979 srv.ssl_ctx = hapd->ssl_ctx;
980 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
981 srv.eap_fast_a_id = conf->eap_fast_a_id;
982 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
983 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
984 srv.eap_fast_prov = conf->eap_fast_prov;
985 srv.pac_key_lifetime = conf->pac_key_lifetime;
986 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
987 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
990 srv.ipv6 = conf->radius_server_ipv6;
991 srv.get_eap_user = hostapd_radius_get_eap_user;
992 srv.eap_req_id_text = conf->eap_req_id_text;
993 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
995 hapd->radius_srv = radius_server_init(&srv);
996 if (hapd->radius_srv == NULL) {
997 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1004 #endif /* RADIUS_SERVER */
1008 * hostapd_setup_bss - Per-BSS setup (initialization)
1009 * @hapd: Pointer to BSS data
1010 * @first: Whether this BSS is the first BSS of an interface
1012 * This function is used to initialize all per-BSS data structures and
1013 * resources. This gets called in a loop for each BSS when an interface is
1014 * initialized. Most of the modules that are initialized here will be
1015 * deinitialized in hostapd_cleanup().
1017 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1019 struct hostapd_bss_config *conf = hapd->conf;
1020 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1021 int ssid_len, set_ssid;
1024 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1025 /* Allocate the next available BSSID. */
1027 inc_byte_array(hapd->own_addr, ETH_ALEN);
1028 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1030 /* Allocate the configured BSSID. */
1031 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1033 if (hostapd_mac_comp(hapd->own_addr,
1034 hapd->iface->bss[0]->own_addr) ==
1036 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1037 "BSSID set to the MAC address of "
1038 "the radio", hapd->conf->iface);
1043 hapd->interface_added = 1;
1044 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1045 hapd->conf->iface, hapd->own_addr, hapd)) {
1046 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1047 MACSTR ")", MAC2STR(hapd->own_addr));
1052 hostapd_flush_old_stations(hapd);
1053 hostapd_set_privacy(hapd, 0);
1055 hostapd_broadcast_wep_clear(hapd);
1056 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1060 * Fetch the SSID from the system and use it or,
1061 * if one was specified in the config file, verify they
1064 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1066 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1069 if (conf->ssid.ssid_set) {
1071 * If SSID is specified in the config file and it differs
1072 * from what is being used then force installation of the
1075 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1076 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1079 * No SSID in the config file; just use the one we got
1083 conf->ssid.ssid_len = ssid_len;
1084 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1085 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1088 if (!hostapd_drv_none(hapd)) {
1089 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1091 hapd->conf->iface, MAC2STR(hapd->own_addr),
1092 hapd->conf->ssid.ssid);
1095 if (hostapd_setup_wpa_psk(conf)) {
1096 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1100 /* Set SSID for the kernel driver (to be used in beacon and probe
1101 * response frames) */
1102 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1103 conf->ssid.ssid_len)) {
1104 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1108 if (wpa_debug_level == MSG_MSGDUMP)
1109 conf->radius->msg_dumps = 1;
1110 #ifndef CONFIG_NO_RADIUS
1111 hapd->radius = radius_client_init(hapd, conf->radius);
1112 if (hapd->radius == NULL) {
1113 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1116 #endif /* CONFIG_NO_RADIUS */
1118 if (hostapd_acl_init(hapd)) {
1119 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1122 if (hostapd_init_wps(hapd, conf))
1125 if (ieee802_1x_init(hapd)) {
1126 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1130 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1133 if (accounting_init(hapd)) {
1134 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1138 if (hapd->conf->ieee802_11f &&
1139 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1140 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1145 if (hostapd_ctrl_iface_init(hapd)) {
1146 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1150 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1151 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1155 #ifdef CONFIG_IEEE80211R
1156 if (!hostapd_drv_none(hapd)) {
1157 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1158 hostapd_rrb_receive, hapd, 0);
1159 if (hapd->l2 == NULL &&
1160 (hapd->driver == NULL ||
1161 hapd->driver->send_ether == NULL)) {
1162 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1167 #endif /* CONFIG_IEEE80211R */
1169 ieee802_11_set_beacon(hapd);
1171 #ifdef RADIUS_SERVER
1172 if (conf->radius_server_clients &&
1173 hostapd_setup_radius_srv(hapd, conf))
1175 #endif /* RADIUS_SERVER */
1181 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1183 struct hostapd_data *hapd = iface->bss[0];
1185 struct hostapd_tx_queue_params *p;
1187 for (i = 0; i < NUM_TX_QUEUES; i++) {
1188 p = &iface->conf->tx_queue[i];
1193 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1194 p->cwmax, p->burst)) {
1195 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1196 "parameters for queue %d.", i);
1197 /* Continue anyway */
1203 static int setup_interface(struct hostapd_iface *iface)
1205 struct hostapd_data *hapd = iface->bss[0];
1206 struct hostapd_bss_config *conf = hapd->conf;
1209 u8 *b = conf->bssid;
1212 * Initialize the driver interface and make sure that all BSSes get
1213 * configured with a pointer to this driver interface.
1215 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
1217 hapd->drv_priv = hostapd_driver_init(hapd, b);
1219 if (hapd->drv_priv == NULL) {
1220 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1221 hapd->driver ? hapd->driver->name : "Unknown");
1222 hapd->driver = NULL;
1225 for (i = 0; i < iface->num_bss; i++) {
1226 iface->bss[i]->driver = hapd->driver;
1227 iface->bss[i]->drv_priv = hapd->drv_priv;
1230 if (hostapd_validate_bssid_configuration(iface))
1233 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1234 os_memcpy(country, hapd->iconf->country, 3);
1236 if (hostapd_set_country(hapd, country) < 0) {
1237 wpa_printf(MSG_ERROR, "Failed to set country code");
1242 if (hostapd_get_hw_features(iface)) {
1243 /* Not all drivers support this yet, so continue without hw
1246 int ret = hostapd_select_hw_mode(iface);
1248 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1249 "channel. (%d)", ret);
1252 ret = hostapd_check_ht_capab(iface);
1256 wpa_printf(MSG_DEBUG, "Interface initialization will "
1257 "be completed in a callback");
1261 return hostapd_setup_interface_complete(iface, 0);
1265 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1267 struct hostapd_data *hapd = iface->bss[0];
1273 wpa_printf(MSG_ERROR, "Interface initialization failed");
1278 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1279 if (hapd->iconf->channel) {
1280 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1281 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1282 "Frequency: %d MHz",
1283 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1284 hapd->iconf->channel, freq);
1286 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1287 hapd->iconf->channel,
1288 hapd->iconf->ieee80211n,
1289 hapd->iconf->secondary_channel)) {
1290 wpa_printf(MSG_ERROR, "Could not set channel for "
1296 if (hapd->iconf->rts_threshold > -1 &&
1297 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1298 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1303 if (hapd->iconf->fragm_threshold > -1 &&
1304 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1305 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1306 "for kernel driver");
1310 prev_addr = hapd->own_addr;
1312 for (j = 0; j < iface->num_bss; j++) {
1313 hapd = iface->bss[j];
1315 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1316 if (hostapd_setup_bss(hapd, j == 0))
1318 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1319 prev_addr = hapd->own_addr;
1322 hostapd_tx_queue_params(iface);
1324 ap_list_init(iface);
1326 if (hostapd_driver_commit(hapd) < 0) {
1327 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1328 "configuration", __func__);
1332 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1333 iface->bss[0]->conf->iface);
1340 * hostapd_setup_interface - Setup of an interface
1341 * @iface: Pointer to interface data.
1342 * Returns: 0 on success, -1 on failure
1344 * Initializes the driver interface, validates the configuration,
1345 * and sets driver parameters based on the configuration.
1346 * Flushes old stations, sets the channel, encryption,
1347 * beacons, and WDS links based on the configuration.
1349 int hostapd_setup_interface(struct hostapd_iface *iface)
1353 ret = setup_interface(iface);
1355 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1356 iface->bss[0]->conf->iface);
1366 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1367 * @hapd_iface: Pointer to interface data
1368 * @conf: Pointer to per-interface configuration
1369 * @bss: Pointer to per-BSS configuration for this BSS
1370 * Returns: Pointer to allocated BSS data
1372 * This function is used to allocate per-BSS data structure. This data will be
1373 * freed after hostapd_cleanup() is called for it during interface
1376 struct hostapd_data *
1377 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1378 struct hostapd_config *conf,
1379 struct hostapd_bss_config *bss)
1381 struct hostapd_data *hapd;
1383 hapd = os_zalloc(sizeof(*hapd));
1387 hostapd_set_driver_ops(&hapd->drv);
1390 hapd->iface = hapd_iface;
1392 #ifdef EAP_TLS_FUNCS
1393 if (hapd->conf->eap_server &&
1394 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1395 hapd->conf->dh_file)) {
1396 struct tls_connection_params params;
1398 hapd->ssl_ctx = tls_init(NULL);
1399 if (hapd->ssl_ctx == NULL) {
1400 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1404 os_memset(¶ms, 0, sizeof(params));
1405 params.ca_cert = hapd->conf->ca_cert;
1406 params.client_cert = hapd->conf->server_cert;
1407 params.private_key = hapd->conf->private_key;
1408 params.private_key_passwd = hapd->conf->private_key_passwd;
1409 params.dh_file = hapd->conf->dh_file;
1411 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1412 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1416 if (tls_global_set_verify(hapd->ssl_ctx,
1417 hapd->conf->check_crl)) {
1418 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1422 #endif /* EAP_TLS_FUNCS */
1425 if (hapd->conf->eap_sim_db) {
1426 hapd->eap_sim_db_priv =
1427 eap_sim_db_init(hapd->conf->eap_sim_db,
1428 hostapd_sim_db_cb, hapd);
1429 if (hapd->eap_sim_db_priv == NULL) {
1430 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1431 "database interface");
1435 #endif /* EAP_SIM_DB */
1437 hapd->driver = hapd->iconf->driver;
1441 #if defined(EAP_TLS_FUNCS) || defined(EAP_SIM_DB)
1444 /* TODO: cleanup allocated resources(?) */
1450 void hostapd_interface_deinit(struct hostapd_iface *iface)
1457 hostapd_cleanup_iface_pre(iface);
1458 for (j = 0; j < iface->num_bss; j++) {
1459 struct hostapd_data *hapd = iface->bss[j];
1460 hostapd_free_stas(hapd);
1461 hostapd_flush_old_stations(hapd);
1462 hostapd_cleanup(hapd);
1463 if (j == iface->num_bss - 1 && hapd->driver)
1464 hostapd_driver_deinit(hapd);
1466 for (j = 0; j < iface->num_bss; j++)
1467 os_free(iface->bss[j]);
1468 hostapd_cleanup_iface(iface);
1472 int hostapd_register_probereq_cb(struct hostapd_data *hapd,
1473 void (*cb)(void *ctx, const u8 *sa,
1474 const u8 *ie, size_t ie_len),
1477 struct hostapd_probereq_cb *n;
1479 n = os_realloc(hapd->probereq_cb, (hapd->num_probereq_cb + 1) *
1480 sizeof(struct hostapd_probereq_cb));
1484 hapd->probereq_cb = n;
1485 n = &hapd->probereq_cb[hapd->num_probereq_cb];
1486 hapd->num_probereq_cb++;
1495 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
1498 struct wpa_bss_params params;
1499 os_memset(¶ms, 0, sizeof(params));
1500 params.ifname = ifname;
1501 params.enabled = enabled;
1503 params.wpa = hapd->conf->wpa;
1504 params.ieee802_1x = hapd->conf->ieee802_1x;
1505 params.wpa_group = hapd->conf->wpa_group;
1506 params.wpa_pairwise = hapd->conf->wpa_pairwise;
1507 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
1508 params.rsn_preauth = hapd->conf->rsn_preauth;
1510 return hostapd_set_ieee8021x(hapd, ¶ms);
1514 int hostapd_sta_flags_to_drv(int flags)
1517 if (flags & WLAN_STA_AUTHORIZED)
1518 res |= WPA_STA_AUTHORIZED;
1519 if (flags & WLAN_STA_WMM)
1521 if (flags & WLAN_STA_SHORT_PREAMBLE)
1522 res |= WPA_STA_SHORT_PREAMBLE;
1523 if (flags & WLAN_STA_MFP)