]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/drv_callbacks.c
FILS: Make handle_auth_fils() re-usable for driver-based AP SME
[thirdparty/hostap.git] / src / ap / drv_callbacks.c
CommitLineData
b5b969e9
JM
1/*
2 * hostapd / Callback functions for driver wrappers
94ddef3e 3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
b5b969e9 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
b5b969e9
JM
7 */
8
6226e38d 9#include "utils/includes.h"
b5b969e9 10
6226e38d 11#include "utils/common.h"
9c47f6a2 12#include "utils/eloop.h"
b5b969e9 13#include "radius/radius.h"
6e6e8c31 14#include "drivers/driver.h"
81f4f619 15#include "common/ieee802_11_defs.h"
c41a1095 16#include "common/ieee802_11_common.h"
3140803b 17#include "common/wpa_ctrl.h"
bbb921da 18#include "crypto/random.h"
ef796391 19#include "p2p/p2p.h"
54f489be 20#include "wps/wps.h"
037378ff 21#include "fst/fst.h"
d32d94db 22#include "wnm_ap.h"
6226e38d
JM
23#include "hostapd.h"
24#include "ieee802_11.h"
0603bcb7 25#include "ieee802_11_auth.h"
6226e38d
JM
26#include "sta_info.h"
27#include "accounting.h"
28#include "tkip_countermeasures.h"
6226e38d
JM
29#include "ieee802_1x.h"
30#include "wpa_auth.h"
6226e38d 31#include "wps_hostapd.h"
51e2a27a 32#include "ap_drv_ops.h"
8b06c1ed 33#include "ap_config.h"
1b487b8b 34#include "hw_features.h"
e76da505 35#include "dfs.h"
9c47f6a2 36#include "beacon.h"
6332aaf3 37#include "mbo_ap.h"
b5b969e9
JM
38
39
b5b969e9 40int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
2bb20281 41 const u8 *req_ies, size_t req_ies_len, int reassoc)
b5b969e9
JM
42{
43 struct sta_info *sta;
44 int new_assoc, res;
c41a1095 45 struct ieee802_11_elems elems;
2bb20281
JM
46 const u8 *ie;
47 size_t ielen;
4ec1fd8e 48#if defined(CONFIG_IEEE80211R_AP) || defined(CONFIG_IEEE80211W)
88b32a99
SP
49 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
50 u8 *p = buf;
4ec1fd8e 51#endif /* CONFIG_IEEE80211R_AP || CONFIG_IEEE80211W */
08a74e6a 52 u16 reason = WLAN_REASON_UNSPECIFIED;
88b32a99 53 u16 status = WLAN_STATUS_SUCCESS;
94ddef3e 54 const u8 *p2p_dev_addr = NULL;
b5b969e9 55
68532a9c
JM
56 if (addr == NULL) {
57 /*
58 * This could potentially happen with unexpected event from the
59 * driver wrapper. This was seen at least in one case where the
60 * driver ended up being set to station mode while hostapd was
61 * running, so better make sure we stop processing such an
62 * event here.
63 */
48b06c17
JM
64 wpa_printf(MSG_DEBUG,
65 "hostapd_notif_assoc: Skip event with no address");
68532a9c
JM
66 return -1;
67 }
bbb921da 68 random_add_randomness(addr, ETH_ALEN);
68532a9c 69
b5b969e9
JM
70 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
71 HOSTAPD_LEVEL_INFO, "associated");
72
2bb20281 73 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
c41a1095
JM
74 if (elems.wps_ie) {
75 ie = elems.wps_ie - 2;
76 ielen = elems.wps_ie_len + 2;
77 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
78 } else if (elems.rsn_ie) {
79 ie = elems.rsn_ie - 2;
80 ielen = elems.rsn_ie_len + 2;
81 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
82 } else if (elems.wpa_ie) {
83 ie = elems.wpa_ie - 2;
84 ielen = elems.wpa_ie_len + 2;
85 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
a14896e8
JM
86#ifdef CONFIG_HS20
87 } else if (elems.osen) {
88 ie = elems.osen - 2;
89 ielen = elems.osen_len + 2;
90 wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
91#endif /* CONFIG_HS20 */
c41a1095
JM
92 } else {
93 ie = NULL;
94 ielen = 0;
48b06c17
JM
95 wpa_printf(MSG_DEBUG,
96 "STA did not include WPS/RSN/WPA IE in (Re)AssocReq");
c41a1095
JM
97 }
98
b5b969e9
JM
99 sta = ap_get_sta(hapd, addr);
100 if (sta) {
4331263b 101 ap_sta_no_session_timeout(hapd, sta);
b5b969e9 102 accounting_sta_stop(hapd, sta);
c72bd6d4
JM
103
104 /*
105 * Make sure that the previously registered inactivity timer
106 * will not remove the STA immediately.
107 */
108 sta->timeout_next = STA_NULLFUNC;
b5b969e9
JM
109 } else {
110 sta = ap_sta_add(hapd, addr);
8bd0fc0e
JM
111 if (sta == NULL) {
112 hostapd_drv_sta_disassoc(hapd, addr,
113 WLAN_REASON_DISASSOC_AP_BUSY);
b5b969e9 114 return -1;
8bd0fc0e 115 }
b5b969e9 116 }
17f6b900 117 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
b5b969e9 118
bb4e19e3
SD
119 /*
120 * ACL configurations to the drivers (implementing AP SME and ACL
121 * offload) without hostapd's knowledge, can result in a disconnection
122 * though the driver accepts the connection. Skip the hostapd check for
123 * ACL if the driver supports ACL offload to avoid potentially
124 * conflicting ACL rules.
125 */
126 if (hapd->iface->drv_max_acl_mac_addrs == 0 &&
127 hostapd_check_acl(hapd, addr, NULL) != HOSTAPD_ACL_ACCEPT) {
0603bcb7
AN
128 wpa_printf(MSG_INFO, "STA " MACSTR " not allowed to connect",
129 MAC2STR(addr));
130 reason = WLAN_REASON_UNSPECIFIED;
131 goto fail;
132 }
133
b305c684
JM
134#ifdef CONFIG_P2P
135 if (elems.p2p) {
136 wpabuf_free(sta->p2p_ie);
2bb20281 137 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
b305c684 138 P2P_IE_VENDOR_TYPE);
94ddef3e
JM
139 if (sta->p2p_ie)
140 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
b305c684
JM
141 }
142#endif /* CONFIG_P2P */
143
9c47f6a2
PX
144#ifdef CONFIG_IEEE80211N
145#ifdef NEED_AP_MLME
146 if (elems.ht_capabilities &&
9c47f6a2
PX
147 (hapd->iface->conf->ht_capab &
148 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
149 struct ieee80211_ht_capabilities *ht_cap =
150 (struct ieee80211_ht_capabilities *)
151 elems.ht_capabilities;
152
153 if (le_to_host16(ht_cap->ht_capabilities_info) &
154 HT_CAP_INFO_40MHZ_INTOLERANT)
155 ht40_intolerant_add(hapd->iface, sta);
156 }
157#endif /* NEED_AP_MLME */
158#endif /* CONFIG_IEEE80211N */
159
c551700f
KP
160#ifdef CONFIG_INTERWORKING
161 if (elems.ext_capab && elems.ext_capab_len > 4) {
162 if (elems.ext_capab[4] & 0x01)
163 sta->qos_map_enabled = 1;
164 }
165#endif /* CONFIG_INTERWORKING */
166
f403dcd6
JM
167#ifdef CONFIG_HS20
168 wpabuf_free(sta->hs20_ie);
169 if (elems.hs20 && elems.hs20_len > 4) {
170 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
171 elems.hs20_len - 4);
172 } else
173 sta->hs20_ie = NULL;
174#endif /* CONFIG_HS20 */
175
ae667c08
AN
176#ifdef CONFIG_FST
177 wpabuf_free(sta->mb_ies);
178 if (hapd->iface->fst)
179 sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
180 else
181 sta->mb_ies = NULL;
182#endif /* CONFIG_FST */
183
6332aaf3
JM
184 mbo_ap_check_sta_assoc(hapd, sta, &elems);
185
adf0478e
JM
186 ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
187 elems.supp_op_classes_len);
188
b5b969e9
JM
189 if (hapd->conf->wpa) {
190 if (ie == NULL || ielen == 0) {
633d4469 191#ifdef CONFIG_WPS
b5b969e9 192 if (hapd->conf->wps_state) {
48b06c17
JM
193 wpa_printf(MSG_DEBUG,
194 "STA did not include WPA/RSN IE in (Re)Association Request - possible WPS use");
b5b969e9
JM
195 sta->flags |= WLAN_STA_MAYBE_WPS;
196 goto skip_wpa_check;
197 }
633d4469 198#endif /* CONFIG_WPS */
b5b969e9
JM
199
200 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
201 return -1;
202 }
633d4469 203#ifdef CONFIG_WPS
b5b969e9
JM
204 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
205 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
17f6b900 206 struct wpabuf *wps;
48b06c17 207
b5b969e9 208 sta->flags |= WLAN_STA_WPS;
17f6b900
JM
209 wps = ieee802_11_vendor_ie_concat(ie, ielen,
210 WPS_IE_VENDOR_TYPE);
211 if (wps) {
212 if (wps_is_20(wps)) {
48b06c17
JM
213 wpa_printf(MSG_DEBUG,
214 "WPS: STA supports WPS 2.0");
17f6b900
JM
215 sta->flags |= WLAN_STA_WPS2;
216 }
217 wpabuf_free(wps);
218 }
b5b969e9
JM
219 goto skip_wpa_check;
220 }
633d4469 221#endif /* CONFIG_WPS */
b5b969e9
JM
222
223 if (sta->wpa_sm == NULL)
224 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e
JM
225 sta->addr,
226 p2p_dev_addr);
b5b969e9 227 if (sta->wpa_sm == NULL) {
48b06c17
JM
228 wpa_printf(MSG_ERROR,
229 "Failed to initialize WPA state machine");
b5b969e9
JM
230 return -1;
231 }
232 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
88b32a99 233 ie, ielen,
09368515
JM
234 elems.mdie, elems.mdie_len,
235 elems.owe_dh, elems.owe_dh_len);
b5b969e9 236 if (res != WPA_IE_OK) {
48b06c17
JM
237 wpa_printf(MSG_DEBUG,
238 "WPA/RSN information element rejected? (res %u)",
239 res);
b5b969e9 240 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
88b32a99 241 if (res == WPA_INVALID_GROUP) {
08a74e6a 242 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
243 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
244 } else if (res == WPA_INVALID_PAIRWISE) {
08a74e6a 245 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
88b32a99
SP
246 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
247 } else if (res == WPA_INVALID_AKMP) {
08a74e6a 248 reason = WLAN_REASON_AKMP_NOT_VALID;
88b32a99
SP
249 status = WLAN_STATUS_AKMP_NOT_VALID;
250 }
355d36a7 251#ifdef CONFIG_IEEE80211W
88b32a99 252 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
08a74e6a 253 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
254 status = WLAN_STATUS_INVALID_IE;
255 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
08a74e6a 256 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
88b32a99
SP
257 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
258 }
355d36a7 259#endif /* CONFIG_IEEE80211W */
88b32a99 260 else {
08a74e6a 261 reason = WLAN_REASON_INVALID_IE;
88b32a99
SP
262 status = WLAN_STATUS_INVALID_IE;
263 }
08a74e6a 264 goto fail;
b5b969e9 265 }
7d9c0cd3
MP
266#ifdef CONFIG_IEEE80211W
267 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
268 sta->sa_query_count > 0)
269 ap_check_sa_query_timeout(hapd, sta);
270 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
271 (sta->auth_alg != WLAN_AUTH_FT)) {
272 /*
273 * STA has already been associated with MFP and SA
274 * Query timeout has not been reached. Reject the
275 * association attempt temporarily and start SA Query,
276 * if one is not pending.
277 */
278
279 if (sta->sa_query_count == 0)
280 ap_sta_start_sa_query(hapd, sta);
281
7d9c0cd3
MP
282 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
283
284 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
285
286 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
287 p - buf);
7d9c0cd3
MP
288 return 0;
289 }
290
291 if (wpa_auth_uses_mfp(sta->wpa_sm))
292 sta->flags |= WLAN_STA_MFP;
293 else
294 sta->flags &= ~WLAN_STA_MFP;
295#endif /* CONFIG_IEEE80211W */
296
4ec1fd8e 297#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
298 if (sta->auth_alg == WLAN_AUTH_FT) {
299 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
300 req_ies_len);
301 if (status != WLAN_STATUS_SUCCESS) {
302 if (status == WLAN_STATUS_INVALID_PMKID)
303 reason = WLAN_REASON_INVALID_IE;
304 if (status == WLAN_STATUS_INVALID_MDIE)
305 reason = WLAN_REASON_INVALID_IE;
306 if (status == WLAN_STATUS_INVALID_FTIE)
307 reason = WLAN_REASON_INVALID_IE;
308 goto fail;
309 }
310 }
4ec1fd8e 311#endif /* CONFIG_IEEE80211R_AP */
a9aca28b 312 } else if (hapd->conf->wps_state) {
633d4469 313#ifdef CONFIG_WPS
17f6b900 314 struct wpabuf *wps;
48b06c17 315
2bb20281
JM
316 if (req_ies)
317 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
fa15d405
JM
318 WPS_IE_VENDOR_TYPE);
319 else
320 wps = NULL;
54f489be 321#ifdef CONFIG_WPS_STRICT
fa15d405 322 if (wps && wps_validate_assoc_req(wps) < 0) {
08a74e6a 323 reason = WLAN_REASON_INVALID_IE;
88b32a99 324 status = WLAN_STATUS_INVALID_IE;
fa15d405 325 wpabuf_free(wps);
08a74e6a 326 goto fail;
54f489be 327 }
54f489be 328#endif /* CONFIG_WPS_STRICT */
fa15d405 329 if (wps) {
a9aca28b 330 sta->flags |= WLAN_STA_WPS;
fa15d405 331 if (wps_is_20(wps)) {
48b06c17
JM
332 wpa_printf(MSG_DEBUG,
333 "WPS: STA supports WPS 2.0");
17f6b900
JM
334 sta->flags |= WLAN_STA_WPS2;
335 }
a9aca28b
JM
336 } else
337 sta->flags |= WLAN_STA_MAYBE_WPS;
17f6b900 338 wpabuf_free(wps);
633d4469 339#endif /* CONFIG_WPS */
a14896e8
JM
340#ifdef CONFIG_HS20
341 } else if (hapd->conf->osen) {
342 if (elems.osen == NULL) {
343 hostapd_logger(
344 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
345 HOSTAPD_LEVEL_INFO,
346 "No HS 2.0 OSEN element in association request");
347 return WLAN_STATUS_INVALID_IE;
348 }
349
350 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
351 if (sta->wpa_sm == NULL)
352 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
353 sta->addr, NULL);
354 if (sta->wpa_sm == NULL) {
48b06c17
JM
355 wpa_printf(MSG_WARNING,
356 "Failed to initialize WPA state machine");
a14896e8
JM
357 return WLAN_STATUS_UNSPECIFIED_FAILURE;
358 }
359 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
360 elems.osen - 2, elems.osen_len + 2) < 0)
361 return WLAN_STATUS_INVALID_IE;
362#endif /* CONFIG_HS20 */
b5b969e9 363 }
4c572281
JM
364
365#ifdef CONFIG_MBO
366 if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
367 elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
368 hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
369 wpa_printf(MSG_INFO,
370 "MBO: Reject WPA2 association without PMF");
371 return WLAN_STATUS_UNSPECIFIED_FAILURE;
372 }
373#endif /* CONFIG_MBO */
374
633d4469 375#ifdef CONFIG_WPS
b5b969e9 376skip_wpa_check:
633d4469 377#endif /* CONFIG_WPS */
b5b969e9 378
4ec1fd8e 379#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
380 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
381 sta->auth_alg, req_ies, req_ies_len);
cc20edc9 382#endif /* CONFIG_IEEE80211R_AP */
88b32a99 383
cc20edc9 384#if defined(CONFIG_IEEE80211R_AP) || defined(CONFIG_FILS)
88b32a99 385 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
e4474c1c 386
cc20edc9
JM
387 if (sta->auth_alg == WLAN_AUTH_FT ||
388 sta->auth_alg == WLAN_AUTH_FILS_SK ||
389 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
390 sta->auth_alg == WLAN_AUTH_FILS_PK)
e4474c1c 391 ap_sta_set_authorized(hapd, sta, 1);
cc20edc9 392#else /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
88b32a99
SP
393 /* Keep compiler silent about unused variables */
394 if (status) {
395 }
cc20edc9 396#endif /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
88b32a99 397
b5b969e9
JM
398 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
399 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
3578e665 400 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
88b32a99 401
e4474c1c
DPS
402 hostapd_set_sta_flags(hapd, sta);
403
88b32a99
SP
404 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
405 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
957bff83
JM
406#ifdef CONFIG_FILS
407 else if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
408 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
409 sta->auth_alg == WLAN_AUTH_FILS_PK)
410 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
411#endif /* CONFIG_FILS */
88b32a99
SP
412 else
413 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
b5b969e9
JM
414
415 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
416
417 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
418
ef796391 419#ifdef CONFIG_P2P
99c01af9
JM
420 if (req_ies) {
421 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
422 req_ies, req_ies_len);
423 }
ef796391
JM
424#endif /* CONFIG_P2P */
425
b5b969e9 426 return 0;
08a74e6a
JM
427
428fail:
4ec1fd8e 429#ifdef CONFIG_IEEE80211R_AP
88b32a99 430 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
4ec1fd8e 431#endif /* CONFIG_IEEE80211R_AP */
08a74e6a
JM
432 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
433 ap_free_sta(hapd, sta);
434 return -1;
b5b969e9
JM
435}
436
437
438void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
439{
440 struct sta_info *sta;
441
83e843e8
JM
442 if (addr == NULL) {
443 /*
444 * This could potentially happen with unexpected event from the
445 * driver wrapper. This was seen at least in one case where the
446 * driver ended up reporting a station mode event while hostapd
447 * was running, so better make sure we stop processing such an
448 * event here.
449 */
48b06c17
JM
450 wpa_printf(MSG_DEBUG,
451 "hostapd_notif_disassoc: Skip event with no address");
1f4c7b6b 452 return;
83e843e8
JM
453 }
454
b5b969e9
JM
455 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
456 HOSTAPD_LEVEL_INFO, "disassociated");
457
458 sta = ap_get_sta(hapd, addr);
459 if (sta == NULL) {
48b06c17
JM
460 wpa_printf(MSG_DEBUG,
461 "Disassociation notification for unknown STA "
462 MACSTR, MAC2STR(addr));
b5b969e9
JM
463 return;
464 }
465
ae055af4 466 ap_sta_set_authorized(hapd, sta, 0);
b5b969e9
JM
467 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
468 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
469 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
470 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
471 ap_free_sta(hapd, sta);
472}
473
474
0d7e5a3a
JB
475void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
476{
477 struct sta_info *sta = ap_get_sta(hapd, addr);
478
479 if (!sta || !hapd->conf->disassoc_low_ack)
480 return;
481
482 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
48b06c17
JM
483 HOSTAPD_LEVEL_INFO,
484 "disconnected due to excessive missing ACKs");
0d7e5a3a 485 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
5f99d962 486 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
0d7e5a3a
JB
487}
488
489
1b487b8b 490void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
8d1fdde7 491 int offset, int width, int cf1, int cf2)
1b487b8b 492{
c7803a02 493#ifdef NEED_AP_MLME
d308a44f
LC
494 int channel, chwidth, is_dfs;
495 u8 seg0_idx = 0, seg1_idx = 0;
1b487b8b
TP
496
497 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
2f06bcb3
JM
498 HOSTAPD_LEVEL_INFO,
499 "driver had channel switch: freq=%d, ht=%d, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
500 freq, ht, offset, width, channel_width_to_string(width),
501 cf1, cf2);
1b487b8b
TP
502
503 hapd->iface->freq = freq;
504
505 channel = hostapd_hw_get_channel(hapd, freq);
506 if (!channel) {
507 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
48b06c17
JM
508 HOSTAPD_LEVEL_WARNING,
509 "driver switched to bad channel!");
1b487b8b
TP
510 return;
511 }
512
8d1fdde7
JD
513 switch (width) {
514 case CHAN_WIDTH_80:
515 chwidth = VHT_CHANWIDTH_80MHZ;
516 break;
517 case CHAN_WIDTH_80P80:
518 chwidth = VHT_CHANWIDTH_80P80MHZ;
519 break;
520 case CHAN_WIDTH_160:
521 chwidth = VHT_CHANWIDTH_160MHZ;
522 break;
523 case CHAN_WIDTH_20_NOHT:
524 case CHAN_WIDTH_20:
525 case CHAN_WIDTH_40:
526 default:
527 chwidth = VHT_CHANWIDTH_USE_HT;
528 break;
529 }
530
531 switch (hapd->iface->current_mode->mode) {
532 case HOSTAPD_MODE_IEEE80211A:
533 if (cf1 > 5000)
534 seg0_idx = (cf1 - 5000) / 5;
535 if (cf2 > 5000)
536 seg1_idx = (cf2 - 5000) / 5;
537 break;
538 default:
d308a44f
LC
539 ieee80211_freq_to_chan(cf1, &seg0_idx);
540 ieee80211_freq_to_chan(cf2, &seg1_idx);
8d1fdde7
JD
541 break;
542 }
543
1b487b8b
TP
544 hapd->iconf->channel = channel;
545 hapd->iconf->ieee80211n = ht;
5de74818
JM
546 if (!ht)
547 hapd->iconf->ieee80211ac = 0;
1b487b8b 548 hapd->iconf->secondary_channel = offset;
8d1fdde7
JD
549 hapd->iconf->vht_oper_chwidth = chwidth;
550 hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
551 hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
bf281c12 552
1e2aaffb
AK
553 is_dfs = ieee80211_is_dfs(freq);
554
6782b684
MK
555 if (hapd->csa_in_progress &&
556 freq == hapd->cs_freq_params.freq) {
bf281c12 557 hostapd_cleanup_cs_params(hapd);
6782b684 558 ieee802_11_set_beacon(hapd);
bf281c12 559
1e2aaffb
AK
560 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
561 "freq=%d dfs=%d", freq, is_dfs);
562 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
563 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
564 "freq=%d dfs=%d", freq, is_dfs);
bf281c12 565 }
c7803a02 566#endif /* NEED_AP_MLME */
1b487b8b
TP
567}
568
569
3140803b
RM
570void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
571 const u8 *addr, int reason_code)
572{
573 switch (reason_code) {
574 case MAX_CLIENT_REACHED:
575 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
576 MAC2STR(addr));
577 break;
578 case BLOCKED_CLIENT:
579 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
580 MAC2STR(addr));
581 break;
582 }
583}
584
585
16689c7c 586#ifdef CONFIG_ACS
d9909717
TB
587void hostapd_acs_channel_selected(struct hostapd_data *hapd,
588 struct acs_selected_channels *acs_res)
16689c7c 589{
3784c058 590 int ret, i;
e1d00d47 591 int err = 0;
16689c7c
PX
592
593 if (hapd->iconf->channel) {
594 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
595 hapd->iconf->channel);
596 return;
597 }
598
3784c058
PX
599 if (!hapd->iface->current_mode) {
600 for (i = 0; i < hapd->iface->num_hw_features; i++) {
601 struct hostapd_hw_modes *mode =
602 &hapd->iface->hw_features[i];
603
604 if (mode->mode == acs_res->hw_mode) {
605 hapd->iface->current_mode = mode;
606 break;
607 }
608 }
609 if (!hapd->iface->current_mode) {
610 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
611 HOSTAPD_LEVEL_WARNING,
612 "driver selected to bad hw_mode");
e1d00d47
PX
613 err = 1;
614 goto out;
3784c058
PX
615 }
616 }
617
857d9422 618 hapd->iface->freq = hostapd_hw_get_freq(hapd, acs_res->pri_channel);
16689c7c 619
857d9422 620 if (!acs_res->pri_channel) {
16689c7c
PX
621 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
622 HOSTAPD_LEVEL_WARNING,
623 "driver switched to bad channel");
e1d00d47
PX
624 err = 1;
625 goto out;
16689c7c
PX
626 }
627
857d9422
MM
628 hapd->iconf->channel = acs_res->pri_channel;
629 hapd->iconf->acs = 1;
16689c7c 630
857d9422 631 if (acs_res->sec_channel == 0)
16689c7c 632 hapd->iconf->secondary_channel = 0;
857d9422 633 else if (acs_res->sec_channel < acs_res->pri_channel)
16689c7c 634 hapd->iconf->secondary_channel = -1;
857d9422 635 else if (acs_res->sec_channel > acs_res->pri_channel)
16689c7c
PX
636 hapd->iconf->secondary_channel = 1;
637 else {
638 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
e1d00d47
PX
639 err = 1;
640 goto out;
16689c7c
PX
641 }
642
857d9422
MM
643 if (hapd->iface->conf->ieee80211ac) {
644 /* set defaults for backwards compatibility */
645 hapd->iconf->vht_oper_centr_freq_seg1_idx = 0;
646 hapd->iconf->vht_oper_centr_freq_seg0_idx = 0;
647 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
648 if (acs_res->ch_width == 80) {
649 hapd->iconf->vht_oper_centr_freq_seg0_idx =
650 acs_res->vht_seg0_center_ch;
651 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
652 } else if (acs_res->ch_width == 160) {
653 if (acs_res->vht_seg1_center_ch == 0) {
654 hapd->iconf->vht_oper_centr_freq_seg0_idx =
655 acs_res->vht_seg0_center_ch;
656 hapd->iconf->vht_oper_chwidth =
657 VHT_CHANWIDTH_160MHZ;
658 } else {
659 hapd->iconf->vht_oper_centr_freq_seg0_idx =
660 acs_res->vht_seg0_center_ch;
661 hapd->iconf->vht_oper_centr_freq_seg1_idx =
662 acs_res->vht_seg1_center_ch;
663 hapd->iconf->vht_oper_chwidth =
664 VHT_CHANWIDTH_80P80MHZ;
665 }
666 }
667 }
668
e1d00d47
PX
669out:
670 ret = hostapd_acs_completed(hapd->iface, err);
16689c7c
PX
671 if (ret) {
672 wpa_printf(MSG_ERROR,
673 "ACS: Possibly channel configuration is invalid");
674 }
675}
676#endif /* CONFIG_ACS */
677
678
04a85e44 679int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
baf513d6
JB
680 const u8 *bssid, const u8 *ie, size_t ie_len,
681 int ssi_signal)
e67b55fb
JM
682{
683 size_t i;
684 int ret = 0;
685
b211f3eb
JM
686 if (sa == NULL || ie == NULL)
687 return -1;
688
689 random_add_randomness(sa, ETH_ALEN);
e67b55fb
JM
690 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
691 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
baf513d6
JB
692 sa, da, bssid, ie, ie_len,
693 ssi_signal) > 0) {
e67b55fb
JM
694 ret = 1;
695 break;
696 }
697 }
698 return ret;
699}
700
701
5c61a282
JM
702#ifdef HOSTAPD
703
4ec1fd8e 704#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
705static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
706 const u8 *bssid,
707 u16 auth_transaction, u16 status,
708 const u8 *ies, size_t ies_len)
709{
710 struct hostapd_data *hapd = ctx;
711 struct sta_info *sta;
712
713 sta = ap_get_sta(hapd, dst);
714 if (sta == NULL)
715 return;
716
717 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
718 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
719 sta->flags |= WLAN_STA_AUTH;
720
721 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
722}
4ec1fd8e 723#endif /* CONFIG_IEEE80211R_AP */
88b32a99
SP
724
725
88b32a99
SP
726static void hostapd_notif_auth(struct hostapd_data *hapd,
727 struct auth_info *rx_auth)
728{
729 struct sta_info *sta;
730 u16 status = WLAN_STATUS_SUCCESS;
731 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
732 size_t resp_ies_len = 0;
733
734 sta = ap_get_sta(hapd, rx_auth->peer);
735 if (!sta) {
736 sta = ap_sta_add(hapd, rx_auth->peer);
737 if (sta == NULL) {
728d9717 738 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
88b32a99
SP
739 goto fail;
740 }
741 }
742 sta->flags &= ~WLAN_STA_PREAUTH;
743 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
4ec1fd8e 744#ifdef CONFIG_IEEE80211R_AP
88b32a99
SP
745 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
746 sta->auth_alg = WLAN_AUTH_FT;
747 if (sta->wpa_sm == NULL)
748 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
94ddef3e 749 sta->addr, NULL);
88b32a99 750 if (sta->wpa_sm == NULL) {
48b06c17
JM
751 wpa_printf(MSG_DEBUG,
752 "FT: Failed to initialize WPA state machine");
88b32a99
SP
753 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
754 goto fail;
755 }
756 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
757 rx_auth->auth_transaction, rx_auth->ies,
758 rx_auth->ies_len,
759 hostapd_notify_auth_ft_finish, hapd);
760 return;
761 }
4ec1fd8e 762#endif /* CONFIG_IEEE80211R_AP */
88b32a99
SP
763fail:
764 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
765 status, resp_ies, resp_ies_len);
766}
767
768
769static void hostapd_action_rx(struct hostapd_data *hapd,
dbfb8e82 770 struct rx_mgmt *drv_mgmt)
88b32a99 771{
dbfb8e82 772 struct ieee80211_mgmt *mgmt;
88b32a99 773 struct sta_info *sta;
dbfb8e82
JM
774 size_t plen __maybe_unused;
775 u16 fc;
776
777 if (drv_mgmt->frame_len < 24 + 1)
778 return;
779
780 plen = drv_mgmt->frame_len - 24 - 1;
781
782 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
783 fc = le_to_host16(mgmt->frame_control);
784 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
785 return; /* handled by the driver */
88b32a99 786
48b06c17 787 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
dbfb8e82 788 mgmt->u.action.category, (int) plen);
7d9c0cd3 789
dbfb8e82 790 sta = ap_get_sta(hapd, mgmt->sa);
88b32a99
SP
791 if (sta == NULL) {
792 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
793 return;
794 }
4ec1fd8e 795#ifdef CONFIG_IEEE80211R_AP
dbfb8e82
JM
796 if (mgmt->u.action.category == WLAN_ACTION_FT) {
797 const u8 *payload = drv_mgmt->frame + 24 + 1;
48b06c17 798
dbfb8e82 799 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
88b32a99 800 }
4ec1fd8e 801#endif /* CONFIG_IEEE80211R_AP */
7d9c0cd3 802#ifdef CONFIG_IEEE80211W
dbfb8e82
JM
803 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
804 ieee802_11_sa_query_action(
805 hapd, mgmt->sa,
806 mgmt->u.action.u.sa_query_resp.action,
807 mgmt->u.action.u.sa_query_resp.trans_id);
7d9c0cd3
MP
808 }
809#endif /* CONFIG_IEEE80211W */
ad3872a3 810#ifdef CONFIG_WNM
dbfb8e82
JM
811 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
812 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
d32d94db 813 }
ad3872a3 814#endif /* CONFIG_WNM */
037378ff
AN
815#ifdef CONFIG_FST
816 if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
817 fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
818 return;
819 }
820#endif /* CONFIG_FST */
821
88b32a99
SP
822}
823
824
fe6bdb77 825#ifdef NEED_AP_MLME
f8b1f695 826
f8b1f695
JM
827#define HAPD_BROADCAST ((struct hostapd_data *) -1)
828
829static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
830 const u8 *bssid)
831{
832 size_t i;
833
834 if (bssid == NULL)
835 return NULL;
836 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
837 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
838 return HAPD_BROADCAST;
839
840 for (i = 0; i < iface->num_bss; i++) {
841 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
842 return iface->bss[i];
843 }
844
845 return NULL;
846}
847
848
849static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
9b90955e
JB
850 const u8 *bssid, const u8 *addr,
851 int wds)
f8b1f695 852{
9b90955e 853 hapd = get_hapd_bssid(hapd->iface, bssid);
f8b1f695
JM
854 if (hapd == NULL || hapd == HAPD_BROADCAST)
855 return;
856
9b90955e 857 ieee802_11_rx_from_unknown(hapd, addr, wds);
f8b1f695
JM
858}
859
860
912b34f0 861static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
b5b969e9 862{
4b9841d3 863 struct hostapd_iface *iface = hapd->iface;
b57e086c 864 const struct ieee80211_hdr *hdr;
4b9841d3 865 const u8 *bssid;
2a8b7416 866 struct hostapd_frame_info fi;
912b34f0 867 int ret;
4b9841d3 868
5f7e1c06
JM
869#ifdef CONFIG_TESTING_OPTIONS
870 if (hapd->ext_mgmt_frame_handling) {
871 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
872 char *hex = os_malloc(hex_len);
48b06c17 873
5f7e1c06
JM
874 if (hex) {
875 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
876 rx_mgmt->frame_len);
877 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
878 os_free(hex);
879 }
880 return 1;
881 }
882#endif /* CONFIG_TESTING_OPTIONS */
883
2a8b7416
JM
884 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
885 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
4b9841d3 886 if (bssid == NULL)
912b34f0 887 return 0;
4b9841d3
JM
888
889 hapd = get_hapd_bssid(iface, bssid);
890 if (hapd == NULL) {
48b06c17 891 u16 fc = le_to_host16(hdr->frame_control);
4b9841d3
JM
892
893 /*
894 * Drop frames to unknown BSSIDs except for Beacon frames which
895 * could be used to update neighbor information.
896 */
897 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
898 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
899 hapd = iface->bss[0];
900 else
912b34f0 901 return 0;
4b9841d3
JM
902 }
903
2a8b7416
JM
904 os_memset(&fi, 0, sizeof(fi));
905 fi.datarate = rx_mgmt->datarate;
906 fi.ssi_signal = rx_mgmt->ssi_signal;
907
4b9841d3
JM
908 if (hapd == HAPD_BROADCAST) {
909 size_t i;
48b06c17 910
912b34f0
JM
911 ret = 0;
912 for (i = 0; i < iface->num_bss; i++) {
1d91f504
SW
913 /* if bss is set, driver will call this function for
914 * each bss individually. */
915 if (rx_mgmt->drv_priv &&
916 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
917 continue;
918
912b34f0
JM
919 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
920 rx_mgmt->frame_len, &fi) > 0)
921 ret = 1;
922 }
4b9841d3 923 } else
912b34f0
JM
924 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
925 &fi);
bbb921da
JM
926
927 random_add_randomness(&fi, sizeof(fi));
912b34f0
JM
928
929 return ret;
b5b969e9
JM
930}
931
932
f8b1f695
JM
933static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
934 size_t len, u16 stype, int ok)
b5b969e9 935{
4b9841d3 936 struct ieee80211_hdr *hdr;
6996ff7b 937 struct hostapd_data *orig_hapd = hapd;
48b06c17 938
4b9841d3
JM
939 hdr = (struct ieee80211_hdr *) buf;
940 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
6996ff7b 941 if (!hapd)
4b9841d3 942 return;
6996ff7b
JM
943 if (hapd == HAPD_BROADCAST) {
944 if (stype != WLAN_FC_STYPE_ACTION || len <= 25 ||
945 buf[24] != WLAN_ACTION_PUBLIC)
946 return;
947 hapd = get_hapd_bssid(orig_hapd->iface, hdr->addr2);
948 if (!hapd || hapd == HAPD_BROADCAST)
949 return;
950 /*
951 * Allow processing of TX status for a Public Action frame that
952 * used wildcard BBSID.
953 */
954 }
b5b969e9
JM
955 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
956}
f82ef4d8 957
f8b1f695 958#endif /* NEED_AP_MLME */
ad1e68e6
JM
959
960
a8e0505b
JM
961static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
962{
963 struct sta_info *sta = ap_get_sta(hapd, addr);
48b06c17 964
a8e0505b
JM
965 if (sta)
966 return 0;
967
968 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
969 " - adding a new STA", MAC2STR(addr));
970 sta = ap_sta_add(hapd, addr);
971 if (sta) {
972 hostapd_new_assoc_sta(hapd, sta, 0);
973 } else {
974 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
975 MAC2STR(addr));
976 return -1;
977 }
978
979 return 0;
980}
981
982
983static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
984 const u8 *data, size_t data_len)
985{
986 struct hostapd_iface *iface = hapd->iface;
f826635c 987 struct sta_info *sta;
a8e0505b
JM
988 size_t j;
989
990 for (j = 0; j < iface->num_bss; j++) {
48b06c17
JM
991 sta = ap_get_sta(iface->bss[j], src);
992 if (sta && sta->flags & WLAN_STA_ASSOC) {
993 hapd = iface->bss[j];
994 break;
a8e0505b
JM
995 }
996 }
997
998 ieee802_1x_receive(hapd, src, data, data_len);
999}
1000
96bc5086
TB
1001#endif /* HOSTAPD */
1002
a8e0505b 1003
0185007c
MK
1004static struct hostapd_channel_data * hostapd_get_mode_channel(
1005 struct hostapd_iface *iface, unsigned int freq)
1006{
1007 int i;
1008 struct hostapd_channel_data *chan;
1009
1010 for (i = 0; i < iface->current_mode->num_channels; i++) {
1011 chan = &iface->current_mode->channels[i];
0185007c
MK
1012 if ((unsigned int) chan->freq == freq)
1013 return chan;
1014 }
1015
1016 return NULL;
1017}
1018
1019
1020static void hostapd_update_nf(struct hostapd_iface *iface,
1021 struct hostapd_channel_data *chan,
1022 struct freq_survey *survey)
1023{
1024 if (!iface->chans_surveyed) {
1025 chan->min_nf = survey->nf;
1026 iface->lowest_nf = survey->nf;
1027 } else {
1028 if (dl_list_empty(&chan->survey_list))
1029 chan->min_nf = survey->nf;
1030 else if (survey->nf < chan->min_nf)
1031 chan->min_nf = survey->nf;
1032 if (survey->nf < iface->lowest_nf)
1033 iface->lowest_nf = survey->nf;
1034 }
1035}
1036
1037
ec8f36af
KP
1038static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
1039 struct survey_results *survey_res)
1040{
1041 struct hostapd_channel_data *chan;
1042 struct freq_survey *survey;
1043 u64 divisor, dividend;
1044
1045 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
1046 list);
1047 if (!survey || !survey->freq)
1048 return;
1049
1050 chan = hostapd_get_mode_channel(iface, survey->freq);
1051 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
1052 return;
1053
48b06c17
JM
1054 wpa_printf(MSG_DEBUG,
1055 "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
ec8f36af
KP
1056 survey->freq,
1057 (unsigned long int) survey->channel_time,
1058 (unsigned long int) survey->channel_time_busy);
1059
1060 if (survey->channel_time > iface->last_channel_time &&
1061 survey->channel_time > survey->channel_time_busy) {
1062 dividend = survey->channel_time_busy -
1063 iface->last_channel_time_busy;
1064 divisor = survey->channel_time - iface->last_channel_time;
1065
1066 iface->channel_utilization = dividend * 255 / divisor;
1067 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
1068 iface->channel_utilization);
1069 }
1070 iface->last_channel_time = survey->channel_time;
1071 iface->last_channel_time_busy = survey->channel_time_busy;
1072}
1073
1074
96bc5086
TB
1075void hostapd_event_get_survey(struct hostapd_iface *iface,
1076 struct survey_results *survey_results)
0185007c 1077{
0185007c
MK
1078 struct freq_survey *survey, *tmp;
1079 struct hostapd_channel_data *chan;
1080
1081 if (dl_list_empty(&survey_results->survey_list)) {
1082 wpa_printf(MSG_DEBUG, "No survey data received");
1083 return;
1084 }
1085
ec8f36af
KP
1086 if (survey_results->freq_filter) {
1087 hostapd_single_channel_get_survey(iface, survey_results);
1088 return;
1089 }
1090
0185007c
MK
1091 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1092 struct freq_survey, list) {
1093 chan = hostapd_get_mode_channel(iface, survey->freq);
1094 if (!chan)
1095 continue;
1096 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1097 continue;
1098
1099 dl_list_del(&survey->list);
1100 dl_list_add_tail(&chan->survey_list, &survey->list);
1101
1102 hostapd_update_nf(iface, chan, survey);
1103
1104 iface->chans_surveyed++;
1105 }
1106}
1107
1108
96bc5086 1109#ifdef HOSTAPD
e76da505
JD
1110#ifdef NEED_AP_MLME
1111
5841958f
MK
1112static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1113{
1114 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1115 hapd->conf->iface);
1116
1117 if (hapd->csa_in_progress) {
1118 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1119 hapd->conf->iface);
1120 hostapd_switch_channel_fallback(hapd->iface,
1121 &hapd->cs_freq_params);
1122 }
1123}
1124
1125
e76da505
JD
1126static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1127 struct dfs_event *radar)
1128{
e76da505 1129 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
dc036d9e 1130 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
1131 radar->chan_offset, radar->chan_width,
1132 radar->cf1, radar->cf2);
e76da505
JD
1133}
1134
1135
7cbb5f1a
VT
1136static void hostapd_event_dfs_pre_cac_expired(struct hostapd_data *hapd,
1137 struct dfs_event *radar)
1138{
1139 wpa_printf(MSG_DEBUG, "DFS Pre-CAC expired on %d MHz", radar->freq);
1140 hostapd_dfs_pre_cac_expired(hapd->iface, radar->freq, radar->ht_enabled,
1141 radar->chan_offset, radar->chan_width,
1142 radar->cf1, radar->cf2);
1143}
1144
1145
e76da505
JD
1146static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1147 struct dfs_event *radar)
1148{
1149 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
dc036d9e 1150 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
58b73e3d
JD
1151 radar->chan_offset, radar->chan_width,
1152 radar->cf1, radar->cf2);
e76da505
JD
1153}
1154
1155
1156static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1157 struct dfs_event *radar)
1158{
1159 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
dc036d9e 1160 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
58b73e3d
JD
1161 radar->chan_offset, radar->chan_width,
1162 radar->cf1, radar->cf2);
e76da505
JD
1163}
1164
1165
1166static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1167 struct dfs_event *radar)
1168{
1169 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
dc036d9e 1170 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
58b73e3d
JD
1171 radar->chan_offset, radar->chan_width,
1172 radar->cf1, radar->cf2);
e76da505
JD
1173}
1174
c13578c3
AK
1175
1176static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1177 struct dfs_event *radar)
1178{
1179 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1180 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1181 radar->chan_offset, radar->chan_width,
1182 radar->cf1, radar->cf2);
1183}
1184
e76da505
JD
1185#endif /* NEED_AP_MLME */
1186
1187
9646a8ab 1188void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
ad1e68e6
JM
1189 union wpa_event_data *data)
1190{
1191 struct hostapd_data *hapd = ctx;
74781dfc
JM
1192#ifndef CONFIG_NO_STDOUT_DEBUG
1193 int level = MSG_DEBUG;
ad1e68e6 1194
34caf71a 1195 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
74781dfc
JM
1196 data->rx_mgmt.frame_len >= 24) {
1197 const struct ieee80211_hdr *hdr;
1198 u16 fc;
48b06c17 1199
74781dfc
JM
1200 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1201 fc = le_to_host16(hdr->frame_control);
1202 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1203 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1204 level = MSG_EXCESSIVE;
cc2ada86
JM
1205 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1206 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1207 level = MSG_EXCESSIVE;
74781dfc
JM
1208 }
1209
1210 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
e26cd1a1 1211 event_to_string(event), event);
74781dfc 1212#endif /* CONFIG_NO_STDOUT_DEBUG */
e26cd1a1 1213
ad1e68e6
JM
1214 switch (event) {
1215 case EVENT_MICHAEL_MIC_FAILURE:
1216 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1217 break;
1218 case EVENT_SCAN_RESULTS:
1219 if (hapd->iface->scan_cb)
1220 hapd->iface->scan_cb(hapd->iface);
1221 break;
fcf0f87d 1222 case EVENT_WPS_BUTTON_PUSHED:
d601247c 1223 hostapd_wps_button_pushed(hapd, NULL);
fcf0f87d 1224 break;
f8b1f695
JM
1225#ifdef NEED_AP_MLME
1226 case EVENT_TX_STATUS:
1227 switch (data->tx_status.type) {
1228 case WLAN_FC_TYPE_MGMT:
1229 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1230 data->tx_status.data_len,
1231 data->tx_status.stype,
1232 data->tx_status.ack);
1233 break;
1234 case WLAN_FC_TYPE_DATA:
1235 hostapd_tx_status(hapd, data->tx_status.dst,
1236 data->tx_status.data,
1237 data->tx_status.data_len,
1238 data->tx_status.ack);
1239 break;
1240 }
1241 break;
dd840f79
JB
1242 case EVENT_EAPOL_TX_STATUS:
1243 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1244 data->eapol_tx_status.data,
1245 data->eapol_tx_status.data_len,
1246 data->eapol_tx_status.ack);
1247 break;
bcf24348
JB
1248 case EVENT_DRIVER_CLIENT_POLL_OK:
1249 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1250 break;
f8b1f695 1251 case EVENT_RX_FROM_UNKNOWN:
9b90955e
JB
1252 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1253 data->rx_from_unknown.addr,
1254 data->rx_from_unknown.wds);
f8b1f695 1255 break;
dbfb8e82 1256#endif /* NEED_AP_MLME */
f8b1f695 1257 case EVENT_RX_MGMT:
c0333c8d
JM
1258 if (!data->rx_mgmt.frame)
1259 break;
dbfb8e82
JM
1260#ifdef NEED_AP_MLME
1261 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1262 break;
f8b1f695 1263#endif /* NEED_AP_MLME */
dbfb8e82
JM
1264 hostapd_action_rx(hapd, &data->rx_mgmt);
1265 break;
a0e0d3bb 1266 case EVENT_RX_PROBE_REQ:
b211f3eb
JM
1267 if (data->rx_probe_req.sa == NULL ||
1268 data->rx_probe_req.ie == NULL)
1269 break;
a0e0d3bb 1270 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
04a85e44
JM
1271 data->rx_probe_req.da,
1272 data->rx_probe_req.bssid,
a0e0d3bb 1273 data->rx_probe_req.ie,
baf513d6
JB
1274 data->rx_probe_req.ie_len,
1275 data->rx_probe_req.ssi_signal);
a0e0d3bb 1276 break;
a70a5d6d 1277 case EVENT_NEW_STA:
a8e0505b
JM
1278 hostapd_event_new_sta(hapd, data->new_sta.addr);
1279 break;
1280 case EVENT_EAPOL_RX:
1281 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1282 data->eapol_rx.data,
1283 data->eapol_rx.data_len);
1284 break;
1d041bec 1285 case EVENT_ASSOC:
04a258e7
JM
1286 if (!data)
1287 return;
1d041bec
JM
1288 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1289 data->assoc_info.req_ies,
39b08b5f
SP
1290 data->assoc_info.req_ies_len,
1291 data->assoc_info.reassoc);
1d041bec
JM
1292 break;
1293 case EVENT_DISASSOC:
1294 if (data)
1295 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1296 break;
1297 case EVENT_DEAUTH:
1298 if (data)
1299 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1300 break;
0d7e5a3a
JB
1301 case EVENT_STATION_LOW_ACK:
1302 if (!data)
1303 break;
1304 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1305 break;
88b32a99
SP
1306 case EVENT_AUTH:
1307 hostapd_notif_auth(hapd, &data->auth);
1308 break;
1b487b8b
TP
1309 case EVENT_CH_SWITCH:
1310 if (!data)
1311 break;
1312 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1313 data->ch_switch.ht_enabled,
8d1fdde7
JD
1314 data->ch_switch.ch_offset,
1315 data->ch_switch.ch_width,
1316 data->ch_switch.cf1,
1317 data->ch_switch.cf2);
1b487b8b 1318 break;
3140803b
RM
1319 case EVENT_CONNECT_FAILED_REASON:
1320 if (!data)
1321 break;
1322 hostapd_event_connect_failed_reason(
1323 hapd, data->connect_failed_reason.addr,
1324 data->connect_failed_reason.code);
1325 break;
0185007c 1326 case EVENT_SURVEY:
96bc5086 1327 hostapd_event_get_survey(hapd->iface, &data->survey_results);
0185007c 1328 break;
e76da505 1329#ifdef NEED_AP_MLME
5841958f
MK
1330 case EVENT_INTERFACE_UNAVAILABLE:
1331 hostapd_event_iface_unavailable(hapd);
1332 break;
e76da505
JD
1333 case EVENT_DFS_RADAR_DETECTED:
1334 if (!data)
1335 break;
1336 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1337 break;
7cbb5f1a
VT
1338 case EVENT_DFS_PRE_CAC_EXPIRED:
1339 if (!data)
1340 break;
1341 hostapd_event_dfs_pre_cac_expired(hapd, &data->dfs_event);
1342 break;
e76da505
JD
1343 case EVENT_DFS_CAC_FINISHED:
1344 if (!data)
1345 break;
1346 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1347 break;
1348 case EVENT_DFS_CAC_ABORTED:
1349 if (!data)
1350 break;
1351 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1352 break;
1353 case EVENT_DFS_NOP_FINISHED:
1354 if (!data)
1355 break;
1356 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1357 break;
1358 case EVENT_CHANNEL_LIST_CHANGED:
1359 /* channel list changed (regulatory?), update channel list */
1360 /* TODO: check this. hostapd_get_hw_features() initializes
1361 * too much stuff. */
1362 /* hostapd_get_hw_features(hapd->iface); */
795baf77
AS
1363 hostapd_channel_list_updated(
1364 hapd->iface, data->channel_list_changed.initiator);
e76da505 1365 break;
c13578c3
AK
1366 case EVENT_DFS_CAC_STARTED:
1367 if (!data)
1368 break;
1369 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1370 break;
e76da505 1371#endif /* NEED_AP_MLME */
ab93fdeb
JM
1372 case EVENT_INTERFACE_ENABLED:
1373 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
f33c8606
JM
1374 if (hapd->disabled && hapd->started) {
1375 hapd->disabled = 0;
1376 /*
1377 * Try to re-enable interface if the driver stopped it
1378 * when the interface got disabled.
1379 */
34782730 1380 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
f33c8606
JM
1381 hapd->reenable_beacon = 1;
1382 ieee802_11_set_beacon(hapd);
1383 }
ab93fdeb
JM
1384 break;
1385 case EVENT_INTERFACE_DISABLED:
c165cb40 1386 hostapd_free_stas(hapd);
ab93fdeb 1387 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
f33c8606 1388 hapd->disabled = 1;
ab93fdeb 1389 break;
16689c7c
PX
1390#ifdef CONFIG_ACS
1391 case EVENT_ACS_CHANNEL_SELECTED:
857d9422
MM
1392 hostapd_acs_channel_selected(hapd,
1393 &data->acs_selected_channels);
16689c7c
PX
1394 break;
1395#endif /* CONFIG_ACS */
ad1e68e6
JM
1396 default:
1397 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1398 break;
1399 }
1400}
f8b1f695 1401
45e3fc72
RM
1402
1403void wpa_supplicant_event_global(void *ctx, enum wpa_event_type event,
1404 union wpa_event_data *data)
1405{
1406 struct hapd_interfaces *interfaces = ctx;
1407 struct hostapd_data *hapd;
1408
1409 if (event != EVENT_INTERFACE_STATUS)
1410 return;
1411
1412 hapd = hostapd_get_iface(interfaces, data->interface_status.ifname);
1413 if (hapd && hapd->driver && hapd->driver->get_ifindex &&
1414 hapd->drv_priv) {
1415 unsigned int ifindex;
1416
1417 ifindex = hapd->driver->get_ifindex(hapd->drv_priv);
1418 if (ifindex != data->interface_status.ifindex) {
1419 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1420 "interface status ifindex %d mismatch (%d)",
1421 ifindex, data->interface_status.ifindex);
1422 return;
1423 }
1424 }
1425 if (hapd)
1426 wpa_supplicant_event(hapd, event, data);
1427}
1428
f8b1f695 1429#endif /* HOSTAPD */