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