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