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 (hapd->drv.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 (hapd->drv.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 hapd->drv.set_key(hapd->conf->iface,
245 hapd, WPA_ALG_WEP, NULL, idx,
246 idx == ssid->wep.idx,
247 NULL, 0, ssid->wep.key[idx],
248 ssid->wep.len[idx])) {
249 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
253 if (ssid->dyn_vlan_keys) {
255 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
257 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
260 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
266 if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL,
267 idx, idx == key->idx, NULL, 0,
268 key->key[idx], key->len[idx])) {
269 wpa_printf(MSG_WARNING, "Could not set "
270 "dynamic VLAN WEP encryption.");
280 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
281 * @hapd: Pointer to BSS data
283 * This function is used to free all per-BSS data structures and resources.
284 * This gets called in a loop for each BSS between calls to
285 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
286 * is deinitialized. Most of the modules that are initialized in
287 * hostapd_setup_bss() are deinitialized here.
289 static void hostapd_cleanup(struct hostapd_data *hapd)
291 hostapd_ctrl_iface_deinit(hapd);
293 iapp_deinit(hapd->iapp);
295 accounting_deinit(hapd);
296 rsn_preauth_iface_deinit(hapd);
297 if (hapd->wpa_auth) {
298 wpa_deinit(hapd->wpa_auth);
299 hapd->wpa_auth = NULL;
301 if (hostapd_set_privacy(hapd, 0)) {
302 wpa_printf(MSG_DEBUG, "Could not disable "
303 "PrivacyInvoked for interface %s",
307 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
308 wpa_printf(MSG_DEBUG, "Could not remove generic "
309 "information element from interface %s",
313 ieee802_1x_deinit(hapd);
315 hostapd_acl_deinit(hapd);
316 #ifndef CONFIG_NO_RADIUS
317 radius_client_deinit(hapd->radius);
319 #endif /* CONFIG_NO_RADIUS */
321 radius_server_deinit(hapd->radius_srv);
322 hapd->radius_srv = NULL;
323 #endif /* RADIUS_SERVER */
325 #ifdef CONFIG_IEEE80211R
326 l2_packet_deinit(hapd->l2);
327 #endif /* CONFIG_IEEE80211R */
329 hostapd_deinit_wps(hapd);
333 tls_deinit(hapd->ssl_ctx);
334 hapd->ssl_ctx = NULL;
336 #endif /* EAP_TLS_FUNCS */
338 #if defined(EAP_SERVER_SIM) || defined(EAP_SERVER_AKA)
339 if (hapd->eap_sim_db_priv) {
340 eap_sim_db_deinit(hapd->eap_sim_db_priv);
341 hapd->eap_sim_db_priv = NULL;
343 #endif /* EAP_SERVER_SIM || EAP_SERVER_AKA */
345 if (hapd->interface_added &&
346 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
347 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
351 os_free(hapd->probereq_cb);
352 hapd->probereq_cb = NULL;
357 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
358 * @iface: Pointer to interface data
360 * This function is called before per-BSS data structures are deinitialized
361 * with hostapd_cleanup().
363 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
369 * hostapd_cleanup_iface - Complete per-interface cleanup
370 * @iface: Pointer to interface data
372 * This function is called after per-BSS data structures are deinitialized
373 * with hostapd_cleanup().
375 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
377 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
378 iface->hw_features = NULL;
379 os_free(iface->current_rates);
380 iface->current_rates = NULL;
381 ap_list_deinit(iface);
382 hostapd_config_free(iface->conf);
385 os_free(iface->config_fname);
391 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
395 hostapd_broadcast_wep_set(hapd);
397 if (hapd->conf->ssid.wep.default_len) {
398 hostapd_set_privacy(hapd, 1);
402 for (i = 0; i < 4; i++) {
403 if (hapd->conf->ssid.wep.key[i] &&
404 hapd->drv.set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
405 i == hapd->conf->ssid.wep.idx, NULL, 0,
406 hapd->conf->ssid.wep.key[i],
407 hapd->conf->ssid.wep.len[i])) {
408 wpa_printf(MSG_WARNING, "Could not set WEP "
412 if (hapd->conf->ssid.wep.key[i] &&
413 i == hapd->conf->ssid.wep.idx)
414 hostapd_set_privacy(hapd, 1);
421 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
425 if (hostapd_drv_none(hapd))
428 wpa_printf(MSG_DEBUG, "Flushing old station entries");
429 if (hostapd_flush(hapd)) {
430 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
433 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
435 /* New Prism2.5/3 STA firmware versions seem to have issues with this
436 * broadcast deauth frame. This gets the firmware in odd state where
437 * nothing works correctly, so let's skip sending this for the hostap
439 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
441 os_memset(addr, 0xff, ETH_ALEN);
442 hostapd_sta_deauth(hapd, addr,
443 WLAN_REASON_PREV_AUTH_NOT_VALID);
450 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
451 logger_level level, const char *txt)
453 #ifndef CONFIG_NO_HOSTAPD_LOGGER
454 struct hostapd_data *hapd = ctx;
459 hlevel = HOSTAPD_LEVEL_WARNING;
462 hlevel = HOSTAPD_LEVEL_INFO;
466 hlevel = HOSTAPD_LEVEL_DEBUG;
470 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
471 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
475 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
478 struct hostapd_data *hapd = ctx;
479 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
480 "STA " MACSTR " reason %d",
481 __func__, MAC2STR(addr), reason);
482 ap_sta_disconnect(hapd, NULL, addr, reason);
486 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
488 struct hostapd_data *hapd = ctx;
489 michael_mic_failure(hapd, addr, 0);
493 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
494 wpa_eapol_variable var, int value)
496 struct hostapd_data *hapd = ctx;
497 struct sta_info *sta = ap_get_sta(hapd, addr);
501 case WPA_EAPOL_portEnabled:
502 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
504 case WPA_EAPOL_portValid:
505 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
507 case WPA_EAPOL_authorized:
508 ieee802_1x_set_sta_authorized(hapd, sta, value);
510 case WPA_EAPOL_portControl_Auto:
512 sta->eapol_sm->portControl = Auto;
514 case WPA_EAPOL_keyRun:
516 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
518 case WPA_EAPOL_keyAvailable:
520 sta->eapol_sm->eap_if->eapKeyAvailable =
521 value ? TRUE : FALSE;
523 case WPA_EAPOL_keyDone:
525 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
527 case WPA_EAPOL_inc_EapolFramesTx:
529 sta->eapol_sm->dot1xAuthEapolFramesTx++;
535 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
536 wpa_eapol_variable var)
538 struct hostapd_data *hapd = ctx;
539 struct sta_info *sta = ap_get_sta(hapd, addr);
540 if (sta == NULL || sta->eapol_sm == NULL)
543 case WPA_EAPOL_keyRun:
544 return sta->eapol_sm->keyRun;
545 case WPA_EAPOL_keyAvailable:
546 return sta->eapol_sm->eap_if->eapKeyAvailable;
553 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
556 struct hostapd_data *hapd = ctx;
557 return hostapd_get_psk(hapd->conf, addr, prev_psk);
561 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
564 struct hostapd_data *hapd = ctx;
567 struct sta_info *sta;
569 sta = ap_get_sta(hapd, addr);
573 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
579 os_memcpy(msk, key, keylen);
586 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
587 const u8 *addr, int idx, u8 *key,
590 struct hostapd_data *hapd = ctx;
591 const char *ifname = hapd->conf->iface;
594 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
599 return hapd->drv.set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
604 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
607 struct hostapd_data *hapd = ctx;
608 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
612 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
613 const u8 *data, size_t data_len,
616 struct hostapd_data *hapd = ctx;
617 return hapd->drv.send_eapol(hapd, addr, data, data_len, encrypt);
621 static int hostapd_wpa_auth_for_each_sta(
622 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
625 struct hostapd_data *hapd = ctx;
626 struct sta_info *sta;
628 for (sta = hapd->sta_list; sta; sta = sta->next) {
629 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
636 struct wpa_auth_iface_iter_data {
637 int (*cb)(struct wpa_authenticator *sm, void *ctx);
641 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
643 struct wpa_auth_iface_iter_data *data = ctx;
645 for (i = 0; i < iface->num_bss; i++) {
646 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
653 static int hostapd_wpa_auth_for_each_auth(
654 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
657 struct hostapd_data *hapd = ctx;
658 struct wpa_auth_iface_iter_data data;
660 data.cb_ctx = cb_ctx;
661 return hostapd_for_each_interface(hapd->iface->interfaces,
662 wpa_auth_iface_iter, &data);
666 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
667 const u8 *data, size_t data_len)
669 struct hostapd_data *hapd = ctx;
671 if (hapd->driver && hapd->driver->send_ether)
672 return hapd->driver->send_ether(hapd->drv_priv, dst,
673 hapd->own_addr, proto,
675 if (hapd->l2 == NULL)
677 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
681 #ifdef CONFIG_IEEE80211R
683 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
684 const u8 *data, size_t data_len)
686 struct hostapd_data *hapd = ctx;
688 struct ieee80211_mgmt *m;
690 struct sta_info *sta;
692 sta = ap_get_sta(hapd, dst);
693 if (sta == NULL || sta->wpa_sm == NULL)
696 m = os_zalloc(sizeof(*m) + data_len);
699 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
700 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
701 WLAN_FC_STYPE_ACTION);
702 os_memcpy(m->da, dst, ETH_ALEN);
703 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
704 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
705 os_memcpy(&m->u, data, data_len);
707 res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
713 static struct wpa_state_machine *
714 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
716 struct hostapd_data *hapd = ctx;
717 struct sta_info *sta;
719 sta = ap_sta_add(hapd, sta_addr);
725 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
726 if (sta->wpa_sm == NULL) {
727 ap_free_sta(hapd, sta);
730 sta->auth_alg = WLAN_AUTH_FT;
736 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
739 struct hostapd_data *hapd = ctx;
740 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
743 #endif /* CONFIG_IEEE80211R */
747 * hostapd_validate_bssid_configuration - Validate BSSID configuration
748 * @iface: Pointer to interface data
749 * Returns: 0 on success, -1 on failure
751 * This function is used to validate that the configured BSSIDs are valid.
753 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
755 u8 mask[ETH_ALEN] = { 0 };
756 struct hostapd_data *hapd = iface->bss[0];
757 unsigned int i = iface->conf->num_bss, bits = 0, j;
761 if (hostapd_drv_none(hapd))
764 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
766 /* Determine the bits necessary to cover the number of BSSIDs. */
767 for (i--; i; i >>= 1)
770 /* Determine the bits necessary to any configured BSSIDs,
771 if they are higher than the number of BSSIDs. */
772 for (j = 0; j < iface->conf->num_bss; j++) {
773 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
779 for (i = 0; i < ETH_ALEN; i++) {
781 iface->conf->bss[j].bssid[i] ^
789 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
795 while (mask[i] != 0) {
805 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
810 os_memset(mask, 0xff, ETH_ALEN);
812 for (i = 5; i > 5 - j; i--)
819 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
820 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
822 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
827 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
828 MACSTR " for start address " MACSTR ".",
829 MAC2STR(mask), MAC2STR(hapd->own_addr));
836 for (i = 0; i < ETH_ALEN; i++) {
837 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
838 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
839 " for start address " MACSTR ".",
840 MAC2STR(mask), MAC2STR(hapd->own_addr));
841 wpa_printf(MSG_ERROR, "Start address must be the "
842 "first address in the block (i.e., addr "
843 "AND mask == addr).");
852 static int mac_in_conf(struct hostapd_config *conf, const void *a)
856 for (i = 0; i < conf->num_bss; i++) {
857 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
866 static int hostapd_setup_wpa(struct hostapd_data *hapd)
868 struct wpa_auth_config _conf;
869 struct wpa_auth_callbacks cb;
873 hostapd_wpa_auth_conf(hapd->conf, &_conf);
874 os_memset(&cb, 0, sizeof(cb));
876 cb.logger = hostapd_wpa_auth_logger;
877 cb.disconnect = hostapd_wpa_auth_disconnect;
878 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
879 cb.set_eapol = hostapd_wpa_auth_set_eapol;
880 cb.get_eapol = hostapd_wpa_auth_get_eapol;
881 cb.get_psk = hostapd_wpa_auth_get_psk;
882 cb.get_msk = hostapd_wpa_auth_get_msk;
883 cb.set_key = hostapd_wpa_auth_set_key;
884 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
885 cb.send_eapol = hostapd_wpa_auth_send_eapol;
886 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
887 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
888 cb.send_ether = hostapd_wpa_auth_send_ether;
889 #ifdef CONFIG_IEEE80211R
890 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
891 cb.add_sta = hostapd_wpa_auth_add_sta;
892 #endif /* CONFIG_IEEE80211R */
893 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
894 if (hapd->wpa_auth == NULL) {
895 wpa_printf(MSG_ERROR, "WPA initialization failed.");
899 if (hostapd_set_privacy(hapd, 1)) {
900 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
901 "for interface %s", hapd->conf->iface);
905 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
906 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
907 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
908 "the kernel driver.");
912 if (rsn_preauth_iface_init(hapd)) {
913 wpa_printf(MSG_ERROR, "Initialization of RSN "
914 "pre-authentication failed.");
925 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
926 size_t identity_len, int phase2,
927 struct eap_user *user)
929 const struct hostapd_eap_user *eap_user;
932 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
933 if (eap_user == NULL)
939 os_memset(user, 0, sizeof(*user));
940 count = EAP_USER_MAX_METHODS;
941 if (count > EAP_MAX_METHODS)
942 count = EAP_MAX_METHODS;
943 for (i = 0; i < count; i++) {
944 user->methods[i].vendor = eap_user->methods[i].vendor;
945 user->methods[i].method = eap_user->methods[i].method;
948 if (eap_user->password) {
949 user->password = os_malloc(eap_user->password_len);
950 if (user->password == NULL)
952 os_memcpy(user->password, eap_user->password,
953 eap_user->password_len);
954 user->password_len = eap_user->password_len;
955 user->password_hash = eap_user->password_hash;
957 user->force_version = eap_user->force_version;
958 user->ttls_auth = eap_user->ttls_auth;
964 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
965 struct hostapd_bss_config *conf)
967 struct radius_server_conf srv;
968 os_memset(&srv, 0, sizeof(srv));
969 srv.client_file = conf->radius_server_clients;
970 srv.auth_port = conf->radius_server_auth_port;
972 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
973 srv.ssl_ctx = hapd->ssl_ctx;
974 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
975 srv.eap_fast_a_id = conf->eap_fast_a_id;
976 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
977 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
978 srv.eap_fast_prov = conf->eap_fast_prov;
979 srv.pac_key_lifetime = conf->pac_key_lifetime;
980 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
981 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
984 srv.ipv6 = conf->radius_server_ipv6;
985 srv.get_eap_user = hostapd_radius_get_eap_user;
986 srv.eap_req_id_text = conf->eap_req_id_text;
987 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
989 hapd->radius_srv = radius_server_init(&srv);
990 if (hapd->radius_srv == NULL) {
991 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
998 #endif /* RADIUS_SERVER */
1002 * hostapd_setup_bss - Per-BSS setup (initialization)
1003 * @hapd: Pointer to BSS data
1004 * @first: Whether this BSS is the first BSS of an interface
1006 * This function is used to initialize all per-BSS data structures and
1007 * resources. This gets called in a loop for each BSS when an interface is
1008 * initialized. Most of the modules that are initialized here will be
1009 * deinitialized in hostapd_cleanup().
1011 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1013 struct hostapd_bss_config *conf = hapd->conf;
1014 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1015 int ssid_len, set_ssid;
1018 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1019 /* Allocate the next available BSSID. */
1021 inc_byte_array(hapd->own_addr, ETH_ALEN);
1022 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1024 /* Allocate the configured BSSID. */
1025 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1027 if (hostapd_mac_comp(hapd->own_addr,
1028 hapd->iface->bss[0]->own_addr) ==
1030 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1031 "BSSID set to the MAC address of "
1032 "the radio", hapd->conf->iface);
1037 hapd->interface_added = 1;
1038 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1039 hapd->conf->iface, hapd->own_addr, hapd)) {
1040 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1041 MACSTR ")", MAC2STR(hapd->own_addr));
1046 hostapd_flush_old_stations(hapd);
1047 hostapd_set_privacy(hapd, 0);
1049 hostapd_broadcast_wep_clear(hapd);
1050 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1054 * Fetch the SSID from the system and use it or,
1055 * if one was specified in the config file, verify they
1058 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1060 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1063 if (conf->ssid.ssid_set) {
1065 * If SSID is specified in the config file and it differs
1066 * from what is being used then force installation of the
1069 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1070 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1073 * No SSID in the config file; just use the one we got
1077 conf->ssid.ssid_len = ssid_len;
1078 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1079 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1082 if (!hostapd_drv_none(hapd)) {
1083 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1085 hapd->conf->iface, MAC2STR(hapd->own_addr),
1086 hapd->conf->ssid.ssid);
1089 if (hostapd_setup_wpa_psk(conf)) {
1090 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1094 /* Set SSID for the kernel driver (to be used in beacon and probe
1095 * response frames) */
1096 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1097 conf->ssid.ssid_len)) {
1098 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1102 if (wpa_debug_level == MSG_MSGDUMP)
1103 conf->radius->msg_dumps = 1;
1104 #ifndef CONFIG_NO_RADIUS
1105 hapd->radius = radius_client_init(hapd, conf->radius);
1106 if (hapd->radius == NULL) {
1107 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1110 #endif /* CONFIG_NO_RADIUS */
1112 if (hostapd_acl_init(hapd)) {
1113 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1116 if (hostapd_init_wps(hapd, conf))
1119 if (ieee802_1x_init(hapd)) {
1120 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1124 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1127 if (accounting_init(hapd)) {
1128 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1132 if (hapd->conf->ieee802_11f &&
1133 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1134 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1139 if (hostapd_ctrl_iface_init(hapd)) {
1140 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1144 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1145 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1149 #ifdef CONFIG_IEEE80211R
1150 if (!hostapd_drv_none(hapd)) {
1151 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1152 hostapd_rrb_receive, hapd, 0);
1153 if (hapd->l2 == NULL &&
1154 (hapd->driver == NULL ||
1155 hapd->driver->send_ether == NULL)) {
1156 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1161 #endif /* CONFIG_IEEE80211R */
1163 ieee802_11_set_beacon(hapd);
1165 #ifdef RADIUS_SERVER
1166 if (conf->radius_server_clients &&
1167 hostapd_setup_radius_srv(hapd, conf))
1169 #endif /* RADIUS_SERVER */
1175 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1177 struct hostapd_data *hapd = iface->bss[0];
1179 struct hostapd_tx_queue_params *p;
1181 for (i = 0; i < NUM_TX_QUEUES; i++) {
1182 p = &iface->conf->tx_queue[i];
1187 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1188 p->cwmax, p->burst)) {
1189 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1190 "parameters for queue %d.", i);
1191 /* Continue anyway */
1197 static int setup_interface(struct hostapd_iface *iface)
1199 struct hostapd_data *hapd = iface->bss[0];
1200 struct hostapd_bss_config *conf = hapd->conf;
1203 u8 *b = conf->bssid;
1206 * Initialize the driver interface and make sure that all BSSes get
1207 * configured with a pointer to this driver interface.
1209 if (!(b[0] | b[1] | b[2] | b[3] | b[4] | b[5]))
1211 hapd->drv_priv = hostapd_driver_init(hapd, b);
1213 if (hapd->drv_priv == NULL) {
1214 wpa_printf(MSG_ERROR, "%s driver initialization failed.",
1215 hapd->driver ? hapd->driver->name : "Unknown");
1216 hapd->driver = NULL;
1219 for (i = 0; i < iface->num_bss; i++) {
1220 iface->bss[i]->driver = hapd->driver;
1221 iface->bss[i]->drv_priv = hapd->drv_priv;
1224 if (hostapd_validate_bssid_configuration(iface))
1227 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1228 os_memcpy(country, hapd->iconf->country, 3);
1230 if (hostapd_set_country(hapd, country) < 0) {
1231 wpa_printf(MSG_ERROR, "Failed to set country code");
1236 if (hostapd_get_hw_features(iface)) {
1237 /* Not all drivers support this yet, so continue without hw
1240 int ret = hostapd_select_hw_mode(iface);
1242 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1243 "channel. (%d)", ret);
1246 ret = hostapd_check_ht_capab(iface);
1250 wpa_printf(MSG_DEBUG, "Interface initialization will "
1251 "be completed in a callback");
1255 return hostapd_setup_interface_complete(iface, 0);
1259 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1261 struct hostapd_data *hapd = iface->bss[0];
1267 wpa_printf(MSG_ERROR, "Interface initialization failed");
1272 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1273 if (hapd->iconf->channel) {
1274 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1275 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1276 "Frequency: %d MHz",
1277 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1278 hapd->iconf->channel, freq);
1280 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1281 hapd->iconf->channel,
1282 hapd->iconf->ieee80211n,
1283 hapd->iconf->secondary_channel)) {
1284 wpa_printf(MSG_ERROR, "Could not set channel for "
1290 if (hapd->iconf->rts_threshold > -1 &&
1291 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1292 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1297 if (hapd->iconf->fragm_threshold > -1 &&
1298 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1299 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1300 "for kernel driver");
1304 prev_addr = hapd->own_addr;
1306 for (j = 0; j < iface->num_bss; j++) {
1307 hapd = iface->bss[j];
1309 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1310 if (hostapd_setup_bss(hapd, j == 0))
1312 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1313 prev_addr = hapd->own_addr;
1316 hostapd_tx_queue_params(iface);
1318 ap_list_init(iface);
1320 if (hostapd_driver_commit(hapd) < 0) {
1321 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1322 "configuration", __func__);
1326 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1327 iface->bss[0]->conf->iface);
1334 * hostapd_setup_interface - Setup of an interface
1335 * @iface: Pointer to interface data.
1336 * Returns: 0 on success, -1 on failure
1338 * Initializes the driver interface, validates the configuration,
1339 * and sets driver parameters based on the configuration.
1340 * Flushes old stations, sets the channel, encryption,
1341 * beacons, and WDS links based on the configuration.
1343 int hostapd_setup_interface(struct hostapd_iface *iface)
1347 ret = setup_interface(iface);
1349 wpa_printf(MSG_DEBUG, "%s: Unable to setup interface.",
1350 iface->bss[0]->conf->iface);
1360 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1361 * @hapd_iface: Pointer to interface data
1362 * @conf: Pointer to per-interface configuration
1363 * @bss: Pointer to per-BSS configuration for this BSS
1364 * Returns: Pointer to allocated BSS data
1366 * This function is used to allocate per-BSS data structure. This data will be
1367 * freed after hostapd_cleanup() is called for it during interface
1370 struct hostapd_data *
1371 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1372 struct hostapd_config *conf,
1373 struct hostapd_bss_config *bss)
1375 struct hostapd_data *hapd;
1377 hapd = os_zalloc(sizeof(*hapd));
1381 hostapd_set_driver_ops(&hapd->drv);
1384 hapd->iface = hapd_iface;
1386 #ifdef EAP_TLS_FUNCS
1387 if (hapd->conf->eap_server &&
1388 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1389 hapd->conf->dh_file)) {
1390 struct tls_connection_params params;
1392 hapd->ssl_ctx = tls_init(NULL);
1393 if (hapd->ssl_ctx == NULL) {
1394 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1398 os_memset(¶ms, 0, sizeof(params));
1399 params.ca_cert = hapd->conf->ca_cert;
1400 params.client_cert = hapd->conf->server_cert;
1401 params.private_key = hapd->conf->private_key;
1402 params.private_key_passwd = hapd->conf->private_key_passwd;
1403 params.dh_file = hapd->conf->dh_file;
1405 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1406 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1410 if (tls_global_set_verify(hapd->ssl_ctx,
1411 hapd->conf->check_crl)) {
1412 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1416 #endif /* EAP_TLS_FUNCS */
1419 if (hapd->conf->eap_sim_db) {
1420 hapd->eap_sim_db_priv =
1421 eap_sim_db_init(hapd->conf->eap_sim_db,
1422 hostapd_sim_db_cb, hapd);
1423 if (hapd->eap_sim_db_priv == NULL) {
1424 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1425 "database interface");
1429 #endif /* EAP_SIM_DB */
1431 hapd->driver = hapd->iconf->driver;
1435 #if defined(EAP_TLS_FUNCS) || defined(EAP_SIM_DB)
1438 /* TODO: cleanup allocated resources(?) */
1444 void hostapd_interface_deinit(struct hostapd_iface *iface)
1451 hostapd_cleanup_iface_pre(iface);
1452 for (j = 0; j < iface->num_bss; j++) {
1453 struct hostapd_data *hapd = iface->bss[j];
1454 hostapd_free_stas(hapd);
1455 hostapd_flush_old_stations(hapd);
1456 hostapd_cleanup(hapd);
1457 if (j == iface->num_bss - 1 && hapd->driver)
1458 hostapd_driver_deinit(hapd);
1460 for (j = 0; j < iface->num_bss; j++)
1461 os_free(iface->bss[j]);
1462 hostapd_cleanup_iface(iface);
1466 int hostapd_register_probereq_cb(struct hostapd_data *hapd,
1467 void (*cb)(void *ctx, const u8 *sa,
1468 const u8 *ie, size_t ie_len),
1471 struct hostapd_probereq_cb *n;
1473 n = os_realloc(hapd->probereq_cb, (hapd->num_probereq_cb + 1) *
1474 sizeof(struct hostapd_probereq_cb));
1478 hapd->probereq_cb = n;
1479 n = &hapd->probereq_cb[hapd->num_probereq_cb];
1480 hapd->num_probereq_cb++;
1489 int hostapd_set_drv_ieee8021x(struct hostapd_data *hapd, const char *ifname,
1492 struct wpa_bss_params params;
1493 os_memset(¶ms, 0, sizeof(params));
1494 params.ifname = ifname;
1495 params.enabled = enabled;
1497 params.wpa = hapd->conf->wpa;
1498 params.ieee802_1x = hapd->conf->ieee802_1x;
1499 params.wpa_group = hapd->conf->wpa_group;
1500 params.wpa_pairwise = hapd->conf->wpa_pairwise;
1501 params.wpa_key_mgmt = hapd->conf->wpa_key_mgmt;
1502 params.rsn_preauth = hapd->conf->rsn_preauth;
1504 return hostapd_set_ieee8021x(hapd, ¶ms);
1508 int hostapd_sta_flags_to_drv(int flags)
1511 if (flags & WLAN_STA_AUTHORIZED)
1512 res |= WPA_STA_AUTHORIZED;
1513 if (flags & WLAN_STA_WMM)
1515 if (flags & WLAN_STA_SHORT_PREAMBLE)
1516 res |= WPA_STA_SHORT_PREAMBLE;
1517 if (flags & WLAN_STA_MFP)