2 * hostapd / Callback functions for driver wrappers
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "radius/radius.h"
14 #include "drivers/driver.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/wpa_ctrl.h"
18 #include "crypto/random.h"
23 #include "ieee802_11.h"
25 #include "accounting.h"
26 #include "tkip_countermeasures.h"
27 #include "ieee802_1x.h"
29 #include "wps_hostapd.h"
30 #include "ap_drv_ops.h"
31 #include "ap_config.h"
32 #include "hw_features.h"
37 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
38 const u8 *req_ies, size_t req_ies_len, int reassoc)
42 struct ieee802_11_elems elems;
45 #ifdef CONFIG_IEEE80211R
46 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
48 #endif /* CONFIG_IEEE80211R */
49 u16 reason = WLAN_REASON_UNSPECIFIED;
50 u16 status = WLAN_STATUS_SUCCESS;
51 const u8 *p2p_dev_addr = NULL;
55 * This could potentially happen with unexpected event from the
56 * driver wrapper. This was seen at least in one case where the
57 * driver ended up being set to station mode while hostapd was
58 * running, so better make sure we stop processing such an
61 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
65 random_add_randomness(addr, ETH_ALEN);
67 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
68 HOSTAPD_LEVEL_INFO, "associated");
70 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
72 ie = elems.wps_ie - 2;
73 ielen = elems.wps_ie_len + 2;
74 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
75 } else if (elems.rsn_ie) {
76 ie = elems.rsn_ie - 2;
77 ielen = elems.rsn_ie_len + 2;
78 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
79 } else if (elems.wpa_ie) {
80 ie = elems.wpa_ie - 2;
81 ielen = elems.wpa_ie_len + 2;
82 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
84 } else if (elems.osen) {
86 ielen = elems.osen_len + 2;
87 wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
88 #endif /* CONFIG_HS20 */
92 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
96 sta = ap_get_sta(hapd, addr);
98 ap_sta_no_session_timeout(hapd, sta);
99 accounting_sta_stop(hapd, sta);
102 * Make sure that the previously registered inactivity timer
103 * will not remove the STA immediately.
105 sta->timeout_next = STA_NULLFUNC;
107 sta = ap_sta_add(hapd, addr);
109 hostapd_drv_sta_disassoc(hapd, addr,
110 WLAN_REASON_DISASSOC_AP_BUSY);
114 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
118 wpabuf_free(sta->p2p_ie);
119 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
122 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
124 #endif /* CONFIG_P2P */
126 #ifdef CONFIG_IEEE80211N
128 if (elems.ht_capabilities &&
129 elems.ht_capabilities_len >=
130 sizeof(struct ieee80211_ht_capabilities) &&
131 (hapd->iface->conf->ht_capab &
132 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
133 struct ieee80211_ht_capabilities *ht_cap =
134 (struct ieee80211_ht_capabilities *)
135 elems.ht_capabilities;
137 if (le_to_host16(ht_cap->ht_capabilities_info) &
138 HT_CAP_INFO_40MHZ_INTOLERANT)
139 ht40_intolerant_add(hapd->iface, sta);
141 #endif /* NEED_AP_MLME */
142 #endif /* CONFIG_IEEE80211N */
144 #ifdef CONFIG_INTERWORKING
145 if (elems.ext_capab && elems.ext_capab_len > 4) {
146 if (elems.ext_capab[4] & 0x01)
147 sta->qos_map_enabled = 1;
149 #endif /* CONFIG_INTERWORKING */
152 wpabuf_free(sta->hs20_ie);
153 if (elems.hs20 && elems.hs20_len > 4) {
154 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
158 #endif /* CONFIG_HS20 */
160 if (hapd->conf->wpa) {
161 if (ie == NULL || ielen == 0) {
163 if (hapd->conf->wps_state) {
164 wpa_printf(MSG_DEBUG, "STA did not include "
165 "WPA/RSN IE in (Re)Association "
166 "Request - possible WPS use");
167 sta->flags |= WLAN_STA_MAYBE_WPS;
170 #endif /* CONFIG_WPS */
172 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
176 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
177 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
179 sta->flags |= WLAN_STA_WPS;
180 wps = ieee802_11_vendor_ie_concat(ie, ielen,
183 if (wps_is_20(wps)) {
184 wpa_printf(MSG_DEBUG, "WPS: STA "
186 sta->flags |= WLAN_STA_WPS2;
192 #endif /* CONFIG_WPS */
194 if (sta->wpa_sm == NULL)
195 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
198 if (sta->wpa_sm == NULL) {
199 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
203 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
205 elems.mdie, elems.mdie_len);
206 if (res != WPA_IE_OK) {
207 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
208 "rejected? (res %u)", res);
209 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
210 if (res == WPA_INVALID_GROUP) {
211 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
212 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
213 } else if (res == WPA_INVALID_PAIRWISE) {
214 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
215 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
216 } else if (res == WPA_INVALID_AKMP) {
217 reason = WLAN_REASON_AKMP_NOT_VALID;
218 status = WLAN_STATUS_AKMP_NOT_VALID;
220 #ifdef CONFIG_IEEE80211W
221 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
222 reason = WLAN_REASON_INVALID_IE;
223 status = WLAN_STATUS_INVALID_IE;
224 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
225 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
226 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
228 #endif /* CONFIG_IEEE80211W */
230 reason = WLAN_REASON_INVALID_IE;
231 status = WLAN_STATUS_INVALID_IE;
235 #ifdef CONFIG_IEEE80211W
236 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
237 sta->sa_query_count > 0)
238 ap_check_sa_query_timeout(hapd, sta);
239 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
240 (sta->auth_alg != WLAN_AUTH_FT)) {
242 * STA has already been associated with MFP and SA
243 * Query timeout has not been reached. Reject the
244 * association attempt temporarily and start SA Query,
245 * if one is not pending.
248 if (sta->sa_query_count == 0)
249 ap_sta_start_sa_query(hapd, sta);
251 #ifdef CONFIG_IEEE80211R
252 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
254 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
256 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
258 #endif /* CONFIG_IEEE80211R */
262 if (wpa_auth_uses_mfp(sta->wpa_sm))
263 sta->flags |= WLAN_STA_MFP;
265 sta->flags &= ~WLAN_STA_MFP;
266 #endif /* CONFIG_IEEE80211W */
268 #ifdef CONFIG_IEEE80211R
269 if (sta->auth_alg == WLAN_AUTH_FT) {
270 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
272 if (status != WLAN_STATUS_SUCCESS) {
273 if (status == WLAN_STATUS_INVALID_PMKID)
274 reason = WLAN_REASON_INVALID_IE;
275 if (status == WLAN_STATUS_INVALID_MDIE)
276 reason = WLAN_REASON_INVALID_IE;
277 if (status == WLAN_STATUS_INVALID_FTIE)
278 reason = WLAN_REASON_INVALID_IE;
282 #endif /* CONFIG_IEEE80211R */
283 } else if (hapd->conf->wps_state) {
287 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
291 #ifdef CONFIG_WPS_STRICT
292 if (wps && wps_validate_assoc_req(wps) < 0) {
293 reason = WLAN_REASON_INVALID_IE;
294 status = WLAN_STATUS_INVALID_IE;
298 #endif /* CONFIG_WPS_STRICT */
300 sta->flags |= WLAN_STA_WPS;
301 if (wps_is_20(wps)) {
302 wpa_printf(MSG_DEBUG, "WPS: STA supports "
304 sta->flags |= WLAN_STA_WPS2;
307 sta->flags |= WLAN_STA_MAYBE_WPS;
309 #endif /* CONFIG_WPS */
311 } else if (hapd->conf->osen) {
312 if (elems.osen == NULL) {
314 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
316 "No HS 2.0 OSEN element in association request");
317 return WLAN_STATUS_INVALID_IE;
320 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
321 if (sta->wpa_sm == NULL)
322 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
324 if (sta->wpa_sm == NULL) {
325 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
327 return WLAN_STATUS_UNSPECIFIED_FAILURE;
329 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
330 elems.osen - 2, elems.osen_len + 2) < 0)
331 return WLAN_STATUS_INVALID_IE;
332 #endif /* CONFIG_HS20 */
336 #endif /* CONFIG_WPS */
338 #ifdef CONFIG_IEEE80211R
339 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
340 sta->auth_alg, req_ies, req_ies_len);
342 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
344 if (sta->auth_alg == WLAN_AUTH_FT)
345 ap_sta_set_authorized(hapd, sta, 1);
346 #else /* CONFIG_IEEE80211R */
347 /* Keep compiler silent about unused variables */
350 #endif /* CONFIG_IEEE80211R */
352 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
353 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
354 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
356 hostapd_set_sta_flags(hapd, sta);
358 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
359 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
361 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
363 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
365 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
369 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
370 req_ies, req_ies_len);
372 #endif /* CONFIG_P2P */
377 #ifdef CONFIG_IEEE80211R
378 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
379 #endif /* CONFIG_IEEE80211R */
380 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
381 ap_free_sta(hapd, sta);
386 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
388 struct sta_info *sta;
392 * This could potentially happen with unexpected event from the
393 * driver wrapper. This was seen at least in one case where the
394 * driver ended up reporting a station mode event while hostapd
395 * was running, so better make sure we stop processing such an
398 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
403 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
404 HOSTAPD_LEVEL_INFO, "disassociated");
406 sta = ap_get_sta(hapd, addr);
408 wpa_printf(MSG_DEBUG, "Disassociation notification for "
409 "unknown STA " MACSTR, MAC2STR(addr));
413 ap_sta_set_authorized(hapd, sta, 0);
414 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
415 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
416 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
417 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
418 ap_free_sta(hapd, sta);
422 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
424 struct sta_info *sta = ap_get_sta(hapd, addr);
426 if (!sta || !hapd->conf->disassoc_low_ack)
429 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
430 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
432 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
434 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
438 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
439 int offset, int width, int cf1, int cf2)
442 int channel, chwidth, seg0_idx = 0, seg1_idx = 0, is_dfs;
444 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
446 "driver had channel switch: freq=%d, ht=%d, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
447 freq, ht, offset, width, channel_width_to_string(width),
450 hapd->iface->freq = freq;
452 channel = hostapd_hw_get_channel(hapd, freq);
454 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
455 HOSTAPD_LEVEL_WARNING, "driver switched to "
462 chwidth = VHT_CHANWIDTH_80MHZ;
464 case CHAN_WIDTH_80P80:
465 chwidth = VHT_CHANWIDTH_80P80MHZ;
468 chwidth = VHT_CHANWIDTH_160MHZ;
470 case CHAN_WIDTH_20_NOHT:
474 chwidth = VHT_CHANWIDTH_USE_HT;
478 switch (hapd->iface->current_mode->mode) {
479 case HOSTAPD_MODE_IEEE80211A:
481 seg0_idx = (cf1 - 5000) / 5;
483 seg1_idx = (cf2 - 5000) / 5;
486 seg0_idx = hostapd_hw_get_channel(hapd, cf1);
487 seg1_idx = hostapd_hw_get_channel(hapd, cf2);
491 hapd->iconf->channel = channel;
492 hapd->iconf->ieee80211n = ht;
494 hapd->iconf->ieee80211ac = 0;
495 hapd->iconf->secondary_channel = offset;
496 hapd->iconf->vht_oper_chwidth = chwidth;
497 hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
498 hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
500 is_dfs = ieee80211_is_dfs(freq);
502 if (hapd->csa_in_progress &&
503 freq == hapd->cs_freq_params.freq) {
504 hostapd_cleanup_cs_params(hapd);
505 ieee802_11_set_beacon(hapd);
507 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
508 "freq=%d dfs=%d", freq, is_dfs);
509 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
510 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
511 "freq=%d dfs=%d", freq, is_dfs);
513 #endif /* NEED_AP_MLME */
517 void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
518 const u8 *addr, int reason_code)
520 switch (reason_code) {
521 case MAX_CLIENT_REACHED:
522 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
526 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
534 static void hostapd_acs_channel_selected(struct hostapd_data *hapd,
535 u8 pri_channel, u8 sec_channel)
540 if (hapd->iconf->channel) {
541 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
542 hapd->iconf->channel);
546 hapd->iface->freq = hostapd_hw_get_freq(hapd, pri_channel);
548 channel = pri_channel;
550 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
551 HOSTAPD_LEVEL_WARNING,
552 "driver switched to bad channel");
556 hapd->iconf->channel = channel;
558 if (sec_channel == 0)
559 hapd->iconf->secondary_channel = 0;
560 else if (sec_channel < pri_channel)
561 hapd->iconf->secondary_channel = -1;
562 else if (sec_channel > pri_channel)
563 hapd->iconf->secondary_channel = 1;
565 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
569 ret = hostapd_acs_completed(hapd->iface, 0);
571 wpa_printf(MSG_ERROR,
572 "ACS: Possibly channel configuration is invalid");
575 #endif /* CONFIG_ACS */
578 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
579 const u8 *bssid, const u8 *ie, size_t ie_len,
585 if (sa == NULL || ie == NULL)
588 random_add_randomness(sa, ETH_ALEN);
589 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
590 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
591 sa, da, bssid, ie, ie_len,
603 #ifdef CONFIG_IEEE80211R
604 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
606 u16 auth_transaction, u16 status,
607 const u8 *ies, size_t ies_len)
609 struct hostapd_data *hapd = ctx;
610 struct sta_info *sta;
612 sta = ap_get_sta(hapd, dst);
616 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
617 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
618 sta->flags |= WLAN_STA_AUTH;
620 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
622 #endif /* CONFIG_IEEE80211R */
625 static void hostapd_notif_auth(struct hostapd_data *hapd,
626 struct auth_info *rx_auth)
628 struct sta_info *sta;
629 u16 status = WLAN_STATUS_SUCCESS;
630 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
631 size_t resp_ies_len = 0;
633 sta = ap_get_sta(hapd, rx_auth->peer);
635 sta = ap_sta_add(hapd, rx_auth->peer);
637 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
641 sta->flags &= ~WLAN_STA_PREAUTH;
642 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
643 #ifdef CONFIG_IEEE80211R
644 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
645 sta->auth_alg = WLAN_AUTH_FT;
646 if (sta->wpa_sm == NULL)
647 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
649 if (sta->wpa_sm == NULL) {
650 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
652 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
655 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
656 rx_auth->auth_transaction, rx_auth->ies,
658 hostapd_notify_auth_ft_finish, hapd);
661 #endif /* CONFIG_IEEE80211R */
663 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
664 status, resp_ies, resp_ies_len);
668 static void hostapd_action_rx(struct hostapd_data *hapd,
669 struct rx_mgmt *drv_mgmt)
671 struct ieee80211_mgmt *mgmt;
672 struct sta_info *sta;
673 size_t plen __maybe_unused;
676 if (drv_mgmt->frame_len < 24 + 1)
679 plen = drv_mgmt->frame_len - 24 - 1;
681 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
682 fc = le_to_host16(mgmt->frame_control);
683 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
684 return; /* handled by the driver */
686 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
687 mgmt->u.action.category, (int) plen);
689 sta = ap_get_sta(hapd, mgmt->sa);
691 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
694 #ifdef CONFIG_IEEE80211R
695 if (mgmt->u.action.category == WLAN_ACTION_FT) {
696 const u8 *payload = drv_mgmt->frame + 24 + 1;
697 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
699 #endif /* CONFIG_IEEE80211R */
700 #ifdef CONFIG_IEEE80211W
701 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
702 ieee802_11_sa_query_action(
704 mgmt->u.action.u.sa_query_resp.action,
705 mgmt->u.action.u.sa_query_resp.trans_id);
707 #endif /* CONFIG_IEEE80211W */
709 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
710 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
712 #endif /* CONFIG_WNM */
718 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
720 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
727 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
728 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
729 return HAPD_BROADCAST;
731 for (i = 0; i < iface->num_bss; i++) {
732 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
733 return iface->bss[i];
740 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
741 const u8 *bssid, const u8 *addr,
744 hapd = get_hapd_bssid(hapd->iface, bssid);
745 if (hapd == NULL || hapd == HAPD_BROADCAST)
748 ieee802_11_rx_from_unknown(hapd, addr, wds);
752 static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
754 struct hostapd_iface *iface = hapd->iface;
755 const struct ieee80211_hdr *hdr;
757 struct hostapd_frame_info fi;
760 #ifdef CONFIG_TESTING_OPTIONS
761 if (hapd->ext_mgmt_frame_handling) {
762 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
763 char *hex = os_malloc(hex_len);
765 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
767 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
772 #endif /* CONFIG_TESTING_OPTIONS */
774 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
775 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
779 hapd = get_hapd_bssid(iface, bssid);
782 fc = le_to_host16(hdr->frame_control);
785 * Drop frames to unknown BSSIDs except for Beacon frames which
786 * could be used to update neighbor information.
788 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
789 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
790 hapd = iface->bss[0];
795 os_memset(&fi, 0, sizeof(fi));
796 fi.datarate = rx_mgmt->datarate;
797 fi.ssi_signal = rx_mgmt->ssi_signal;
799 if (hapd == HAPD_BROADCAST) {
802 for (i = 0; i < iface->num_bss; i++) {
803 /* if bss is set, driver will call this function for
804 * each bss individually. */
805 if (rx_mgmt->drv_priv &&
806 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
809 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
810 rx_mgmt->frame_len, &fi) > 0)
814 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
817 random_add_randomness(&fi, sizeof(fi));
823 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
824 size_t len, u16 stype, int ok)
826 struct ieee80211_hdr *hdr;
827 hdr = (struct ieee80211_hdr *) buf;
828 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
829 if (hapd == NULL || hapd == HAPD_BROADCAST)
831 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
834 #endif /* NEED_AP_MLME */
837 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
839 struct sta_info *sta = ap_get_sta(hapd, addr);
843 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
844 " - adding a new STA", MAC2STR(addr));
845 sta = ap_sta_add(hapd, addr);
847 hostapd_new_assoc_sta(hapd, sta, 0);
849 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
858 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
859 const u8 *data, size_t data_len)
861 struct hostapd_iface *iface = hapd->iface;
862 struct sta_info *sta;
865 for (j = 0; j < iface->num_bss; j++) {
866 if ((sta = ap_get_sta(iface->bss[j], src))) {
867 if (sta->flags & WLAN_STA_ASSOC) {
868 hapd = iface->bss[j];
874 ieee802_1x_receive(hapd, src, data, data_len);
878 static struct hostapd_channel_data * hostapd_get_mode_channel(
879 struct hostapd_iface *iface, unsigned int freq)
882 struct hostapd_channel_data *chan;
884 for (i = 0; i < iface->current_mode->num_channels; i++) {
885 chan = &iface->current_mode->channels[i];
888 if ((unsigned int) chan->freq == freq)
896 static void hostapd_update_nf(struct hostapd_iface *iface,
897 struct hostapd_channel_data *chan,
898 struct freq_survey *survey)
900 if (!iface->chans_surveyed) {
901 chan->min_nf = survey->nf;
902 iface->lowest_nf = survey->nf;
904 if (dl_list_empty(&chan->survey_list))
905 chan->min_nf = survey->nf;
906 else if (survey->nf < chan->min_nf)
907 chan->min_nf = survey->nf;
908 if (survey->nf < iface->lowest_nf)
909 iface->lowest_nf = survey->nf;
914 static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
915 struct survey_results *survey_res)
917 struct hostapd_channel_data *chan;
918 struct freq_survey *survey;
919 u64 divisor, dividend;
921 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
923 if (!survey || !survey->freq)
926 chan = hostapd_get_mode_channel(iface, survey->freq);
927 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
930 wpa_printf(MSG_DEBUG, "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
932 (unsigned long int) survey->channel_time,
933 (unsigned long int) survey->channel_time_busy);
935 if (survey->channel_time > iface->last_channel_time &&
936 survey->channel_time > survey->channel_time_busy) {
937 dividend = survey->channel_time_busy -
938 iface->last_channel_time_busy;
939 divisor = survey->channel_time - iface->last_channel_time;
941 iface->channel_utilization = dividend * 255 / divisor;
942 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
943 iface->channel_utilization);
945 iface->last_channel_time = survey->channel_time;
946 iface->last_channel_time_busy = survey->channel_time_busy;
950 static void hostapd_event_get_survey(struct hostapd_data *hapd,
951 struct survey_results *survey_results)
953 struct hostapd_iface *iface = hapd->iface;
954 struct freq_survey *survey, *tmp;
955 struct hostapd_channel_data *chan;
957 if (dl_list_empty(&survey_results->survey_list)) {
958 wpa_printf(MSG_DEBUG, "No survey data received");
962 if (survey_results->freq_filter) {
963 hostapd_single_channel_get_survey(iface, survey_results);
967 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
968 struct freq_survey, list) {
969 chan = hostapd_get_mode_channel(iface, survey->freq);
972 if (chan->flag & HOSTAPD_CHAN_DISABLED)
975 dl_list_del(&survey->list);
976 dl_list_add_tail(&chan->survey_list, &survey->list);
978 hostapd_update_nf(iface, chan, survey);
980 iface->chans_surveyed++;
987 static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
989 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
992 if (hapd->csa_in_progress) {
993 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
995 hostapd_switch_channel_fallback(hapd->iface,
996 &hapd->cs_freq_params);
1001 static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1002 struct dfs_event *radar)
1004 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1005 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
1006 radar->chan_offset, radar->chan_width,
1007 radar->cf1, radar->cf2);
1011 static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1012 struct dfs_event *radar)
1014 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1015 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
1016 radar->chan_offset, radar->chan_width,
1017 radar->cf1, radar->cf2);
1021 static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1022 struct dfs_event *radar)
1024 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1025 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
1026 radar->chan_offset, radar->chan_width,
1027 radar->cf1, radar->cf2);
1031 static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1032 struct dfs_event *radar)
1034 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1035 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
1036 radar->chan_offset, radar->chan_width,
1037 radar->cf1, radar->cf2);
1041 static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1042 struct dfs_event *radar)
1044 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1045 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1046 radar->chan_offset, radar->chan_width,
1047 radar->cf1, radar->cf2);
1050 #endif /* NEED_AP_MLME */
1053 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1054 union wpa_event_data *data)
1056 struct hostapd_data *hapd = ctx;
1057 #ifndef CONFIG_NO_STDOUT_DEBUG
1058 int level = MSG_DEBUG;
1060 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
1061 data->rx_mgmt.frame_len >= 24) {
1062 const struct ieee80211_hdr *hdr;
1064 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1065 fc = le_to_host16(hdr->frame_control);
1066 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1067 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1068 level = MSG_EXCESSIVE;
1069 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1070 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1071 level = MSG_EXCESSIVE;
1074 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1075 event_to_string(event), event);
1076 #endif /* CONFIG_NO_STDOUT_DEBUG */
1079 case EVENT_MICHAEL_MIC_FAILURE:
1080 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1082 case EVENT_SCAN_RESULTS:
1083 if (hapd->iface->scan_cb)
1084 hapd->iface->scan_cb(hapd->iface);
1086 case EVENT_WPS_BUTTON_PUSHED:
1087 hostapd_wps_button_pushed(hapd, NULL);
1090 case EVENT_TX_STATUS:
1091 switch (data->tx_status.type) {
1092 case WLAN_FC_TYPE_MGMT:
1093 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1094 data->tx_status.data_len,
1095 data->tx_status.stype,
1096 data->tx_status.ack);
1098 case WLAN_FC_TYPE_DATA:
1099 hostapd_tx_status(hapd, data->tx_status.dst,
1100 data->tx_status.data,
1101 data->tx_status.data_len,
1102 data->tx_status.ack);
1106 case EVENT_EAPOL_TX_STATUS:
1107 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1108 data->eapol_tx_status.data,
1109 data->eapol_tx_status.data_len,
1110 data->eapol_tx_status.ack);
1112 case EVENT_DRIVER_CLIENT_POLL_OK:
1113 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1115 case EVENT_RX_FROM_UNKNOWN:
1116 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1117 data->rx_from_unknown.addr,
1118 data->rx_from_unknown.wds);
1120 #endif /* NEED_AP_MLME */
1122 if (!data->rx_mgmt.frame)
1125 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1127 #endif /* NEED_AP_MLME */
1128 hostapd_action_rx(hapd, &data->rx_mgmt);
1130 case EVENT_RX_PROBE_REQ:
1131 if (data->rx_probe_req.sa == NULL ||
1132 data->rx_probe_req.ie == NULL)
1134 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
1135 data->rx_probe_req.da,
1136 data->rx_probe_req.bssid,
1137 data->rx_probe_req.ie,
1138 data->rx_probe_req.ie_len,
1139 data->rx_probe_req.ssi_signal);
1142 hostapd_event_new_sta(hapd, data->new_sta.addr);
1144 case EVENT_EAPOL_RX:
1145 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1146 data->eapol_rx.data,
1147 data->eapol_rx.data_len);
1152 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1153 data->assoc_info.req_ies,
1154 data->assoc_info.req_ies_len,
1155 data->assoc_info.reassoc);
1157 case EVENT_DISASSOC:
1159 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1163 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1165 case EVENT_STATION_LOW_ACK:
1168 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1171 hostapd_notif_auth(hapd, &data->auth);
1173 case EVENT_CH_SWITCH:
1176 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1177 data->ch_switch.ht_enabled,
1178 data->ch_switch.ch_offset,
1179 data->ch_switch.ch_width,
1180 data->ch_switch.cf1,
1181 data->ch_switch.cf2);
1183 case EVENT_CONNECT_FAILED_REASON:
1186 hostapd_event_connect_failed_reason(
1187 hapd, data->connect_failed_reason.addr,
1188 data->connect_failed_reason.code);
1191 hostapd_event_get_survey(hapd, &data->survey_results);
1194 case EVENT_INTERFACE_UNAVAILABLE:
1195 hostapd_event_iface_unavailable(hapd);
1197 case EVENT_DFS_RADAR_DETECTED:
1200 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1202 case EVENT_DFS_CAC_FINISHED:
1205 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1207 case EVENT_DFS_CAC_ABORTED:
1210 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1212 case EVENT_DFS_NOP_FINISHED:
1215 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1217 case EVENT_CHANNEL_LIST_CHANGED:
1218 /* channel list changed (regulatory?), update channel list */
1219 /* TODO: check this. hostapd_get_hw_features() initializes
1220 * too much stuff. */
1221 /* hostapd_get_hw_features(hapd->iface); */
1222 hostapd_channel_list_updated(
1223 hapd->iface, data->channel_list_changed.initiator);
1225 case EVENT_DFS_CAC_STARTED:
1228 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1230 #endif /* NEED_AP_MLME */
1231 case EVENT_INTERFACE_ENABLED:
1232 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
1233 if (hapd->disabled && hapd->started) {
1236 * Try to re-enable interface if the driver stopped it
1237 * when the interface got disabled.
1239 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1240 hapd->reenable_beacon = 1;
1241 ieee802_11_set_beacon(hapd);
1244 case EVENT_INTERFACE_DISABLED:
1245 hostapd_free_stas(hapd);
1246 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
1250 case EVENT_ACS_CHANNEL_SELECTED:
1251 hostapd_acs_channel_selected(
1252 hapd, data->acs_selected_channels.pri_channel,
1253 data->acs_selected_channels.sec_channel);
1255 #endif /* CONFIG_ACS */
1257 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1262 #endif /* HOSTAPD */