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"
24 #include "ieee802_11.h"
26 #include "accounting.h"
27 #include "tkip_countermeasures.h"
28 #include "ieee802_1x.h"
30 #include "wps_hostapd.h"
31 #include "ap_drv_ops.h"
32 #include "ap_config.h"
33 #include "hw_features.h"
38 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
39 const u8 *req_ies, size_t req_ies_len, int reassoc)
43 struct ieee802_11_elems elems;
46 #if defined(CONFIG_IEEE80211R) || defined(CONFIG_IEEE80211W)
47 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
49 #endif /* CONFIG_IEEE80211R || CONFIG_IEEE80211W */
50 u16 reason = WLAN_REASON_UNSPECIFIED;
51 u16 status = WLAN_STATUS_SUCCESS;
52 const u8 *p2p_dev_addr = NULL;
56 * This could potentially happen with unexpected event from the
57 * driver wrapper. This was seen at least in one case where the
58 * driver ended up being set to station mode while hostapd was
59 * running, so better make sure we stop processing such an
63 "hostapd_notif_assoc: Skip event with no address");
66 random_add_randomness(addr, ETH_ALEN);
68 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
69 HOSTAPD_LEVEL_INFO, "associated");
71 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
73 ie = elems.wps_ie - 2;
74 ielen = elems.wps_ie_len + 2;
75 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
76 } else if (elems.rsn_ie) {
77 ie = elems.rsn_ie - 2;
78 ielen = elems.rsn_ie_len + 2;
79 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
80 } else if (elems.wpa_ie) {
81 ie = elems.wpa_ie - 2;
82 ielen = elems.wpa_ie_len + 2;
83 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
85 } else if (elems.osen) {
87 ielen = elems.osen_len + 2;
88 wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
89 #endif /* CONFIG_HS20 */
94 "STA did not include WPS/RSN/WPA IE in (Re)AssocReq");
97 sta = ap_get_sta(hapd, addr);
99 ap_sta_no_session_timeout(hapd, sta);
100 accounting_sta_stop(hapd, sta);
103 * Make sure that the previously registered inactivity timer
104 * will not remove the STA immediately.
106 sta->timeout_next = STA_NULLFUNC;
108 sta = ap_sta_add(hapd, addr);
110 hostapd_drv_sta_disassoc(hapd, addr,
111 WLAN_REASON_DISASSOC_AP_BUSY);
115 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
119 wpabuf_free(sta->p2p_ie);
120 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
123 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
125 #endif /* CONFIG_P2P */
127 #ifdef CONFIG_IEEE80211N
129 if (elems.ht_capabilities &&
130 (hapd->iface->conf->ht_capab &
131 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
132 struct ieee80211_ht_capabilities *ht_cap =
133 (struct ieee80211_ht_capabilities *)
134 elems.ht_capabilities;
136 if (le_to_host16(ht_cap->ht_capabilities_info) &
137 HT_CAP_INFO_40MHZ_INTOLERANT)
138 ht40_intolerant_add(hapd->iface, sta);
140 #endif /* NEED_AP_MLME */
141 #endif /* CONFIG_IEEE80211N */
143 #ifdef CONFIG_INTERWORKING
144 if (elems.ext_capab && elems.ext_capab_len > 4) {
145 if (elems.ext_capab[4] & 0x01)
146 sta->qos_map_enabled = 1;
148 #endif /* CONFIG_INTERWORKING */
151 wpabuf_free(sta->hs20_ie);
152 if (elems.hs20 && elems.hs20_len > 4) {
153 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
157 #endif /* CONFIG_HS20 */
160 wpabuf_free(sta->mb_ies);
161 if (hapd->iface->fst)
162 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
165 #endif /* CONFIG_FST */
167 if (hapd->conf->wpa) {
168 if (ie == NULL || ielen == 0) {
170 if (hapd->conf->wps_state) {
171 wpa_printf(MSG_DEBUG,
172 "STA did not include WPA/RSN IE in (Re)Association Request - possible WPS use");
173 sta->flags |= WLAN_STA_MAYBE_WPS;
176 #endif /* CONFIG_WPS */
178 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
182 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
183 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
186 sta->flags |= WLAN_STA_WPS;
187 wps = ieee802_11_vendor_ie_concat(ie, ielen,
190 if (wps_is_20(wps)) {
191 wpa_printf(MSG_DEBUG,
192 "WPS: STA supports WPS 2.0");
193 sta->flags |= WLAN_STA_WPS2;
199 #endif /* CONFIG_WPS */
201 if (sta->wpa_sm == NULL)
202 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
205 if (sta->wpa_sm == NULL) {
206 wpa_printf(MSG_ERROR,
207 "Failed to initialize WPA state machine");
210 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
212 elems.mdie, elems.mdie_len);
213 if (res != WPA_IE_OK) {
214 wpa_printf(MSG_DEBUG,
215 "WPA/RSN information element rejected? (res %u)",
217 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
218 if (res == WPA_INVALID_GROUP) {
219 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
220 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
221 } else if (res == WPA_INVALID_PAIRWISE) {
222 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
223 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
224 } else if (res == WPA_INVALID_AKMP) {
225 reason = WLAN_REASON_AKMP_NOT_VALID;
226 status = WLAN_STATUS_AKMP_NOT_VALID;
228 #ifdef CONFIG_IEEE80211W
229 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
230 reason = WLAN_REASON_INVALID_IE;
231 status = WLAN_STATUS_INVALID_IE;
232 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
233 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
234 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
236 #endif /* CONFIG_IEEE80211W */
238 reason = WLAN_REASON_INVALID_IE;
239 status = WLAN_STATUS_INVALID_IE;
243 #ifdef CONFIG_IEEE80211W
244 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
245 sta->sa_query_count > 0)
246 ap_check_sa_query_timeout(hapd, sta);
247 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
248 (sta->auth_alg != WLAN_AUTH_FT)) {
250 * STA has already been associated with MFP and SA
251 * Query timeout has not been reached. Reject the
252 * association attempt temporarily and start SA Query,
253 * if one is not pending.
256 if (sta->sa_query_count == 0)
257 ap_sta_start_sa_query(hapd, sta);
259 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
261 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
263 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
268 if (wpa_auth_uses_mfp(sta->wpa_sm))
269 sta->flags |= WLAN_STA_MFP;
271 sta->flags &= ~WLAN_STA_MFP;
272 #endif /* CONFIG_IEEE80211W */
274 #ifdef CONFIG_IEEE80211R
275 if (sta->auth_alg == WLAN_AUTH_FT) {
276 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
278 if (status != WLAN_STATUS_SUCCESS) {
279 if (status == WLAN_STATUS_INVALID_PMKID)
280 reason = WLAN_REASON_INVALID_IE;
281 if (status == WLAN_STATUS_INVALID_MDIE)
282 reason = WLAN_REASON_INVALID_IE;
283 if (status == WLAN_STATUS_INVALID_FTIE)
284 reason = WLAN_REASON_INVALID_IE;
288 #endif /* CONFIG_IEEE80211R */
289 } else if (hapd->conf->wps_state) {
294 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
298 #ifdef CONFIG_WPS_STRICT
299 if (wps && wps_validate_assoc_req(wps) < 0) {
300 reason = WLAN_REASON_INVALID_IE;
301 status = WLAN_STATUS_INVALID_IE;
305 #endif /* CONFIG_WPS_STRICT */
307 sta->flags |= WLAN_STA_WPS;
308 if (wps_is_20(wps)) {
309 wpa_printf(MSG_DEBUG,
310 "WPS: STA supports WPS 2.0");
311 sta->flags |= WLAN_STA_WPS2;
314 sta->flags |= WLAN_STA_MAYBE_WPS;
316 #endif /* CONFIG_WPS */
318 } else if (hapd->conf->osen) {
319 if (elems.osen == NULL) {
321 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
323 "No HS 2.0 OSEN element in association request");
324 return WLAN_STATUS_INVALID_IE;
327 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
328 if (sta->wpa_sm == NULL)
329 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
331 if (sta->wpa_sm == NULL) {
332 wpa_printf(MSG_WARNING,
333 "Failed to initialize WPA state machine");
334 return WLAN_STATUS_UNSPECIFIED_FAILURE;
336 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
337 elems.osen - 2, elems.osen_len + 2) < 0)
338 return WLAN_STATUS_INVALID_IE;
339 #endif /* CONFIG_HS20 */
343 #endif /* CONFIG_WPS */
345 #ifdef CONFIG_IEEE80211R
346 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
347 sta->auth_alg, req_ies, req_ies_len);
349 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
351 if (sta->auth_alg == WLAN_AUTH_FT)
352 ap_sta_set_authorized(hapd, sta, 1);
353 #else /* CONFIG_IEEE80211R */
354 /* Keep compiler silent about unused variables */
357 #endif /* CONFIG_IEEE80211R */
359 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
360 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
361 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
363 hostapd_set_sta_flags(hapd, sta);
365 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
366 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
368 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
370 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
372 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
376 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
377 req_ies, req_ies_len);
379 #endif /* CONFIG_P2P */
384 #ifdef CONFIG_IEEE80211R
385 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
386 #endif /* CONFIG_IEEE80211R */
387 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
388 ap_free_sta(hapd, sta);
393 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
395 struct sta_info *sta;
399 * This could potentially happen with unexpected event from the
400 * driver wrapper. This was seen at least in one case where the
401 * driver ended up reporting a station mode event while hostapd
402 * was running, so better make sure we stop processing such an
405 wpa_printf(MSG_DEBUG,
406 "hostapd_notif_disassoc: Skip event with no address");
410 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
411 HOSTAPD_LEVEL_INFO, "disassociated");
413 sta = ap_get_sta(hapd, addr);
415 wpa_printf(MSG_DEBUG,
416 "Disassociation notification for unknown STA "
417 MACSTR, MAC2STR(addr));
421 ap_sta_set_authorized(hapd, sta, 0);
422 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
423 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
424 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
425 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
426 ap_free_sta(hapd, sta);
430 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
432 struct sta_info *sta = ap_get_sta(hapd, addr);
434 if (!sta || !hapd->conf->disassoc_low_ack)
437 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
439 "disconnected due to excessive missing ACKs");
440 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
442 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
446 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
447 int offset, int width, int cf1, int cf2)
450 int channel, chwidth, seg0_idx = 0, seg1_idx = 0, is_dfs;
452 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
454 "driver had channel switch: freq=%d, ht=%d, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
455 freq, ht, offset, width, channel_width_to_string(width),
458 hapd->iface->freq = freq;
460 channel = hostapd_hw_get_channel(hapd, freq);
462 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
463 HOSTAPD_LEVEL_WARNING,
464 "driver switched to bad channel!");
470 chwidth = VHT_CHANWIDTH_80MHZ;
472 case CHAN_WIDTH_80P80:
473 chwidth = VHT_CHANWIDTH_80P80MHZ;
476 chwidth = VHT_CHANWIDTH_160MHZ;
478 case CHAN_WIDTH_20_NOHT:
482 chwidth = VHT_CHANWIDTH_USE_HT;
486 switch (hapd->iface->current_mode->mode) {
487 case HOSTAPD_MODE_IEEE80211A:
489 seg0_idx = (cf1 - 5000) / 5;
491 seg1_idx = (cf2 - 5000) / 5;
494 seg0_idx = hostapd_hw_get_channel(hapd, cf1);
495 seg1_idx = hostapd_hw_get_channel(hapd, cf2);
499 hapd->iconf->channel = channel;
500 hapd->iconf->ieee80211n = ht;
502 hapd->iconf->ieee80211ac = 0;
503 hapd->iconf->secondary_channel = offset;
504 hapd->iconf->vht_oper_chwidth = chwidth;
505 hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
506 hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
508 is_dfs = ieee80211_is_dfs(freq);
510 if (hapd->csa_in_progress &&
511 freq == hapd->cs_freq_params.freq) {
512 hostapd_cleanup_cs_params(hapd);
513 ieee802_11_set_beacon(hapd);
515 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
516 "freq=%d dfs=%d", freq, is_dfs);
517 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
518 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
519 "freq=%d dfs=%d", freq, is_dfs);
521 #endif /* NEED_AP_MLME */
525 void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
526 const u8 *addr, int reason_code)
528 switch (reason_code) {
529 case MAX_CLIENT_REACHED:
530 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
534 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
542 static void hostapd_acs_channel_selected(struct hostapd_data *hapd,
543 struct acs_selected_channels *acs_res)
547 if (hapd->iconf->channel) {
548 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
549 hapd->iconf->channel);
553 if (!hapd->iface->current_mode) {
554 for (i = 0; i < hapd->iface->num_hw_features; i++) {
555 struct hostapd_hw_modes *mode =
556 &hapd->iface->hw_features[i];
558 if (mode->mode == acs_res->hw_mode) {
559 hapd->iface->current_mode = mode;
563 if (!hapd->iface->current_mode) {
564 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
565 HOSTAPD_LEVEL_WARNING,
566 "driver selected to bad hw_mode");
571 hapd->iface->freq = hostapd_hw_get_freq(hapd, acs_res->pri_channel);
573 if (!acs_res->pri_channel) {
574 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
575 HOSTAPD_LEVEL_WARNING,
576 "driver switched to bad channel");
580 hapd->iconf->channel = acs_res->pri_channel;
581 hapd->iconf->acs = 1;
583 if (acs_res->sec_channel == 0)
584 hapd->iconf->secondary_channel = 0;
585 else if (acs_res->sec_channel < acs_res->pri_channel)
586 hapd->iconf->secondary_channel = -1;
587 else if (acs_res->sec_channel > acs_res->pri_channel)
588 hapd->iconf->secondary_channel = 1;
590 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
594 if (hapd->iface->conf->ieee80211ac) {
595 /* set defaults for backwards compatibility */
596 hapd->iconf->vht_oper_centr_freq_seg1_idx = 0;
597 hapd->iconf->vht_oper_centr_freq_seg0_idx = 0;
598 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
599 if (acs_res->ch_width == 80) {
600 hapd->iconf->vht_oper_centr_freq_seg0_idx =
601 acs_res->vht_seg0_center_ch;
602 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
603 } else if (acs_res->ch_width == 160) {
604 if (acs_res->vht_seg1_center_ch == 0) {
605 hapd->iconf->vht_oper_centr_freq_seg0_idx =
606 acs_res->vht_seg0_center_ch;
607 hapd->iconf->vht_oper_chwidth =
608 VHT_CHANWIDTH_160MHZ;
610 hapd->iconf->vht_oper_centr_freq_seg0_idx =
611 acs_res->vht_seg0_center_ch;
612 hapd->iconf->vht_oper_centr_freq_seg1_idx =
613 acs_res->vht_seg1_center_ch;
614 hapd->iconf->vht_oper_chwidth =
615 VHT_CHANWIDTH_80P80MHZ;
620 ret = hostapd_acs_completed(hapd->iface, 0);
622 wpa_printf(MSG_ERROR,
623 "ACS: Possibly channel configuration is invalid");
626 #endif /* CONFIG_ACS */
629 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
630 const u8 *bssid, const u8 *ie, size_t ie_len,
636 if (sa == NULL || ie == NULL)
639 random_add_randomness(sa, ETH_ALEN);
640 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
641 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
642 sa, da, bssid, ie, ie_len,
654 #ifdef CONFIG_IEEE80211R
655 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
657 u16 auth_transaction, u16 status,
658 const u8 *ies, size_t ies_len)
660 struct hostapd_data *hapd = ctx;
661 struct sta_info *sta;
663 sta = ap_get_sta(hapd, dst);
667 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
668 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
669 sta->flags |= WLAN_STA_AUTH;
671 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
673 #endif /* CONFIG_IEEE80211R */
676 static void hostapd_notif_auth(struct hostapd_data *hapd,
677 struct auth_info *rx_auth)
679 struct sta_info *sta;
680 u16 status = WLAN_STATUS_SUCCESS;
681 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
682 size_t resp_ies_len = 0;
684 sta = ap_get_sta(hapd, rx_auth->peer);
686 sta = ap_sta_add(hapd, rx_auth->peer);
688 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
692 sta->flags &= ~WLAN_STA_PREAUTH;
693 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
694 #ifdef CONFIG_IEEE80211R
695 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
696 sta->auth_alg = WLAN_AUTH_FT;
697 if (sta->wpa_sm == NULL)
698 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
700 if (sta->wpa_sm == NULL) {
701 wpa_printf(MSG_DEBUG,
702 "FT: Failed to initialize WPA state machine");
703 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
706 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
707 rx_auth->auth_transaction, rx_auth->ies,
709 hostapd_notify_auth_ft_finish, hapd);
712 #endif /* CONFIG_IEEE80211R */
714 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
715 status, resp_ies, resp_ies_len);
719 static void hostapd_action_rx(struct hostapd_data *hapd,
720 struct rx_mgmt *drv_mgmt)
722 struct ieee80211_mgmt *mgmt;
723 struct sta_info *sta;
724 size_t plen __maybe_unused;
727 if (drv_mgmt->frame_len < 24 + 1)
730 plen = drv_mgmt->frame_len - 24 - 1;
732 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
733 fc = le_to_host16(mgmt->frame_control);
734 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
735 return; /* handled by the driver */
737 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
738 mgmt->u.action.category, (int) plen);
740 sta = ap_get_sta(hapd, mgmt->sa);
742 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
745 #ifdef CONFIG_IEEE80211R
746 if (mgmt->u.action.category == WLAN_ACTION_FT) {
747 const u8 *payload = drv_mgmt->frame + 24 + 1;
749 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
751 #endif /* CONFIG_IEEE80211R */
752 #ifdef CONFIG_IEEE80211W
753 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
754 ieee802_11_sa_query_action(
756 mgmt->u.action.u.sa_query_resp.action,
757 mgmt->u.action.u.sa_query_resp.trans_id);
759 #endif /* CONFIG_IEEE80211W */
761 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
762 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
764 #endif /* CONFIG_WNM */
766 if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
767 fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
770 #endif /* CONFIG_FST */
777 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
779 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
786 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
787 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
788 return HAPD_BROADCAST;
790 for (i = 0; i < iface->num_bss; i++) {
791 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
792 return iface->bss[i];
799 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
800 const u8 *bssid, const u8 *addr,
803 hapd = get_hapd_bssid(hapd->iface, bssid);
804 if (hapd == NULL || hapd == HAPD_BROADCAST)
807 ieee802_11_rx_from_unknown(hapd, addr, wds);
811 static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
813 struct hostapd_iface *iface = hapd->iface;
814 const struct ieee80211_hdr *hdr;
816 struct hostapd_frame_info fi;
819 #ifdef CONFIG_TESTING_OPTIONS
820 if (hapd->ext_mgmt_frame_handling) {
821 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
822 char *hex = os_malloc(hex_len);
825 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
827 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
832 #endif /* CONFIG_TESTING_OPTIONS */
834 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
835 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
839 hapd = get_hapd_bssid(iface, bssid);
841 u16 fc = le_to_host16(hdr->frame_control);
844 * Drop frames to unknown BSSIDs except for Beacon frames which
845 * could be used to update neighbor information.
847 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
848 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
849 hapd = iface->bss[0];
854 os_memset(&fi, 0, sizeof(fi));
855 fi.datarate = rx_mgmt->datarate;
856 fi.ssi_signal = rx_mgmt->ssi_signal;
858 if (hapd == HAPD_BROADCAST) {
862 for (i = 0; i < iface->num_bss; i++) {
863 /* if bss is set, driver will call this function for
864 * each bss individually. */
865 if (rx_mgmt->drv_priv &&
866 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
869 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
870 rx_mgmt->frame_len, &fi) > 0)
874 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
877 random_add_randomness(&fi, sizeof(fi));
883 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
884 size_t len, u16 stype, int ok)
886 struct ieee80211_hdr *hdr;
888 hdr = (struct ieee80211_hdr *) buf;
889 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
890 if (hapd == NULL || hapd == HAPD_BROADCAST)
892 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
895 #endif /* NEED_AP_MLME */
898 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
900 struct sta_info *sta = ap_get_sta(hapd, addr);
905 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
906 " - adding a new STA", MAC2STR(addr));
907 sta = ap_sta_add(hapd, addr);
909 hostapd_new_assoc_sta(hapd, sta, 0);
911 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
920 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
921 const u8 *data, size_t data_len)
923 struct hostapd_iface *iface = hapd->iface;
924 struct sta_info *sta;
927 for (j = 0; j < iface->num_bss; j++) {
928 sta = ap_get_sta(iface->bss[j], src);
929 if (sta && sta->flags & WLAN_STA_ASSOC) {
930 hapd = iface->bss[j];
935 ieee802_1x_receive(hapd, src, data, data_len);
939 static struct hostapd_channel_data * hostapd_get_mode_channel(
940 struct hostapd_iface *iface, unsigned int freq)
943 struct hostapd_channel_data *chan;
945 for (i = 0; i < iface->current_mode->num_channels; i++) {
946 chan = &iface->current_mode->channels[i];
949 if ((unsigned int) chan->freq == freq)
957 static void hostapd_update_nf(struct hostapd_iface *iface,
958 struct hostapd_channel_data *chan,
959 struct freq_survey *survey)
961 if (!iface->chans_surveyed) {
962 chan->min_nf = survey->nf;
963 iface->lowest_nf = survey->nf;
965 if (dl_list_empty(&chan->survey_list))
966 chan->min_nf = survey->nf;
967 else if (survey->nf < chan->min_nf)
968 chan->min_nf = survey->nf;
969 if (survey->nf < iface->lowest_nf)
970 iface->lowest_nf = survey->nf;
975 static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
976 struct survey_results *survey_res)
978 struct hostapd_channel_data *chan;
979 struct freq_survey *survey;
980 u64 divisor, dividend;
982 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
984 if (!survey || !survey->freq)
987 chan = hostapd_get_mode_channel(iface, survey->freq);
988 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
991 wpa_printf(MSG_DEBUG,
992 "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
994 (unsigned long int) survey->channel_time,
995 (unsigned long int) survey->channel_time_busy);
997 if (survey->channel_time > iface->last_channel_time &&
998 survey->channel_time > survey->channel_time_busy) {
999 dividend = survey->channel_time_busy -
1000 iface->last_channel_time_busy;
1001 divisor = survey->channel_time - iface->last_channel_time;
1003 iface->channel_utilization = dividend * 255 / divisor;
1004 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
1005 iface->channel_utilization);
1007 iface->last_channel_time = survey->channel_time;
1008 iface->last_channel_time_busy = survey->channel_time_busy;
1012 static void hostapd_event_get_survey(struct hostapd_data *hapd,
1013 struct survey_results *survey_results)
1015 struct hostapd_iface *iface = hapd->iface;
1016 struct freq_survey *survey, *tmp;
1017 struct hostapd_channel_data *chan;
1019 if (dl_list_empty(&survey_results->survey_list)) {
1020 wpa_printf(MSG_DEBUG, "No survey data received");
1024 if (survey_results->freq_filter) {
1025 hostapd_single_channel_get_survey(iface, survey_results);
1029 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1030 struct freq_survey, list) {
1031 chan = hostapd_get_mode_channel(iface, survey->freq);
1034 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1037 dl_list_del(&survey->list);
1038 dl_list_add_tail(&chan->survey_list, &survey->list);
1040 hostapd_update_nf(iface, chan, survey);
1042 iface->chans_surveyed++;
1049 static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1051 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1054 if (hapd->csa_in_progress) {
1055 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1057 hostapd_switch_channel_fallback(hapd->iface,
1058 &hapd->cs_freq_params);
1063 static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1064 struct dfs_event *radar)
1066 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1067 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
1068 radar->chan_offset, radar->chan_width,
1069 radar->cf1, radar->cf2);
1073 static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1074 struct dfs_event *radar)
1076 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1077 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
1078 radar->chan_offset, radar->chan_width,
1079 radar->cf1, radar->cf2);
1083 static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1084 struct dfs_event *radar)
1086 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1087 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
1088 radar->chan_offset, radar->chan_width,
1089 radar->cf1, radar->cf2);
1093 static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1094 struct dfs_event *radar)
1096 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1097 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
1098 radar->chan_offset, radar->chan_width,
1099 radar->cf1, radar->cf2);
1103 static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1104 struct dfs_event *radar)
1106 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1107 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1108 radar->chan_offset, radar->chan_width,
1109 radar->cf1, radar->cf2);
1112 #endif /* NEED_AP_MLME */
1115 void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1116 union wpa_event_data *data)
1118 struct hostapd_data *hapd = ctx;
1119 #ifndef CONFIG_NO_STDOUT_DEBUG
1120 int level = MSG_DEBUG;
1122 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
1123 data->rx_mgmt.frame_len >= 24) {
1124 const struct ieee80211_hdr *hdr;
1127 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1128 fc = le_to_host16(hdr->frame_control);
1129 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1130 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1131 level = MSG_EXCESSIVE;
1132 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1133 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1134 level = MSG_EXCESSIVE;
1137 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1138 event_to_string(event), event);
1139 #endif /* CONFIG_NO_STDOUT_DEBUG */
1142 case EVENT_MICHAEL_MIC_FAILURE:
1143 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1145 case EVENT_SCAN_RESULTS:
1146 if (hapd->iface->scan_cb)
1147 hapd->iface->scan_cb(hapd->iface);
1149 case EVENT_WPS_BUTTON_PUSHED:
1150 hostapd_wps_button_pushed(hapd, NULL);
1153 case EVENT_TX_STATUS:
1154 switch (data->tx_status.type) {
1155 case WLAN_FC_TYPE_MGMT:
1156 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1157 data->tx_status.data_len,
1158 data->tx_status.stype,
1159 data->tx_status.ack);
1161 case WLAN_FC_TYPE_DATA:
1162 hostapd_tx_status(hapd, data->tx_status.dst,
1163 data->tx_status.data,
1164 data->tx_status.data_len,
1165 data->tx_status.ack);
1169 case EVENT_EAPOL_TX_STATUS:
1170 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1171 data->eapol_tx_status.data,
1172 data->eapol_tx_status.data_len,
1173 data->eapol_tx_status.ack);
1175 case EVENT_DRIVER_CLIENT_POLL_OK:
1176 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1178 case EVENT_RX_FROM_UNKNOWN:
1179 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1180 data->rx_from_unknown.addr,
1181 data->rx_from_unknown.wds);
1183 #endif /* NEED_AP_MLME */
1185 if (!data->rx_mgmt.frame)
1188 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1190 #endif /* NEED_AP_MLME */
1191 hostapd_action_rx(hapd, &data->rx_mgmt);
1193 case EVENT_RX_PROBE_REQ:
1194 if (data->rx_probe_req.sa == NULL ||
1195 data->rx_probe_req.ie == NULL)
1197 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
1198 data->rx_probe_req.da,
1199 data->rx_probe_req.bssid,
1200 data->rx_probe_req.ie,
1201 data->rx_probe_req.ie_len,
1202 data->rx_probe_req.ssi_signal);
1205 hostapd_event_new_sta(hapd, data->new_sta.addr);
1207 case EVENT_EAPOL_RX:
1208 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1209 data->eapol_rx.data,
1210 data->eapol_rx.data_len);
1215 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1216 data->assoc_info.req_ies,
1217 data->assoc_info.req_ies_len,
1218 data->assoc_info.reassoc);
1220 case EVENT_DISASSOC:
1222 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1226 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1228 case EVENT_STATION_LOW_ACK:
1231 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1234 hostapd_notif_auth(hapd, &data->auth);
1236 case EVENT_CH_SWITCH:
1239 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1240 data->ch_switch.ht_enabled,
1241 data->ch_switch.ch_offset,
1242 data->ch_switch.ch_width,
1243 data->ch_switch.cf1,
1244 data->ch_switch.cf2);
1246 case EVENT_CONNECT_FAILED_REASON:
1249 hostapd_event_connect_failed_reason(
1250 hapd, data->connect_failed_reason.addr,
1251 data->connect_failed_reason.code);
1254 hostapd_event_get_survey(hapd, &data->survey_results);
1257 case EVENT_INTERFACE_UNAVAILABLE:
1258 hostapd_event_iface_unavailable(hapd);
1260 case EVENT_DFS_RADAR_DETECTED:
1263 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1265 case EVENT_DFS_CAC_FINISHED:
1268 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1270 case EVENT_DFS_CAC_ABORTED:
1273 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1275 case EVENT_DFS_NOP_FINISHED:
1278 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1280 case EVENT_CHANNEL_LIST_CHANGED:
1281 /* channel list changed (regulatory?), update channel list */
1282 /* TODO: check this. hostapd_get_hw_features() initializes
1283 * too much stuff. */
1284 /* hostapd_get_hw_features(hapd->iface); */
1285 hostapd_channel_list_updated(
1286 hapd->iface, data->channel_list_changed.initiator);
1288 case EVENT_DFS_CAC_STARTED:
1291 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1293 #endif /* NEED_AP_MLME */
1294 case EVENT_INTERFACE_ENABLED:
1295 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
1296 if (hapd->disabled && hapd->started) {
1299 * Try to re-enable interface if the driver stopped it
1300 * when the interface got disabled.
1302 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1303 hapd->reenable_beacon = 1;
1304 ieee802_11_set_beacon(hapd);
1307 case EVENT_INTERFACE_DISABLED:
1308 hostapd_free_stas(hapd);
1309 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
1313 case EVENT_ACS_CHANNEL_SELECTED:
1314 hostapd_acs_channel_selected(hapd,
1315 &data->acs_selected_channels);
1317 #endif /* CONFIG_ACS */
1319 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1324 #endif /* HOSTAPD */