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"
29 #include "ap/hostapd.h"
30 #include "ap/sta_info.h"
31 #include "ap/accounting.h"
32 #include "ap/ap_list.h"
33 #include "ap/beacon.h"
35 #include "ap/ieee802_1x.h"
36 #include "ap/ieee802_11_auth.h"
37 #include "ap/preauth.h"
38 #include "ap/tkip_countermeasures.h"
39 #include "ap/vlan_init.h"
41 #include "ap/wps_hostapd.h"
42 #include "hw_features.h"
44 #include "ctrl_iface.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 if (iface->config_read_cb == NULL)
132 newconf = iface->config_read_cb(iface->config_fname);
137 * Deauthenticate all stations since the new configuration may not
138 * allow them to use the BSS anymore.
140 for (j = 0; j < iface->num_bss; j++)
141 hostapd_flush_old_stations(iface->bss[j]);
143 #ifndef CONFIG_NO_RADIUS
144 /* TODO: update dynamic data based on changed configuration
145 * items (e.g., open/close sockets, etc.) */
146 radius_client_flush(hapd->radius, 0);
147 #endif /* CONFIG_NO_RADIUS */
149 oldconf = hapd->iconf;
150 hapd->iconf = newconf;
151 hapd->conf = &newconf->bss[0];
152 iface->conf = newconf;
154 if (hostapd_setup_wpa_psk(hapd->conf)) {
155 wpa_printf(MSG_ERROR, "Failed to re-configure WPA PSK "
156 "after reloading configuration");
159 if (hapd->conf->wpa && hapd->wpa_auth == NULL)
160 hostapd_setup_wpa(hapd);
161 else if (hapd->conf->wpa) {
162 hostapd_wpa_auth_conf(&newconf->bss[0], &wpa_auth_conf);
163 wpa_reconfig(hapd->wpa_auth, &wpa_auth_conf);
164 } else if (hapd->wpa_auth) {
165 wpa_deinit(hapd->wpa_auth);
166 hapd->wpa_auth = NULL;
167 hostapd_set_privacy(hapd, 0);
168 hostapd_setup_encryption(hapd->conf->iface, hapd);
171 ieee802_11_set_beacon(hapd);
173 if (hapd->conf->ssid.ssid_set &&
174 hostapd_set_ssid(hapd, (u8 *) hapd->conf->ssid.ssid,
175 hapd->conf->ssid.ssid_len)) {
176 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
177 /* try to continue */
180 if (hapd->conf->ieee802_1x || hapd->conf->wpa)
181 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 1);
183 hapd->drv.set_drv_ieee8021x(hapd, hapd->conf->iface, 0);
185 hostapd_config_free(oldconf);
187 wpa_printf(MSG_DEBUG, "Reconfigured interface %s", hapd->conf->iface);
193 int handle_reload_iface(struct hostapd_iface *iface, void *ctx)
195 if (hostapd_reload_config(iface) < 0) {
196 wpa_printf(MSG_WARNING, "Failed to read new configuration "
197 "file - continuing with old.");
203 static void hostapd_broadcast_key_clear_iface(struct hostapd_data *hapd,
208 for (i = 0; i < NUM_WEP_KEYS; i++) {
209 if (hapd->drv.set_key(ifname, hapd, WPA_ALG_NONE, NULL, i,
210 i == 0 ? 1 : 0, NULL, 0, NULL, 0)) {
211 wpa_printf(MSG_DEBUG, "Failed to clear default "
212 "encryption keys (ifname=%s keyidx=%d)",
216 #ifdef CONFIG_IEEE80211W
217 if (hapd->conf->ieee80211w) {
218 for (i = NUM_WEP_KEYS; i < NUM_WEP_KEYS + 2; i++) {
219 if (hapd->drv.set_key(ifname, hapd, WPA_ALG_NONE, NULL,
220 i, i == 0 ? 1 : 0, NULL, 0,
222 wpa_printf(MSG_DEBUG, "Failed to clear "
223 "default mgmt encryption keys "
224 "(ifname=%s keyidx=%d)", ifname, i);
228 #endif /* CONFIG_IEEE80211W */
232 static int hostapd_broadcast_wep_clear(struct hostapd_data *hapd)
234 hostapd_broadcast_key_clear_iface(hapd, hapd->conf->iface);
239 static int hostapd_broadcast_wep_set(struct hostapd_data *hapd)
242 struct hostapd_ssid *ssid = &hapd->conf->ssid;
245 if (ssid->wep.default_len &&
246 hapd->drv.set_key(hapd->conf->iface,
247 hapd, WPA_ALG_WEP, NULL, idx,
248 idx == ssid->wep.idx,
249 NULL, 0, ssid->wep.key[idx],
250 ssid->wep.len[idx])) {
251 wpa_printf(MSG_WARNING, "Could not set WEP encryption.");
255 if (ssid->dyn_vlan_keys) {
257 for (i = 0; i <= ssid->max_dyn_vlan_keys; i++) {
259 struct hostapd_wep_keys *key = ssid->dyn_vlan_keys[i];
262 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan,
268 if (hapd->drv.set_key(ifname, hapd, WPA_ALG_WEP, NULL,
269 idx, idx == key->idx, NULL, 0,
270 key->key[idx], key->len[idx])) {
271 wpa_printf(MSG_WARNING, "Could not set "
272 "dynamic VLAN WEP encryption.");
282 * hostapd_cleanup - Per-BSS cleanup (deinitialization)
283 * @hapd: Pointer to BSS data
285 * This function is used to free all per-BSS data structures and resources.
286 * This gets called in a loop for each BSS between calls to
287 * hostapd_cleanup_iface_pre() and hostapd_cleanup_iface() when an interface
288 * is deinitialized. Most of the modules that are initialized in
289 * hostapd_setup_bss() are deinitialized here.
291 static void hostapd_cleanup(struct hostapd_data *hapd)
293 hostapd_ctrl_iface_deinit(hapd);
295 iapp_deinit(hapd->iapp);
297 accounting_deinit(hapd);
298 rsn_preauth_iface_deinit(hapd);
299 if (hapd->wpa_auth) {
300 wpa_deinit(hapd->wpa_auth);
301 hapd->wpa_auth = NULL;
303 if (hostapd_set_privacy(hapd, 0)) {
304 wpa_printf(MSG_DEBUG, "Could not disable "
305 "PrivacyInvoked for interface %s",
309 if (hostapd_set_generic_elem(hapd, (u8 *) "", 0)) {
310 wpa_printf(MSG_DEBUG, "Could not remove generic "
311 "information element from interface %s",
315 ieee802_1x_deinit(hapd);
317 hostapd_acl_deinit(hapd);
318 #ifndef CONFIG_NO_RADIUS
319 radius_client_deinit(hapd->radius);
321 #endif /* CONFIG_NO_RADIUS */
323 radius_server_deinit(hapd->radius_srv);
324 hapd->radius_srv = NULL;
325 #endif /* RADIUS_SERVER */
327 #ifdef CONFIG_IEEE80211R
328 l2_packet_deinit(hapd->l2);
329 #endif /* CONFIG_IEEE80211R */
331 hostapd_deinit_wps(hapd);
335 tls_deinit(hapd->ssl_ctx);
336 hapd->ssl_ctx = NULL;
338 #endif /* EAP_TLS_FUNCS */
340 #if defined(EAP_SERVER_SIM) || defined(EAP_SERVER_AKA)
341 if (hapd->eap_sim_db_priv) {
342 eap_sim_db_deinit(hapd->eap_sim_db_priv);
343 hapd->eap_sim_db_priv = NULL;
345 #endif /* EAP_SERVER_SIM || EAP_SERVER_AKA */
347 if (hapd->interface_added &&
348 hostapd_if_remove(hapd, WPA_IF_AP_BSS, hapd->conf->iface)) {
349 wpa_printf(MSG_WARNING, "Failed to remove BSS interface %s",
353 os_free(hapd->probereq_cb);
354 hapd->probereq_cb = NULL;
359 * hostapd_cleanup_iface_pre - Preliminary per-interface cleanup
360 * @iface: Pointer to interface data
362 * This function is called before per-BSS data structures are deinitialized
363 * with hostapd_cleanup().
365 static void hostapd_cleanup_iface_pre(struct hostapd_iface *iface)
371 * hostapd_cleanup_iface - Complete per-interface cleanup
372 * @iface: Pointer to interface data
374 * This function is called after per-BSS data structures are deinitialized
375 * with hostapd_cleanup().
377 static void hostapd_cleanup_iface(struct hostapd_iface *iface)
379 hostapd_free_hw_features(iface->hw_features, iface->num_hw_features);
380 iface->hw_features = NULL;
381 os_free(iface->current_rates);
382 iface->current_rates = NULL;
383 ap_list_deinit(iface);
384 hostapd_config_free(iface->conf);
387 os_free(iface->config_fname);
393 static int hostapd_setup_encryption(char *iface, struct hostapd_data *hapd)
397 hostapd_broadcast_wep_set(hapd);
399 if (hapd->conf->ssid.wep.default_len) {
400 hostapd_set_privacy(hapd, 1);
404 for (i = 0; i < 4; i++) {
405 if (hapd->conf->ssid.wep.key[i] &&
406 hapd->drv.set_key(iface, hapd, WPA_ALG_WEP, NULL, i,
407 i == hapd->conf->ssid.wep.idx, NULL, 0,
408 hapd->conf->ssid.wep.key[i],
409 hapd->conf->ssid.wep.len[i])) {
410 wpa_printf(MSG_WARNING, "Could not set WEP "
414 if (hapd->conf->ssid.wep.key[i] &&
415 i == hapd->conf->ssid.wep.idx)
416 hostapd_set_privacy(hapd, 1);
423 static int hostapd_flush_old_stations(struct hostapd_data *hapd)
427 if (hostapd_drv_none(hapd))
430 wpa_printf(MSG_DEBUG, "Flushing old station entries");
431 if (hostapd_flush(hapd)) {
432 wpa_printf(MSG_WARNING, "Could not connect to kernel driver.");
435 wpa_printf(MSG_DEBUG, "Deauthenticate all stations");
437 /* New Prism2.5/3 STA firmware versions seem to have issues with this
438 * broadcast deauth frame. This gets the firmware in odd state where
439 * nothing works correctly, so let's skip sending this for the hostap
441 if (hapd->driver && os_strcmp(hapd->driver->name, "hostap") != 0) {
443 os_memset(addr, 0xff, ETH_ALEN);
444 hapd->drv.sta_deauth(hapd, addr,
445 WLAN_REASON_PREV_AUTH_NOT_VALID);
452 static void hostapd_wpa_auth_logger(void *ctx, const u8 *addr,
453 logger_level level, const char *txt)
455 #ifndef CONFIG_NO_HOSTAPD_LOGGER
456 struct hostapd_data *hapd = ctx;
461 hlevel = HOSTAPD_LEVEL_WARNING;
464 hlevel = HOSTAPD_LEVEL_INFO;
468 hlevel = HOSTAPD_LEVEL_DEBUG;
472 hostapd_logger(hapd, addr, HOSTAPD_MODULE_WPA, hlevel, "%s", txt);
473 #endif /* CONFIG_NO_HOSTAPD_LOGGER */
477 static void hostapd_wpa_auth_disconnect(void *ctx, const u8 *addr,
480 struct hostapd_data *hapd = ctx;
481 wpa_printf(MSG_DEBUG, "%s: WPA authenticator requests disconnect: "
482 "STA " MACSTR " reason %d",
483 __func__, MAC2STR(addr), reason);
484 ap_sta_disconnect(hapd, NULL, addr, reason);
488 static void hostapd_wpa_auth_mic_failure_report(void *ctx, const u8 *addr)
490 struct hostapd_data *hapd = ctx;
491 michael_mic_failure(hapd, addr, 0);
495 static void hostapd_wpa_auth_set_eapol(void *ctx, const u8 *addr,
496 wpa_eapol_variable var, int value)
498 struct hostapd_data *hapd = ctx;
499 struct sta_info *sta = ap_get_sta(hapd, addr);
503 case WPA_EAPOL_portEnabled:
504 ieee802_1x_notify_port_enabled(sta->eapol_sm, value);
506 case WPA_EAPOL_portValid:
507 ieee802_1x_notify_port_valid(sta->eapol_sm, value);
509 case WPA_EAPOL_authorized:
510 ieee802_1x_set_sta_authorized(hapd, sta, value);
512 case WPA_EAPOL_portControl_Auto:
514 sta->eapol_sm->portControl = Auto;
516 case WPA_EAPOL_keyRun:
518 sta->eapol_sm->keyRun = value ? TRUE : FALSE;
520 case WPA_EAPOL_keyAvailable:
522 sta->eapol_sm->eap_if->eapKeyAvailable =
523 value ? TRUE : FALSE;
525 case WPA_EAPOL_keyDone:
527 sta->eapol_sm->keyDone = value ? TRUE : FALSE;
529 case WPA_EAPOL_inc_EapolFramesTx:
531 sta->eapol_sm->dot1xAuthEapolFramesTx++;
537 static int hostapd_wpa_auth_get_eapol(void *ctx, const u8 *addr,
538 wpa_eapol_variable var)
540 struct hostapd_data *hapd = ctx;
541 struct sta_info *sta = ap_get_sta(hapd, addr);
542 if (sta == NULL || sta->eapol_sm == NULL)
545 case WPA_EAPOL_keyRun:
546 return sta->eapol_sm->keyRun;
547 case WPA_EAPOL_keyAvailable:
548 return sta->eapol_sm->eap_if->eapKeyAvailable;
555 static const u8 * hostapd_wpa_auth_get_psk(void *ctx, const u8 *addr,
558 struct hostapd_data *hapd = ctx;
559 return hostapd_get_psk(hapd->conf, addr, prev_psk);
563 static int hostapd_wpa_auth_get_msk(void *ctx, const u8 *addr, u8 *msk,
566 struct hostapd_data *hapd = ctx;
569 struct sta_info *sta;
571 sta = ap_get_sta(hapd, addr);
575 key = ieee802_1x_get_key(sta->eapol_sm, &keylen);
581 os_memcpy(msk, key, keylen);
588 static int hostapd_wpa_auth_set_key(void *ctx, int vlan_id, wpa_alg alg,
589 const u8 *addr, int idx, u8 *key,
592 struct hostapd_data *hapd = ctx;
593 const char *ifname = hapd->conf->iface;
596 ifname = hostapd_get_vlan_id_ifname(hapd->conf->vlan, vlan_id);
601 return hapd->drv.set_key(ifname, hapd, alg, addr, idx, 1, NULL, 0,
606 static int hostapd_wpa_auth_get_seqnum(void *ctx, const u8 *addr, int idx,
609 struct hostapd_data *hapd = ctx;
610 return hostapd_get_seqnum(hapd->conf->iface, hapd, addr, idx, seq);
614 static int hostapd_wpa_auth_send_eapol(void *ctx, const u8 *addr,
615 const u8 *data, size_t data_len,
618 struct hostapd_data *hapd = ctx;
619 return hapd->drv.send_eapol(hapd, addr, data, data_len, encrypt);
623 static int hostapd_wpa_auth_for_each_sta(
624 void *ctx, int (*cb)(struct wpa_state_machine *sm, void *ctx),
627 struct hostapd_data *hapd = ctx;
628 struct sta_info *sta;
630 for (sta = hapd->sta_list; sta; sta = sta->next) {
631 if (sta->wpa_sm && cb(sta->wpa_sm, cb_ctx))
638 struct wpa_auth_iface_iter_data {
639 int (*cb)(struct wpa_authenticator *sm, void *ctx);
643 static int wpa_auth_iface_iter(struct hostapd_iface *iface, void *ctx)
645 struct wpa_auth_iface_iter_data *data = ctx;
647 for (i = 0; i < iface->num_bss; i++) {
648 if (data->cb(iface->bss[i]->wpa_auth, data->cb_ctx))
655 static int hostapd_wpa_auth_for_each_auth(
656 void *ctx, int (*cb)(struct wpa_authenticator *sm, void *ctx),
659 struct hostapd_data *hapd = ctx;
660 struct wpa_auth_iface_iter_data data;
662 data.cb_ctx = cb_ctx;
663 return hostapd_for_each_interface(hapd->iface->interfaces,
664 wpa_auth_iface_iter, &data);
668 static int hostapd_wpa_auth_send_ether(void *ctx, const u8 *dst, u16 proto,
669 const u8 *data, size_t data_len)
671 struct hostapd_data *hapd = ctx;
673 if (hapd->driver && hapd->driver->send_ether)
674 return hapd->driver->send_ether(hapd->drv_priv, dst,
675 hapd->own_addr, proto,
677 if (hapd->l2 == NULL)
679 return l2_packet_send(hapd->l2, dst, proto, data, data_len);
683 #ifdef CONFIG_IEEE80211R
685 static int hostapd_wpa_auth_send_ft_action(void *ctx, const u8 *dst,
686 const u8 *data, size_t data_len)
688 struct hostapd_data *hapd = ctx;
690 struct ieee80211_mgmt *m;
692 struct sta_info *sta;
694 sta = ap_get_sta(hapd, dst);
695 if (sta == NULL || sta->wpa_sm == NULL)
698 m = os_zalloc(sizeof(*m) + data_len);
701 mlen = ((u8 *) &m->u - (u8 *) m) + data_len;
702 m->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
703 WLAN_FC_STYPE_ACTION);
704 os_memcpy(m->da, dst, ETH_ALEN);
705 os_memcpy(m->sa, hapd->own_addr, ETH_ALEN);
706 os_memcpy(m->bssid, hapd->own_addr, ETH_ALEN);
707 os_memcpy(&m->u, data, data_len);
709 res = hapd->drv.send_mgmt_frame(hapd, (u8 *) m, mlen);
715 static struct wpa_state_machine *
716 hostapd_wpa_auth_add_sta(void *ctx, const u8 *sta_addr)
718 struct hostapd_data *hapd = ctx;
719 struct sta_info *sta;
721 sta = ap_sta_add(hapd, sta_addr);
727 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth, sta->addr);
728 if (sta->wpa_sm == NULL) {
729 ap_free_sta(hapd, sta);
732 sta->auth_alg = WLAN_AUTH_FT;
738 static void hostapd_rrb_receive(void *ctx, const u8 *src_addr, const u8 *buf,
741 struct hostapd_data *hapd = ctx;
742 wpa_ft_rrb_rx(hapd->wpa_auth, src_addr, buf, len);
745 #endif /* CONFIG_IEEE80211R */
749 * hostapd_validate_bssid_configuration - Validate BSSID configuration
750 * @iface: Pointer to interface data
751 * Returns: 0 on success, -1 on failure
753 * This function is used to validate that the configured BSSIDs are valid.
755 static int hostapd_validate_bssid_configuration(struct hostapd_iface *iface)
757 u8 mask[ETH_ALEN] = { 0 };
758 struct hostapd_data *hapd = iface->bss[0];
759 unsigned int i = iface->conf->num_bss, bits = 0, j;
763 if (hostapd_drv_none(hapd))
766 /* Generate BSSID mask that is large enough to cover the BSSIDs. */
768 /* Determine the bits necessary to cover the number of BSSIDs. */
769 for (i--; i; i >>= 1)
772 /* Determine the bits necessary to any configured BSSIDs,
773 if they are higher than the number of BSSIDs. */
774 for (j = 0; j < iface->conf->num_bss; j++) {
775 if (hostapd_mac_comp_empty(iface->conf->bss[j].bssid) == 0) {
781 for (i = 0; i < ETH_ALEN; i++) {
783 iface->conf->bss[j].bssid[i] ^
791 for (i = 0; i < ETH_ALEN && mask[i] == 0; i++)
797 while (mask[i] != 0) {
807 wpa_printf(MSG_ERROR, "Too many bits in the BSSID mask (%u)",
812 os_memset(mask, 0xff, ETH_ALEN);
814 for (i = 5; i > 5 - j; i--)
821 wpa_printf(MSG_DEBUG, "BSS count %lu, BSSID mask " MACSTR " (%d bits)",
822 (unsigned long) iface->conf->num_bss, MAC2STR(mask), bits);
824 res = hostapd_valid_bss_mask(hapd, hapd->own_addr, mask);
829 wpa_printf(MSG_ERROR, "Driver did not accept BSSID mask "
830 MACSTR " for start address " MACSTR ".",
831 MAC2STR(mask), MAC2STR(hapd->own_addr));
838 for (i = 0; i < ETH_ALEN; i++) {
839 if ((hapd->own_addr[i] & mask[i]) != hapd->own_addr[i]) {
840 wpa_printf(MSG_ERROR, "Invalid BSSID mask " MACSTR
841 " for start address " MACSTR ".",
842 MAC2STR(mask), MAC2STR(hapd->own_addr));
843 wpa_printf(MSG_ERROR, "Start address must be the "
844 "first address in the block (i.e., addr "
845 "AND mask == addr).");
854 static int mac_in_conf(struct hostapd_config *conf, const void *a)
858 for (i = 0; i < conf->num_bss; i++) {
859 if (hostapd_mac_comp(conf->bss[i].bssid, a) == 0) {
868 static int hostapd_setup_wpa(struct hostapd_data *hapd)
870 struct wpa_auth_config _conf;
871 struct wpa_auth_callbacks cb;
875 hostapd_wpa_auth_conf(hapd->conf, &_conf);
876 os_memset(&cb, 0, sizeof(cb));
878 cb.logger = hostapd_wpa_auth_logger;
879 cb.disconnect = hostapd_wpa_auth_disconnect;
880 cb.mic_failure_report = hostapd_wpa_auth_mic_failure_report;
881 cb.set_eapol = hostapd_wpa_auth_set_eapol;
882 cb.get_eapol = hostapd_wpa_auth_get_eapol;
883 cb.get_psk = hostapd_wpa_auth_get_psk;
884 cb.get_msk = hostapd_wpa_auth_get_msk;
885 cb.set_key = hostapd_wpa_auth_set_key;
886 cb.get_seqnum = hostapd_wpa_auth_get_seqnum;
887 cb.send_eapol = hostapd_wpa_auth_send_eapol;
888 cb.for_each_sta = hostapd_wpa_auth_for_each_sta;
889 cb.for_each_auth = hostapd_wpa_auth_for_each_auth;
890 cb.send_ether = hostapd_wpa_auth_send_ether;
891 #ifdef CONFIG_IEEE80211R
892 cb.send_ft_action = hostapd_wpa_auth_send_ft_action;
893 cb.add_sta = hostapd_wpa_auth_add_sta;
894 #endif /* CONFIG_IEEE80211R */
895 hapd->wpa_auth = wpa_init(hapd->own_addr, &_conf, &cb);
896 if (hapd->wpa_auth == NULL) {
897 wpa_printf(MSG_ERROR, "WPA initialization failed.");
901 if (hostapd_set_privacy(hapd, 1)) {
902 wpa_printf(MSG_ERROR, "Could not set PrivacyInvoked "
903 "for interface %s", hapd->conf->iface);
907 wpa_ie = wpa_auth_get_wpa_ie(hapd->wpa_auth, &wpa_ie_len);
908 if (hostapd_set_generic_elem(hapd, wpa_ie, wpa_ie_len)) {
909 wpa_printf(MSG_ERROR, "Failed to configure WPA IE for "
910 "the kernel driver.");
914 if (rsn_preauth_iface_init(hapd)) {
915 wpa_printf(MSG_ERROR, "Initialization of RSN "
916 "pre-authentication failed.");
927 static int hostapd_radius_get_eap_user(void *ctx, const u8 *identity,
928 size_t identity_len, int phase2,
929 struct eap_user *user)
931 const struct hostapd_eap_user *eap_user;
934 eap_user = hostapd_get_eap_user(ctx, identity, identity_len, phase2);
935 if (eap_user == NULL)
941 os_memset(user, 0, sizeof(*user));
942 count = EAP_USER_MAX_METHODS;
943 if (count > EAP_MAX_METHODS)
944 count = EAP_MAX_METHODS;
945 for (i = 0; i < count; i++) {
946 user->methods[i].vendor = eap_user->methods[i].vendor;
947 user->methods[i].method = eap_user->methods[i].method;
950 if (eap_user->password) {
951 user->password = os_malloc(eap_user->password_len);
952 if (user->password == NULL)
954 os_memcpy(user->password, eap_user->password,
955 eap_user->password_len);
956 user->password_len = eap_user->password_len;
957 user->password_hash = eap_user->password_hash;
959 user->force_version = eap_user->force_version;
960 user->ttls_auth = eap_user->ttls_auth;
966 static int hostapd_setup_radius_srv(struct hostapd_data *hapd,
967 struct hostapd_bss_config *conf)
969 struct radius_server_conf srv;
970 os_memset(&srv, 0, sizeof(srv));
971 srv.client_file = conf->radius_server_clients;
972 srv.auth_port = conf->radius_server_auth_port;
974 srv.eap_sim_db_priv = hapd->eap_sim_db_priv;
975 srv.ssl_ctx = hapd->ssl_ctx;
976 srv.pac_opaque_encr_key = conf->pac_opaque_encr_key;
977 srv.eap_fast_a_id = conf->eap_fast_a_id;
978 srv.eap_fast_a_id_len = conf->eap_fast_a_id_len;
979 srv.eap_fast_a_id_info = conf->eap_fast_a_id_info;
980 srv.eap_fast_prov = conf->eap_fast_prov;
981 srv.pac_key_lifetime = conf->pac_key_lifetime;
982 srv.pac_key_refresh_time = conf->pac_key_refresh_time;
983 srv.eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind;
986 srv.ipv6 = conf->radius_server_ipv6;
987 srv.get_eap_user = hostapd_radius_get_eap_user;
988 srv.eap_req_id_text = conf->eap_req_id_text;
989 srv.eap_req_id_text_len = conf->eap_req_id_text_len;
991 hapd->radius_srv = radius_server_init(&srv);
992 if (hapd->radius_srv == NULL) {
993 wpa_printf(MSG_ERROR, "RADIUS server initialization failed.");
1000 #endif /* RADIUS_SERVER */
1004 * hostapd_setup_bss - Per-BSS setup (initialization)
1005 * @hapd: Pointer to BSS data
1006 * @first: Whether this BSS is the first BSS of an interface
1008 * This function is used to initialize all per-BSS data structures and
1009 * resources. This gets called in a loop for each BSS when an interface is
1010 * initialized. Most of the modules that are initialized here will be
1011 * deinitialized in hostapd_cleanup().
1013 static int hostapd_setup_bss(struct hostapd_data *hapd, int first)
1015 struct hostapd_bss_config *conf = hapd->conf;
1016 u8 ssid[HOSTAPD_MAX_SSID_LEN + 1];
1017 int ssid_len, set_ssid;
1020 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0) {
1021 /* Allocate the next available BSSID. */
1023 inc_byte_array(hapd->own_addr, ETH_ALEN);
1024 } while (mac_in_conf(hapd->iconf, hapd->own_addr));
1026 /* Allocate the configured BSSID. */
1027 os_memcpy(hapd->own_addr, hapd->conf->bssid, ETH_ALEN);
1029 if (hostapd_mac_comp(hapd->own_addr,
1030 hapd->iface->bss[0]->own_addr) ==
1032 wpa_printf(MSG_ERROR, "BSS '%s' may not have "
1033 "BSSID set to the MAC address of "
1034 "the radio", hapd->conf->iface);
1039 hapd->interface_added = 1;
1040 if (hostapd_if_add(hapd->iface->bss[0], WPA_IF_AP_BSS,
1041 hapd->conf->iface, hapd->own_addr, hapd)) {
1042 wpa_printf(MSG_ERROR, "Failed to add BSS (BSSID="
1043 MACSTR ")", MAC2STR(hapd->own_addr));
1048 hostapd_flush_old_stations(hapd);
1049 hostapd_set_privacy(hapd, 0);
1051 hostapd_broadcast_wep_clear(hapd);
1052 if (hostapd_setup_encryption(hapd->conf->iface, hapd))
1056 * Fetch the SSID from the system and use it or,
1057 * if one was specified in the config file, verify they
1060 ssid_len = hostapd_get_ssid(hapd, ssid, sizeof(ssid));
1062 wpa_printf(MSG_ERROR, "Could not read SSID from system");
1065 if (conf->ssid.ssid_set) {
1067 * If SSID is specified in the config file and it differs
1068 * from what is being used then force installation of the
1071 set_ssid = (conf->ssid.ssid_len != (size_t) ssid_len ||
1072 os_memcmp(conf->ssid.ssid, ssid, ssid_len) != 0);
1075 * No SSID in the config file; just use the one we got
1079 conf->ssid.ssid_len = ssid_len;
1080 os_memcpy(conf->ssid.ssid, ssid, conf->ssid.ssid_len);
1081 conf->ssid.ssid[conf->ssid.ssid_len] = '\0';
1084 if (!hostapd_drv_none(hapd)) {
1085 wpa_printf(MSG_ERROR, "Using interface %s with hwaddr " MACSTR
1087 hapd->conf->iface, MAC2STR(hapd->own_addr),
1088 hapd->conf->ssid.ssid);
1091 if (hostapd_setup_wpa_psk(conf)) {
1092 wpa_printf(MSG_ERROR, "WPA-PSK setup failed.");
1096 /* Set SSID for the kernel driver (to be used in beacon and probe
1097 * response frames) */
1098 if (set_ssid && hostapd_set_ssid(hapd, (u8 *) conf->ssid.ssid,
1099 conf->ssid.ssid_len)) {
1100 wpa_printf(MSG_ERROR, "Could not set SSID for kernel driver");
1104 if (wpa_debug_level == MSG_MSGDUMP)
1105 conf->radius->msg_dumps = 1;
1106 #ifndef CONFIG_NO_RADIUS
1107 hapd->radius = radius_client_init(hapd, conf->radius);
1108 if (hapd->radius == NULL) {
1109 wpa_printf(MSG_ERROR, "RADIUS client initialization failed.");
1112 #endif /* CONFIG_NO_RADIUS */
1114 if (hostapd_acl_init(hapd)) {
1115 wpa_printf(MSG_ERROR, "ACL initialization failed.");
1118 if (hostapd_init_wps(hapd, conf))
1121 if (ieee802_1x_init(hapd)) {
1122 wpa_printf(MSG_ERROR, "IEEE 802.1X initialization failed.");
1126 if (hapd->conf->wpa && hostapd_setup_wpa(hapd))
1129 if (accounting_init(hapd)) {
1130 wpa_printf(MSG_ERROR, "Accounting initialization failed.");
1134 if (hapd->conf->ieee802_11f &&
1135 (hapd->iapp = iapp_init(hapd, hapd->conf->iapp_iface)) == NULL) {
1136 wpa_printf(MSG_ERROR, "IEEE 802.11F (IAPP) initialization "
1141 if (hostapd_ctrl_iface_init(hapd)) {
1142 wpa_printf(MSG_ERROR, "Failed to setup control interface");
1146 if (!hostapd_drv_none(hapd) && vlan_init(hapd)) {
1147 wpa_printf(MSG_ERROR, "VLAN initialization failed.");
1151 #ifdef CONFIG_IEEE80211R
1152 if (!hostapd_drv_none(hapd)) {
1153 hapd->l2 = l2_packet_init(hapd->conf->iface, NULL, ETH_P_RRB,
1154 hostapd_rrb_receive, hapd, 0);
1155 if (hapd->l2 == NULL &&
1156 (hapd->driver == NULL ||
1157 hapd->driver->send_ether == NULL)) {
1158 wpa_printf(MSG_ERROR, "Failed to open l2_packet "
1163 #endif /* CONFIG_IEEE80211R */
1165 ieee802_11_set_beacon(hapd);
1167 #ifdef RADIUS_SERVER
1168 if (conf->radius_server_clients &&
1169 hostapd_setup_radius_srv(hapd, conf))
1171 #endif /* RADIUS_SERVER */
1177 static void hostapd_tx_queue_params(struct hostapd_iface *iface)
1179 struct hostapd_data *hapd = iface->bss[0];
1181 struct hostapd_tx_queue_params *p;
1183 for (i = 0; i < NUM_TX_QUEUES; i++) {
1184 p = &iface->conf->tx_queue[i];
1189 if (hostapd_set_tx_queue_params(hapd, i, p->aifs, p->cwmin,
1190 p->cwmax, p->burst)) {
1191 wpa_printf(MSG_DEBUG, "Failed to set TX queue "
1192 "parameters for queue %d.", i);
1193 /* Continue anyway */
1199 static int setup_interface(struct hostapd_iface *iface)
1201 struct hostapd_data *hapd = iface->bss[0];
1206 * Make sure that all BSSes get configured with a pointer to the same
1209 for (i = 1; i < iface->num_bss; i++) {
1210 iface->bss[i]->driver = hapd->driver;
1211 iface->bss[i]->drv_priv = hapd->drv_priv;
1214 if (hostapd_validate_bssid_configuration(iface))
1217 if (hapd->iconf->country[0] && hapd->iconf->country[1]) {
1218 os_memcpy(country, hapd->iconf->country, 3);
1220 if (hostapd_set_country(hapd, country) < 0) {
1221 wpa_printf(MSG_ERROR, "Failed to set country code");
1226 if (hostapd_get_hw_features(iface)) {
1227 /* Not all drivers support this yet, so continue without hw
1230 int ret = hostapd_select_hw_mode(iface);
1232 wpa_printf(MSG_ERROR, "Could not select hw_mode and "
1233 "channel. (%d)", ret);
1236 ret = hostapd_check_ht_capab(iface);
1240 wpa_printf(MSG_DEBUG, "Interface initialization will "
1241 "be completed in a callback");
1245 return hostapd_setup_interface_complete(iface, 0);
1249 int hostapd_setup_interface_complete(struct hostapd_iface *iface, int err)
1251 struct hostapd_data *hapd = iface->bss[0];
1257 wpa_printf(MSG_ERROR, "Interface initialization failed");
1262 wpa_printf(MSG_DEBUG, "Completing interface initialization");
1263 if (hapd->iconf->channel) {
1264 freq = hostapd_hw_get_freq(hapd, hapd->iconf->channel);
1265 wpa_printf(MSG_DEBUG, "Mode: %s Channel: %d "
1266 "Frequency: %d MHz",
1267 hostapd_hw_mode_txt(hapd->iconf->hw_mode),
1268 hapd->iconf->channel, freq);
1270 if (hostapd_set_freq(hapd, hapd->iconf->hw_mode, freq,
1271 hapd->iconf->channel,
1272 hapd->iconf->ieee80211n,
1273 hapd->iconf->secondary_channel)) {
1274 wpa_printf(MSG_ERROR, "Could not set channel for "
1280 if (hapd->iconf->rts_threshold > -1 &&
1281 hostapd_set_rts(hapd, hapd->iconf->rts_threshold)) {
1282 wpa_printf(MSG_ERROR, "Could not set RTS threshold for "
1287 if (hapd->iconf->fragm_threshold > -1 &&
1288 hostapd_set_frag(hapd, hapd->iconf->fragm_threshold)) {
1289 wpa_printf(MSG_ERROR, "Could not set fragmentation threshold "
1290 "for kernel driver");
1294 prev_addr = hapd->own_addr;
1296 for (j = 0; j < iface->num_bss; j++) {
1297 hapd = iface->bss[j];
1299 os_memcpy(hapd->own_addr, prev_addr, ETH_ALEN);
1300 if (hostapd_setup_bss(hapd, j == 0))
1302 if (hostapd_mac_comp_empty(hapd->conf->bssid) == 0)
1303 prev_addr = hapd->own_addr;
1306 hostapd_tx_queue_params(iface);
1308 ap_list_init(iface);
1310 if (hostapd_driver_commit(hapd) < 0) {
1311 wpa_printf(MSG_ERROR, "%s: Failed to commit driver "
1312 "configuration", __func__);
1316 wpa_printf(MSG_DEBUG, "%s: Setup of interface done.",
1317 iface->bss[0]->conf->iface);
1324 * hostapd_setup_interface - Setup of an interface
1325 * @iface: Pointer to interface data.
1326 * Returns: 0 on success, -1 on failure
1328 * Initializes the driver interface, validates the configuration,
1329 * and sets driver parameters based on the configuration.
1330 * Flushes old stations, sets the channel, encryption,
1331 * beacons, and WDS links based on the configuration.
1333 int hostapd_setup_interface(struct hostapd_iface *iface)
1337 ret = setup_interface(iface);
1339 wpa_printf(MSG_ERROR, "%s: Unable to setup interface.",
1340 iface->bss[0]->conf->iface);
1349 * hostapd_alloc_bss_data - Allocate and initialize per-BSS data
1350 * @hapd_iface: Pointer to interface data
1351 * @conf: Pointer to per-interface configuration
1352 * @bss: Pointer to per-BSS configuration for this BSS
1353 * Returns: Pointer to allocated BSS data
1355 * This function is used to allocate per-BSS data structure. This data will be
1356 * freed after hostapd_cleanup() is called for it during interface
1359 struct hostapd_data *
1360 hostapd_alloc_bss_data(struct hostapd_iface *hapd_iface,
1361 struct hostapd_config *conf,
1362 struct hostapd_bss_config *bss)
1364 struct hostapd_data *hapd;
1366 hapd = os_zalloc(sizeof(*hapd));
1370 hostapd_set_driver_ops(&hapd->drv);
1371 hapd->new_assoc_sta_cb = hostapd_new_assoc_sta;
1374 hapd->iface = hapd_iface;
1376 #ifdef EAP_TLS_FUNCS
1377 if (hapd->conf->eap_server &&
1378 (hapd->conf->ca_cert || hapd->conf->server_cert ||
1379 hapd->conf->dh_file)) {
1380 struct tls_connection_params params;
1382 hapd->ssl_ctx = tls_init(NULL);
1383 if (hapd->ssl_ctx == NULL) {
1384 wpa_printf(MSG_ERROR, "Failed to initialize TLS");
1388 os_memset(¶ms, 0, sizeof(params));
1389 params.ca_cert = hapd->conf->ca_cert;
1390 params.client_cert = hapd->conf->server_cert;
1391 params.private_key = hapd->conf->private_key;
1392 params.private_key_passwd = hapd->conf->private_key_passwd;
1393 params.dh_file = hapd->conf->dh_file;
1395 if (tls_global_set_params(hapd->ssl_ctx, ¶ms)) {
1396 wpa_printf(MSG_ERROR, "Failed to set TLS parameters");
1400 if (tls_global_set_verify(hapd->ssl_ctx,
1401 hapd->conf->check_crl)) {
1402 wpa_printf(MSG_ERROR, "Failed to enable check_crl");
1406 #endif /* EAP_TLS_FUNCS */
1409 if (hapd->conf->eap_sim_db) {
1410 hapd->eap_sim_db_priv =
1411 eap_sim_db_init(hapd->conf->eap_sim_db,
1412 hostapd_sim_db_cb, hapd);
1413 if (hapd->eap_sim_db_priv == NULL) {
1414 wpa_printf(MSG_ERROR, "Failed to initialize EAP-SIM "
1415 "database interface");
1419 #endif /* EAP_SIM_DB */
1421 hapd->driver = hapd->iconf->driver;
1425 #if defined(EAP_TLS_FUNCS) || defined(EAP_SIM_DB)
1428 /* TODO: cleanup allocated resources(?) */
1434 void hostapd_interface_deinit(struct hostapd_iface *iface)
1441 hostapd_cleanup_iface_pre(iface);
1442 for (j = 0; j < iface->num_bss; j++) {
1443 struct hostapd_data *hapd = iface->bss[j];
1444 hostapd_free_stas(hapd);
1445 hostapd_flush_old_stations(hapd);
1446 hostapd_cleanup(hapd);
1447 if (j == iface->num_bss - 1 && hapd->driver)
1448 hostapd_driver_deinit(hapd);
1450 for (j = 0; j < iface->num_bss; j++)
1451 os_free(iface->bss[j]);
1452 hostapd_cleanup_iface(iface);
1457 struct hostapd_data *hapd;
1461 static int prune_associations(struct hostapd_iface *iface, void *ctx)
1463 struct prune_data *data = ctx;
1464 struct sta_info *osta;
1465 struct hostapd_data *ohapd;
1468 for (j = 0; j < iface->num_bss; j++) {
1469 ohapd = iface->bss[j];
1470 if (ohapd == data->hapd)
1472 osta = ap_get_sta(ohapd, data->addr);
1476 ap_sta_disassociate(ohapd, osta, WLAN_REASON_UNSPECIFIED);
1483 * hostapd_prune_associations - Remove extraneous associations
1484 * @hapd: Pointer to BSS data for the most recent association
1485 * @sta: Pointer to the associated STA data
1487 * This function looks through all radios and BSS's for previous
1488 * (stale) associations of STA. If any are found they are removed.
1490 static void hostapd_prune_associations(struct hostapd_data *hapd,
1491 struct sta_info *sta)
1493 struct prune_data data;
1495 data.addr = sta->addr;
1496 hostapd_for_each_interface(hapd->iface->interfaces,
1497 prune_associations, &data);
1502 * hostapd_new_assoc_sta - Notify that a new station associated with the AP
1503 * @hapd: Pointer to BSS data
1504 * @sta: Pointer to the associated STA data
1505 * @reassoc: 1 to indicate this was a re-association; 0 = first association
1507 * This function will be called whenever a station associates with the AP. It
1508 * can be called from ieee802_11.c for drivers that export MLME to hostapd and
1509 * from drv_callbacks.c based on driver events for drivers that take care of
1510 * management frames (IEEE 802.11 authentication and association) internally.
1512 void hostapd_new_assoc_sta(struct hostapd_data *hapd, struct sta_info *sta,
1515 if (hapd->tkip_countermeasures) {
1516 hapd->drv.sta_deauth(hapd, sta->addr,
1517 WLAN_REASON_MICHAEL_MIC_FAILURE);
1521 hostapd_prune_associations(hapd, sta);
1523 /* IEEE 802.11F (IAPP) */
1524 if (hapd->conf->ieee802_11f)
1525 iapp_new_station(hapd->iapp, sta);
1527 /* Start accounting here, if IEEE 802.1X and WPA are not used.
1528 * IEEE 802.1X/WPA code will start accounting after the station has
1529 * been authorized. */
1530 if (!hapd->conf->ieee802_1x && !hapd->conf->wpa)
1531 accounting_sta_start(hapd, sta);
1533 /* Start IEEE 802.1X authentication process for new stations */
1534 ieee802_1x_new_station(hapd, sta);
1536 if (sta->auth_alg != WLAN_AUTH_FT &&
1537 !(sta->flags & (WLAN_STA_WPS | WLAN_STA_MAYBE_WPS)))
1538 wpa_auth_sm_event(sta->wpa_sm, WPA_REAUTH);
1540 wpa_auth_sta_associated(hapd->wpa_auth, sta->wpa_sm);