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 "common/dpp.h"
19 #include "common/sae.h"
20 #include "common/hw_features_common.h"
21 #include "crypto/random.h"
22 #include "p2p/p2p.h"
23 #include "wps/wps.h"
24 #include "fst/fst.h"
25 #include "wnm_ap.h"
26 #include "hostapd.h"
27 #include "ieee802_11.h"
28 #include "ieee802_11_auth.h"
29 #include "sta_info.h"
30 #include "accounting.h"
31 #include "tkip_countermeasures.h"
32 #include "ieee802_1x.h"
33 #include "wpa_auth.h"
34 #include "wps_hostapd.h"
35 #include "ap_drv_ops.h"
36 #include "ap_config.h"
37 #include "ap_mlme.h"
38 #include "hw_features.h"
39 #include "dfs.h"
40 #include "beacon.h"
41 #include "mbo_ap.h"
42 #include "dpp_hostapd.h"
43 #include "fils_hlp.h"
44 #include "neighbor_db.h"
45 #ifdef CONFIG_VENDOR_EXT
46 #include "vendor_ext.h"
47 #endif
48 #ifdef CONFIG_LIBWPA_VENDOR
49 #include "wpa_client.h"
50 #include "wpa_supplicant_i.h"
51 #include "hostapd_client.h"
52 #endif
53 
54 #ifdef OPEN_HARMONY_MIRACAST_SINK_OPT
55 #include "hm_miracast_sink.h"
56 #endif
57 
58 #ifdef CONFIG_FILS
hostapd_notify_assoc_fils_finish(struct hostapd_data *hapd, struct sta_info *sta)59 void hostapd_notify_assoc_fils_finish(struct hostapd_data *hapd,
60 				      struct sta_info *sta)
61 {
62 	u16 reply_res = WLAN_STATUS_SUCCESS;
63 	struct ieee802_11_elems elems;
64 	u8 buf[IEEE80211_MAX_MMPDU_SIZE], *p = buf;
65 	int new_assoc;
66 
67 	wpa_printf(MSG_DEBUG, "%s FILS: Finish association with " MACSTR_SEC,
68 		   __func__, MAC2STR_SEC(sta->addr));
69 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
70 	if (!sta->fils_pending_assoc_req)
71 		return;
72 
73 	ieee802_11_parse_elems(sta->fils_pending_assoc_req,
74 			       sta->fils_pending_assoc_req_len, &elems, 0);
75 	if (!elems.fils_session) {
76 		wpa_printf(MSG_DEBUG, "%s failed to find FILS Session element",
77 			   __func__);
78 		return;
79 	}
80 
81 	p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
82 					   elems.fils_session,
83 					   sta->fils_hlp_resp);
84 
85 	reply_res = hostapd_sta_assoc(hapd, sta->addr,
86 				      sta->fils_pending_assoc_is_reassoc,
87 				      WLAN_STATUS_SUCCESS,
88 				      buf, p - buf);
89 	ap_sta_set_authorized(hapd, sta, 1);
90 	new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
91 	sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
92 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
93 	hostapd_set_sta_flags(hapd, sta);
94 	wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
95 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
96 	hostapd_new_assoc_sta(hapd, sta, !new_assoc);
97 	os_free(sta->fils_pending_assoc_req);
98 	sta->fils_pending_assoc_req = NULL;
99 	sta->fils_pending_assoc_req_len = 0;
100 	wpabuf_free(sta->fils_hlp_resp);
101 	sta->fils_hlp_resp = NULL;
102 	wpabuf_free(sta->hlp_dhcp_discover);
103 	sta->hlp_dhcp_discover = NULL;
104 	fils_hlp_deinit(hapd);
105 
106 	/*
107 	 * Remove the station in case transmission of a success response fails
108 	 * (the STA was added associated to the driver) or if the station was
109 	 * previously added unassociated.
110 	 */
111 	if (reply_res != WLAN_STATUS_SUCCESS || sta->added_unassoc) {
112 		hostapd_drv_sta_remove(hapd, sta->addr);
113 		sta->added_unassoc = 0;
114 	}
115 }
116 #endif /* CONFIG_FILS */
117 
118 
check_sa_query_need(struct hostapd_data *hapd, struct sta_info *sta)119 static bool check_sa_query_need(struct hostapd_data *hapd, struct sta_info *sta)
120 {
121 	if ((sta->flags &
122 	     (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED)) !=
123 	    (WLAN_STA_ASSOC | WLAN_STA_MFP | WLAN_STA_AUTHORIZED))
124 		return false;
125 
126 	if (!sta->sa_query_timed_out && sta->sa_query_count > 0)
127 		ap_check_sa_query_timeout(hapd, sta);
128 
129 	if (!sta->sa_query_timed_out && (sta->auth_alg != WLAN_AUTH_FT)) {
130 		/*
131 		 * STA has already been associated with MFP and SA Query timeout
132 		 * has not been reached. Reject the association attempt
133 		 * temporarily and start SA Query, if one is not pending.
134 		 */
135 		if (sta->sa_query_count == 0)
136 			ap_sta_start_sa_query(hapd, sta);
137 
138 		return true;
139 	}
140 
141 	return false;
142 }
143 
144 
hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr, const u8 *req_ies, size_t req_ies_len, int reassoc)145 int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
146 			const u8 *req_ies, size_t req_ies_len, int reassoc)
147 {
148 	struct sta_info *sta;
149 	int new_assoc;
150 	enum wpa_validate_result res;
151 	struct ieee802_11_elems elems;
152 	const u8 *ie;
153 	size_t ielen;
154 	u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
155 	u8 *p = buf;
156 	u16 reason = WLAN_REASON_UNSPECIFIED;
157 	int status = WLAN_STATUS_SUCCESS;
158 	const u8 *p2p_dev_addr = NULL;
159 
160 	if (addr == NULL) {
161 		/*
162 		 * This could potentially happen with unexpected event from the
163 		 * driver wrapper. This was seen at least in one case where the
164 		 * driver ended up being set to station mode while hostapd was
165 		 * running, so better make sure we stop processing such an
166 		 * event here.
167 		 */
168 		wpa_printf(MSG_DEBUG,
169 			   "hostapd_notif_assoc: Skip event with no address");
170 		return -1;
171 	}
172 
173 	if (is_multicast_ether_addr(addr) ||
174 	    is_zero_ether_addr(addr) ||
175 	    os_memcmp(addr, hapd->own_addr, ETH_ALEN) == 0) {
176 		/* Do not process any frames with unexpected/invalid SA so that
177 		 * we do not add any state for unexpected STA addresses or end
178 		 * up sending out frames to unexpected destination. */
179 		wpa_printf(MSG_DEBUG, "%s: Invalid SA=" MACSTR_SEC
180 			   " in received indication - ignore this indication silently",
181 			   __func__, MAC2STR_SEC(addr));
182 		return 0;
183 	}
184 
185 	random_add_randomness(addr, ETH_ALEN);
186 
187 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
188 		       HOSTAPD_LEVEL_INFO, "associated");
189 
190 	ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
191 	if (elems.wps_ie) {
192 		ie = elems.wps_ie - 2;
193 		ielen = elems.wps_ie_len + 2;
194 		wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
195 	} else if (elems.rsn_ie) {
196 		ie = elems.rsn_ie - 2;
197 		ielen = elems.rsn_ie_len + 2;
198 		wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
199 	} else if (elems.wpa_ie) {
200 		ie = elems.wpa_ie - 2;
201 		ielen = elems.wpa_ie_len + 2;
202 		wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
203 #ifdef CONFIG_HS20
204 	} else if (elems.osen) {
205 		ie = elems.osen - 2;
206 		ielen = elems.osen_len + 2;
207 		wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
208 #endif /* CONFIG_HS20 */
209 	} else {
210 		ie = NULL;
211 		ielen = 0;
212 		wpa_printf(MSG_DEBUG,
213 			   "STA did not include WPS/RSN/WPA IE in (Re)AssocReq");
214 	}
215 
216 	sta = ap_get_sta(hapd, addr);
217 	if (sta) {
218 		ap_sta_no_session_timeout(hapd, sta);
219 		accounting_sta_stop(hapd, sta);
220 
221 		/*
222 		 * Make sure that the previously registered inactivity timer
223 		 * will not remove the STA immediately.
224 		 */
225 		sta->timeout_next = STA_NULLFUNC;
226 	} else {
227 		sta = ap_sta_add(hapd, addr);
228 		if (sta == NULL) {
229 			hostapd_drv_sta_disassoc(hapd, addr,
230 						 WLAN_REASON_DISASSOC_AP_BUSY);
231 			return -1;
232 		}
233 	}
234 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
235 
236 	/*
237 	 * ACL configurations to the drivers (implementing AP SME and ACL
238 	 * offload) without hostapd's knowledge, can result in a disconnection
239 	 * though the driver accepts the connection. Skip the hostapd check for
240 	 * ACL if the driver supports ACL offload to avoid potentially
241 	 * conflicting ACL rules.
242 	 */
243 	if (hapd->iface->drv_max_acl_mac_addrs == 0 &&
244 	    hostapd_check_acl(hapd, addr, NULL) != HOSTAPD_ACL_ACCEPT) {
245 		wpa_printf(MSG_INFO, "STA " MACSTR_SEC " not allowed to connect",
246 			   MAC2STR_SEC(addr));
247 		reason = WLAN_REASON_UNSPECIFIED;
248 		goto fail;
249 	}
250 
251 #ifdef CONFIG_P2P
252 	if (elems.p2p) {
253 		wpabuf_free(sta->p2p_ie);
254 		sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
255 							  P2P_IE_VENDOR_TYPE);
256 		if (sta->p2p_ie)
257 			p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
258 	}
259 #endif /* CONFIG_P2P */
260 
261 #ifdef NEED_AP_MLME
262 	if (elems.ht_capabilities &&
263 	    (hapd->iface->conf->ht_capab &
264 	     HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
265 		struct ieee80211_ht_capabilities *ht_cap =
266 			(struct ieee80211_ht_capabilities *)
267 			elems.ht_capabilities;
268 
269 		if (le_to_host16(ht_cap->ht_capabilities_info) &
270 		    HT_CAP_INFO_40MHZ_INTOLERANT)
271 			ht40_intolerant_add(hapd->iface, sta);
272 	}
273 #endif /* NEED_AP_MLME */
274 
275 #ifdef CONFIG_INTERWORKING
276 	if (elems.ext_capab && elems.ext_capab_len > 4) {
277 		if (elems.ext_capab[4] & 0x01)
278 			sta->qos_map_enabled = 1;
279 	}
280 #endif /* CONFIG_INTERWORKING */
281 
282 #ifdef CONFIG_HS20
283 	wpabuf_free(sta->hs20_ie);
284 	if (elems.hs20 && elems.hs20_len > 4) {
285 		sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
286 						 elems.hs20_len - 4);
287 	} else
288 		sta->hs20_ie = NULL;
289 
290 	wpabuf_free(sta->roaming_consortium);
291 	if (elems.roaming_cons_sel)
292 		sta->roaming_consortium = wpabuf_alloc_copy(
293 			elems.roaming_cons_sel + 4,
294 			elems.roaming_cons_sel_len - 4);
295 	else
296 		sta->roaming_consortium = NULL;
297 #endif /* CONFIG_HS20 */
298 
299 #ifdef CONFIG_FST
300 	wpabuf_free(sta->mb_ies);
301 	if (hapd->iface->fst)
302 		sta->mb_ies = mb_ies_by_info(&elems.mb_ies);
303 	else
304 		sta->mb_ies = NULL;
305 #endif /* CONFIG_FST */
306 
307 	mbo_ap_check_sta_assoc(hapd, sta, &elems);
308 
309 	ap_copy_sta_supp_op_classes(sta, elems.supp_op_classes,
310 				    elems.supp_op_classes_len);
311 
312 	if (hapd->conf->wpa) {
313 		if (ie == NULL || ielen == 0) {
314 #ifdef CONFIG_WPS
315 			if (hapd->conf->wps_state) {
316 				wpa_printf(MSG_DEBUG,
317 					   "STA did not include WPA/RSN IE in (Re)Association Request - possible WPS use");
318 				sta->flags |= WLAN_STA_MAYBE_WPS;
319 				goto skip_wpa_check;
320 			}
321 #endif /* CONFIG_WPS */
322 
323 			wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
324 			reason = WLAN_REASON_INVALID_IE;
325 			status = WLAN_STATUS_INVALID_IE;
326 			goto fail;
327 		}
328 #ifdef CONFIG_WPS
329 		if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
330 		    os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
331 			struct wpabuf *wps;
332 
333 			if (check_sa_query_need(hapd, sta)) {
334 				status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
335 
336 				p = hostapd_eid_assoc_comeback_time(hapd, sta,
337 								    p);
338 
339 				hostapd_sta_assoc(hapd, addr, reassoc, status,
340 						  buf, p - buf);
341 				return 0;
342 			}
343 
344 			sta->flags |= WLAN_STA_WPS;
345 			wps = ieee802_11_vendor_ie_concat(ie, ielen,
346 							  WPS_IE_VENDOR_TYPE);
347 			if (wps) {
348 				if (wps_is_20(wps)) {
349 					wpa_printf(MSG_DEBUG,
350 						   "WPS: STA supports WPS 2.0");
351 					sta->flags |= WLAN_STA_WPS2;
352 				}
353 				wpabuf_free(wps);
354 			}
355 			goto skip_wpa_check;
356 		}
357 #endif /* CONFIG_WPS */
358 
359 		if (sta->wpa_sm == NULL)
360 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
361 							sta->addr,
362 							p2p_dev_addr);
363 		if (sta->wpa_sm == NULL) {
364 			wpa_printf(MSG_ERROR,
365 				   "Failed to initialize WPA state machine");
366 			return -1;
367 		}
368 		res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
369 					  hapd->iface->freq,
370 					  ie, ielen,
371 					  elems.rsnxe ? elems.rsnxe - 2 : NULL,
372 					  elems.rsnxe ? elems.rsnxe_len + 2 : 0,
373 					  elems.mdie, elems.mdie_len,
374 					  elems.owe_dh, elems.owe_dh_len);
375 		reason = WLAN_REASON_INVALID_IE;
376 		status = WLAN_STATUS_INVALID_IE;
377 		switch (res) {
378 		case WPA_IE_OK:
379 			reason = WLAN_REASON_UNSPECIFIED;
380 			status = WLAN_STATUS_SUCCESS;
381 			break;
382 		case WPA_INVALID_IE:
383 			reason = WLAN_REASON_INVALID_IE;
384 			status = WLAN_STATUS_INVALID_IE;
385 			break;
386 		case WPA_INVALID_GROUP:
387 			reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
388 			status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
389 			break;
390 		case WPA_INVALID_PAIRWISE:
391 			reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
392 			status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
393 			break;
394 		case WPA_INVALID_AKMP:
395 			reason = WLAN_REASON_AKMP_NOT_VALID;
396 			status = WLAN_STATUS_AKMP_NOT_VALID;
397 			break;
398 		case WPA_NOT_ENABLED:
399 			reason = WLAN_REASON_INVALID_IE;
400 			status = WLAN_STATUS_INVALID_IE;
401 			break;
402 		case WPA_ALLOC_FAIL:
403 			reason = WLAN_REASON_UNSPECIFIED;
404 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
405 			break;
406 		case WPA_MGMT_FRAME_PROTECTION_VIOLATION:
407 			reason = WLAN_REASON_INVALID_IE;
408 			status = WLAN_STATUS_INVALID_IE;
409 			break;
410 		case WPA_INVALID_MGMT_GROUP_CIPHER:
411 			reason = WLAN_REASON_CIPHER_SUITE_REJECTED;
412 			status = WLAN_STATUS_CIPHER_REJECTED_PER_POLICY;
413 			break;
414 		case WPA_INVALID_MDIE:
415 			reason = WLAN_REASON_INVALID_MDE;
416 			status = WLAN_STATUS_INVALID_MDIE;
417 			break;
418 		case WPA_INVALID_PROTO:
419 			reason = WLAN_REASON_INVALID_IE;
420 			status = WLAN_STATUS_INVALID_IE;
421 			break;
422 		case WPA_INVALID_PMKID:
423 			reason = WLAN_REASON_INVALID_PMKID;
424 			status = WLAN_STATUS_INVALID_PMKID;
425 			break;
426 		case WPA_DENIED_OTHER_REASON:
427 			reason = WLAN_REASON_UNSPECIFIED;
428 			status = WLAN_STATUS_ASSOC_DENIED_UNSPEC;
429 			break;
430 		}
431 		if (status != WLAN_STATUS_SUCCESS) {
432 			wpa_printf(MSG_DEBUG,
433 				   "WPA/RSN information element rejected? (res %u)",
434 				   res);
435 			wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
436 			goto fail;
437 		}
438 
439 		if (check_sa_query_need(hapd, sta)) {
440 			status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
441 
442 			p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
443 
444 			hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
445 					  p - buf);
446 			return 0;
447 		}
448 
449 		if (wpa_auth_uses_mfp(sta->wpa_sm))
450 			sta->flags |= WLAN_STA_MFP;
451 		else
452 			sta->flags &= ~WLAN_STA_MFP;
453 
454 #ifdef CONFIG_IEEE80211R_AP
455 		if (sta->auth_alg == WLAN_AUTH_FT) {
456 			status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
457 							 req_ies_len);
458 			if (status != WLAN_STATUS_SUCCESS) {
459 				if (status == WLAN_STATUS_INVALID_PMKID)
460 					reason = WLAN_REASON_INVALID_IE;
461 				if (status == WLAN_STATUS_INVALID_MDIE)
462 					reason = WLAN_REASON_INVALID_IE;
463 				if (status == WLAN_STATUS_INVALID_FTIE)
464 					reason = WLAN_REASON_INVALID_IE;
465 				goto fail;
466 			}
467 		}
468 #endif /* CONFIG_IEEE80211R_AP */
469 #ifdef CONFIG_SAE
470 		if (hapd->conf->sae_pwe == 2 &&
471 		    sta->auth_alg == WLAN_AUTH_SAE &&
472 		    sta->sae && !sta->sae->h2e &&
473 		    ieee802_11_rsnx_capab_len(elems.rsnxe, elems.rsnxe_len,
474 					      WLAN_RSNX_CAPAB_SAE_H2E)) {
475 			wpa_printf(MSG_INFO, "SAE: " MACSTR_SEC
476 				   " indicates support for SAE H2E, but did not use it",
477 				   MAC2STR_SEC(sta->addr));
478 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
479 			reason = WLAN_REASON_UNSPECIFIED;
480 			goto fail;
481 		}
482 #endif /* CONFIG_SAE */
483 	} else if (hapd->conf->wps_state) {
484 #ifdef CONFIG_WPS
485 		struct wpabuf *wps;
486 
487 		if (req_ies)
488 			wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
489 							  WPS_IE_VENDOR_TYPE);
490 		else
491 			wps = NULL;
492 #ifdef CONFIG_WPS_STRICT
493 		if (wps && wps_validate_assoc_req(wps) < 0) {
494 			reason = WLAN_REASON_INVALID_IE;
495 			status = WLAN_STATUS_INVALID_IE;
496 			wpabuf_free(wps);
497 			goto fail;
498 		}
499 #endif /* CONFIG_WPS_STRICT */
500 		if (wps) {
501 			sta->flags |= WLAN_STA_WPS;
502 			if (wps_is_20(wps)) {
503 				wpa_printf(MSG_DEBUG,
504 					   "WPS: STA supports WPS 2.0");
505 				sta->flags |= WLAN_STA_WPS2;
506 			}
507 		} else
508 			sta->flags |= WLAN_STA_MAYBE_WPS;
509 		wpabuf_free(wps);
510 #endif /* CONFIG_WPS */
511 #ifdef CONFIG_HS20
512 	} else if (hapd->conf->osen) {
513 		if (elems.osen == NULL) {
514 			hostapd_logger(
515 				hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
516 				HOSTAPD_LEVEL_INFO,
517 				"No HS 2.0 OSEN element in association request");
518 			return WLAN_STATUS_INVALID_IE;
519 		}
520 
521 		wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
522 		if (sta->wpa_sm == NULL)
523 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
524 							sta->addr, NULL);
525 		if (sta->wpa_sm == NULL) {
526 			wpa_printf(MSG_WARNING,
527 				   "Failed to initialize WPA state machine");
528 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
529 		}
530 		if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
531 				      elems.osen - 2, elems.osen_len + 2) < 0)
532 			return WLAN_STATUS_INVALID_IE;
533 #endif /* CONFIG_HS20 */
534 	}
535 #ifdef CONFIG_WPS
536 skip_wpa_check:
537 #endif /* CONFIG_WPS */
538 
539 #ifdef CONFIG_MBO
540 	if (hapd->conf->mbo_enabled && (hapd->conf->wpa & 2) &&
541 	    elems.mbo && sta->cell_capa && !(sta->flags & WLAN_STA_MFP) &&
542 	    hapd->conf->ieee80211w != NO_MGMT_FRAME_PROTECTION) {
543 		wpa_printf(MSG_INFO,
544 			   "MBO: Reject WPA2 association without PMF");
545 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
546 	}
547 #endif /* CONFIG_MBO */
548 
549 #ifdef CONFIG_IEEE80211R_AP
550 	p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
551 					sta->auth_alg, req_ies, req_ies_len,
552 					!elems.rsnxe);
553 	if (!p) {
554 		wpa_printf(MSG_DEBUG, "FT: Failed to write AssocResp IEs");
555 		return WLAN_STATUS_UNSPECIFIED_FAILURE;
556 	}
557 #endif /* CONFIG_IEEE80211R_AP */
558 
559 #ifdef CONFIG_FILS
560 	if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
561 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
562 	    sta->auth_alg == WLAN_AUTH_FILS_PK) {
563 		int delay_assoc = 0;
564 
565 		if (!req_ies)
566 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
567 
568 		if (!wpa_fils_validate_fils_session(sta->wpa_sm, req_ies,
569 						    req_ies_len,
570 						    sta->fils_session)) {
571 			wpa_printf(MSG_DEBUG,
572 				   "FILS: Session validation failed");
573 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
574 		}
575 
576 		res = wpa_fils_validate_key_confirm(sta->wpa_sm, req_ies,
577 						    req_ies_len);
578 		if (res < 0) {
579 			wpa_printf(MSG_DEBUG,
580 				   "FILS: Key Confirm validation failed");
581 			return WLAN_STATUS_UNSPECIFIED_FAILURE;
582 		}
583 
584 		if (fils_process_hlp(hapd, sta, req_ies, req_ies_len) > 0) {
585 			wpa_printf(MSG_DEBUG,
586 				   "FILS: Delaying Assoc Response (HLP)");
587 			delay_assoc = 1;
588 		} else {
589 			wpa_printf(MSG_DEBUG,
590 				   "FILS: Going ahead with Assoc Response (no HLP)");
591 		}
592 
593 		if (sta) {
594 			wpa_printf(MSG_DEBUG, "FILS: HLP callback cleanup");
595 			eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
596 			os_free(sta->fils_pending_assoc_req);
597 			sta->fils_pending_assoc_req = NULL;
598 			sta->fils_pending_assoc_req_len = 0;
599 			wpabuf_free(sta->fils_hlp_resp);
600 			sta->fils_hlp_resp = NULL;
601 			sta->fils_drv_assoc_finish = 0;
602 		}
603 
604 		if (sta && delay_assoc && status == WLAN_STATUS_SUCCESS) {
605 			u8 *req_tmp;
606 
607 			req_tmp = os_malloc(req_ies_len);
608 			if (!req_tmp) {
609 				wpa_printf(MSG_DEBUG,
610 					   "FILS: buffer allocation failed for assoc req");
611 				goto fail;
612 			}
613 			os_memcpy(req_tmp, req_ies, req_ies_len);
614 			sta->fils_pending_assoc_req = req_tmp;
615 			sta->fils_pending_assoc_req_len = req_ies_len;
616 			sta->fils_pending_assoc_is_reassoc = reassoc;
617 			sta->fils_drv_assoc_finish = 1;
618 			wpa_printf(MSG_DEBUG,
619 				   "FILS: Waiting for HLP processing before sending (Re)Association Response frame to "
620 				   MACSTR_SEC, MAC2STR_SEC(sta->addr));
621 			eloop_register_timeout(
622 				0, hapd->conf->fils_hlp_wait_time * 1024,
623 				fils_hlp_timeout, hapd, sta);
624 			return 0;
625 		}
626 		p = hostapd_eid_assoc_fils_session(sta->wpa_sm, p,
627 						   elems.fils_session,
628 						   sta->fils_hlp_resp);
629 		wpa_hexdump(MSG_DEBUG, "FILS Assoc Resp BUF (IEs)",
630 			    buf, p - buf);
631 	}
632 #endif /* CONFIG_FILS */
633 
634 #ifdef CONFIG_OWE
635 	if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE) &&
636 	    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_OWE &&
637 	    elems.owe_dh) {
638 		u8 *npos;
639 		u16 ret_status;
640 
641 		npos = owe_assoc_req_process(hapd, sta,
642 					     elems.owe_dh, elems.owe_dh_len,
643 					     p, sizeof(buf) - (p - buf),
644 					     &ret_status);
645 		status = ret_status;
646 		if (npos)
647 			p = npos;
648 
649 		if (!npos &&
650 		    status == WLAN_STATUS_FINITE_CYCLIC_GROUP_NOT_SUPPORTED) {
651 			hostapd_sta_assoc(hapd, addr, reassoc, ret_status, buf,
652 					  p - buf);
653 			return 0;
654 		}
655 
656 		if (!npos || status != WLAN_STATUS_SUCCESS)
657 			goto fail;
658 	}
659 #endif /* CONFIG_OWE */
660 
661 #ifdef CONFIG_DPP2
662 		dpp_pfs_free(sta->dpp_pfs);
663 		sta->dpp_pfs = NULL;
664 
665 		if ((hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_DPP) &&
666 		    hapd->conf->dpp_netaccesskey && sta->wpa_sm &&
667 		    wpa_auth_sta_key_mgmt(sta->wpa_sm) == WPA_KEY_MGMT_DPP &&
668 		    elems.owe_dh) {
669 			sta->dpp_pfs = dpp_pfs_init(
670 				wpabuf_head(hapd->conf->dpp_netaccesskey),
671 				wpabuf_len(hapd->conf->dpp_netaccesskey));
672 			if (!sta->dpp_pfs) {
673 				wpa_printf(MSG_DEBUG,
674 					   "DPP: Could not initialize PFS");
675 				/* Try to continue without PFS */
676 				goto pfs_fail;
677 			}
678 
679 			if (dpp_pfs_process(sta->dpp_pfs, elems.owe_dh,
680 					    elems.owe_dh_len) < 0) {
681 				dpp_pfs_free(sta->dpp_pfs);
682 				sta->dpp_pfs = NULL;
683 				reason = WLAN_REASON_UNSPECIFIED;
684 				goto fail;
685 			}
686 		}
687 
688 		wpa_auth_set_dpp_z(sta->wpa_sm, sta->dpp_pfs ?
689 				   sta->dpp_pfs->secret : NULL);
690 	pfs_fail:
691 #endif /* CONFIG_DPP2 */
692 
693 	if (elems.rrm_enabled &&
694 	    elems.rrm_enabled_len >= sizeof(sta->rrm_enabled_capa))
695 	    os_memcpy(sta->rrm_enabled_capa, elems.rrm_enabled,
696 		      sizeof(sta->rrm_enabled_capa));
697 
698 #if defined(CONFIG_IEEE80211R_AP) || defined(CONFIG_FILS) || defined(CONFIG_OWE)
699 	hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
700 
701 	if (sta->auth_alg == WLAN_AUTH_FT ||
702 	    sta->auth_alg == WLAN_AUTH_FILS_SK ||
703 	    sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
704 	    sta->auth_alg == WLAN_AUTH_FILS_PK)
705 		ap_sta_set_authorized(hapd, sta, 1);
706 #else /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
707 	/* Keep compiler silent about unused variables */
708 	if (status) {
709 	}
710 #endif /* CONFIG_IEEE80211R_AP || CONFIG_FILS */
711 
712 	new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
713 	sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
714 	sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
715 
716 	hostapd_set_sta_flags(hapd, sta);
717 
718 #ifdef CONFIG_VENDOR_EXT
719 	int result = wpa_vendor_ext_process_hostapd_assoc(hapd, req_ies, req_ies_len, sta);
720 	if (result == -1) {
721 		goto fail;
722 	}
723 	if (result == 1) {
724 		goto processed;
725 	}
726 #endif
727 	if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
728 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
729 #ifdef CONFIG_FILS
730 	else if (sta->auth_alg == WLAN_AUTH_FILS_SK ||
731 		 sta->auth_alg == WLAN_AUTH_FILS_SK_PFS ||
732 		 sta->auth_alg == WLAN_AUTH_FILS_PK)
733 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FILS);
734 #endif /* CONFIG_FILS */
735 	else
736 		wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
737 #ifdef CONFIG_VENDOR_EXT
738 processed:
739 #endif
740 	hostapd_new_assoc_sta(hapd, sta, !new_assoc);
741 
742 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
743 
744 #ifdef CONFIG_P2P
745 	if (req_ies) {
746 		p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
747 				      req_ies, req_ies_len);
748 	}
749 #endif /* CONFIG_P2P */
750 
751 	return 0;
752 
753 fail:
754 #ifdef CONFIG_IEEE80211R_AP
755 	if (status >= 0)
756 		hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
757 #endif /* CONFIG_IEEE80211R_AP */
758 	hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
759 	ap_free_sta(hapd, sta);
760 	return -1;
761 }
762 
763 
hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)764 void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
765 {
766 	struct sta_info *sta;
767 
768 	if (addr == NULL) {
769 		/*
770 		 * This could potentially happen with unexpected event from the
771 		 * driver wrapper. This was seen at least in one case where the
772 		 * driver ended up reporting a station mode event while hostapd
773 		 * was running, so better make sure we stop processing such an
774 		 * event here.
775 		 */
776 		wpa_printf(MSG_DEBUG,
777 			   "hostapd_notif_disassoc: Skip event with no address");
778 		return;
779 	}
780 
781 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
782 		       HOSTAPD_LEVEL_INFO, "disassociated");
783 
784 	sta = ap_get_sta(hapd, addr);
785 	if (sta == NULL) {
786 		wpa_printf(MSG_DEBUG,
787 			   "Disassociation notification for unknown STA "
788 			   MACSTR_SEC, MAC2STR_SEC(addr));
789 		return;
790 	}
791 
792 	ap_sta_set_authorized(hapd, sta, 0);
793 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
794 	hostapd_set_sta_flags(hapd, sta);
795 	wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
796 	sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
797 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
798 	ap_free_sta(hapd, sta);
799 }
800 
801 
hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)802 void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
803 {
804 	struct sta_info *sta = ap_get_sta(hapd, addr);
805 
806 	if (!sta || !hapd->conf->disassoc_low_ack || sta->agreed_to_steer)
807 		return;
808 
809 	hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
810 		       HOSTAPD_LEVEL_INFO,
811 		       "disconnected due to excessive missing ACKs");
812 	hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
813 	ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
814 }
815 
816 
hostapd_event_sta_opmode_changed(struct hostapd_data *hapd, const u8 *addr, enum smps_mode smps_mode, enum chan_width chan_width, u8 rx_nss)817 void hostapd_event_sta_opmode_changed(struct hostapd_data *hapd, const u8 *addr,
818 				      enum smps_mode smps_mode,
819 				      enum chan_width chan_width, u8 rx_nss)
820 {
821 	struct sta_info *sta = ap_get_sta(hapd, addr);
822 	const char *txt;
823 
824 	if (!sta)
825 		return;
826 
827 	switch (smps_mode) {
828 	case SMPS_AUTOMATIC:
829 		txt = "automatic";
830 		break;
831 	case SMPS_OFF:
832 		txt = "off";
833 		break;
834 	case SMPS_DYNAMIC:
835 		txt = "dynamic";
836 		break;
837 	case SMPS_STATIC:
838 		txt = "static";
839 		break;
840 	default:
841 		txt = NULL;
842 		break;
843 	}
844 	if (txt) {
845 		wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_SMPS_MODE_CHANGED
846 			MACSTR " %s", MAC2STR(addr), txt);
847 	}
848 
849 	switch (chan_width) {
850 	case CHAN_WIDTH_20_NOHT:
851 		txt = "20(no-HT)";
852 		break;
853 	case CHAN_WIDTH_20:
854 		txt = "20";
855 		break;
856 	case CHAN_WIDTH_40:
857 		txt = "40";
858 		break;
859 	case CHAN_WIDTH_80:
860 		txt = "80";
861 		break;
862 	case CHAN_WIDTH_80P80:
863 		txt = "80+80";
864 		break;
865 	case CHAN_WIDTH_160:
866 		txt = "160";
867 		break;
868 	default:
869 		txt = NULL;
870 		break;
871 	}
872 	if (txt) {
873 		wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_MAX_BW_CHANGED
874 			MACSTR " %s", MAC2STR(addr), txt);
875 	}
876 
877 	if (rx_nss != 0xff) {
878 		wpa_msg(hapd->msg_ctx, MSG_INFO, STA_OPMODE_N_SS_CHANGED
879 			MACSTR " %d", MAC2STR(addr), rx_nss);
880 	}
881 }
882 
883 
hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht, int offset, int width, int cf1, int cf2, int finished)884 void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
885 			     int offset, int width, int cf1, int cf2,
886 			     int finished)
887 {
888 #define VENDOR_EXT_CHANNEL_SWITCH 4
889 #ifdef NEED_AP_MLME
890 	int channel, chwidth, is_dfs;
891 	u8 seg0_idx = 0, seg1_idx = 0;
892 	size_t i;
893 
894 	hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
895 		       HOSTAPD_LEVEL_INFO,
896 		       "driver %s channel switch: freq=%d, ht=%d, vht_ch=0x%x, he_ch=0x%x, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
897 		       finished ? "had" : "starting",
898 		       freq, ht, hapd->iconf->ch_switch_vht_config,
899 		       hapd->iconf->ch_switch_he_config, offset,
900 		       width, channel_width_to_string(width), cf1, cf2);
901 
902 	if (!hapd->iface->current_mode) {
903 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
904 			       HOSTAPD_LEVEL_WARNING,
905 			       "ignore channel switch since the interface is not yet ready");
906 		return;
907 	}
908 
909 	hapd->iface->freq = freq;
910 
911 	channel = hostapd_hw_get_channel(hapd, freq);
912 	if (!channel) {
913 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
914 			       HOSTAPD_LEVEL_WARNING,
915 			       "driver switched to bad channel!");
916 		return;
917 	}
918 
919 	switch (width) {
920 	case CHAN_WIDTH_80:
921 		chwidth = CHANWIDTH_80MHZ;
922 		break;
923 	case CHAN_WIDTH_80P80:
924 		chwidth = CHANWIDTH_80P80MHZ;
925 		break;
926 	case CHAN_WIDTH_160:
927 		chwidth = CHANWIDTH_160MHZ;
928 		break;
929 	case CHAN_WIDTH_20_NOHT:
930 	case CHAN_WIDTH_20:
931 	case CHAN_WIDTH_40:
932 	default:
933 		chwidth = CHANWIDTH_USE_HT;
934 		break;
935 	}
936 
937 	switch (hapd->iface->current_mode->mode) {
938 	case HOSTAPD_MODE_IEEE80211A:
939 		if (cf1 == 5935)
940 			seg0_idx = (cf1 - 5925) / 5;
941 		else if (cf1 > 5950)
942 			seg0_idx = (cf1 - 5950) / 5;
943 		else if (cf1 > 5000)
944 			seg0_idx = (cf1 - 5000) / 5;
945 
946 		if (cf2 == 5935)
947 			seg1_idx = (cf2 - 5925) / 5;
948 		else if (cf2 > 5950)
949 			seg1_idx = (cf2 - 5950) / 5;
950 		else if (cf2 > 5000)
951 			seg1_idx = (cf2 - 5000) / 5;
952 		break;
953 	default:
954 		ieee80211_freq_to_chan(cf1, &seg0_idx);
955 		ieee80211_freq_to_chan(cf2, &seg1_idx);
956 		break;
957 	}
958 
959 	hapd->iconf->channel = channel;
960 	hapd->iconf->ieee80211n = ht;
961 	if (!ht) {
962 		hapd->iconf->ieee80211ac = 0;
963 	} else if (hapd->iconf->ch_switch_vht_config) {
964 		/* CHAN_SWITCH VHT config */
965 		if (hapd->iconf->ch_switch_vht_config &
966 		    CH_SWITCH_VHT_ENABLED)
967 			hapd->iconf->ieee80211ac = 1;
968 		else if (hapd->iconf->ch_switch_vht_config &
969 			 CH_SWITCH_VHT_DISABLED)
970 			hapd->iconf->ieee80211ac = 0;
971 	} else if (hapd->iconf->ch_switch_he_config) {
972 		/* CHAN_SWITCH HE config */
973 		if (hapd->iconf->ch_switch_he_config &
974 		    CH_SWITCH_HE_ENABLED)
975 			hapd->iconf->ieee80211ax = 1;
976 		else if (hapd->iconf->ch_switch_he_config &
977 			 CH_SWITCH_HE_DISABLED)
978 			hapd->iconf->ieee80211ax = 0;
979 	}
980 	hapd->iconf->ch_switch_vht_config = 0;
981 	hapd->iconf->ch_switch_he_config = 0;
982 
983 	if (width == CHAN_WIDTH_40 || width == CHAN_WIDTH_80 ||
984 	    width == CHAN_WIDTH_80P80 || width == CHAN_WIDTH_160)
985 		hapd->iconf->ht_capab |= HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
986 	else if (width == CHAN_WIDTH_20 || width == CHAN_WIDTH_20_NOHT)
987 		hapd->iconf->ht_capab &= ~HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET;
988 
989 	hapd->iconf->secondary_channel = offset;
990 	hostapd_set_oper_chwidth(hapd->iconf, chwidth);
991 	hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, seg0_idx);
992 	hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, seg1_idx);
993 	if (hapd->iconf->ieee80211ac) {
994 		hapd->iconf->vht_capab &= ~VHT_CAP_SUPP_CHAN_WIDTH_MASK;
995 		if (chwidth == CHANWIDTH_160MHZ)
996 			hapd->iconf->vht_capab |=
997 				VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
998 		else if (chwidth == CHANWIDTH_80P80MHZ)
999 			hapd->iconf->vht_capab |=
1000 				VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ;
1001 	}
1002 
1003 	is_dfs = ieee80211_is_dfs(freq, hapd->iface->hw_features,
1004 				  hapd->iface->num_hw_features);
1005 
1006 	wpa_msg(hapd->msg_ctx, MSG_INFO,
1007 		"%sfreq=%d ht_enabled=%d ch_offset=%d ch_width=%s cf1=%d cf2=%d dfs=%d",
1008 		finished ? WPA_EVENT_CHANNEL_SWITCH :
1009 		WPA_EVENT_CHANNEL_SWITCH_STARTED,
1010 		freq, ht, offset, channel_width_to_string(width),
1011 		cf1, cf2, is_dfs);
1012 #ifdef CONFIG_LIBWPA_VENDOR
1013 	if (finished) {
1014 		struct WpaVendorExtInfo wpaVendorExtInfo;
1015 		os_memset(&wpaVendorExtInfo, 0, sizeof(struct WpaVendorExtInfo));
1016 		wpaVendorExtInfo.type = VENDOR_EXT_CHANNEL_SWITCH;
1017 		wpaVendorExtInfo.freq = freq;
1018 		wpaVendorExtInfo.width = width;
1019 		wpa_printf(MSG_INFO, "HDI: EVENT_CHANNEL_SWITCH-type %d ", wpaVendorExtInfo.type);
1020 		struct wpa_supplicant *wpa_s = (struct wpa_supplicant *) hapd->msg_ctx;
1021 		WpaEventReport(wpa_s->ifname, WPA_EVENT_VENDOR_EXT, (void *) &wpaVendorExtInfo);
1022 	}
1023 #endif
1024 	if (!finished)
1025 		return;
1026 
1027 	if (hapd->csa_in_progress &&
1028 	    freq == hapd->cs_freq_params.freq) {
1029 		hostapd_cleanup_cs_params(hapd);
1030 		ieee802_11_set_beacon(hapd);
1031 
1032 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
1033 			"freq=%d dfs=%d", freq, is_dfs);
1034 #ifdef CONFIG_LIBWPA_VENDOR
1035 		struct HostapdApCbParm hostapdApCbParm = {};
1036 		int result = os_snprintf((char*)hostapdApCbParm.content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
1037 			"%sfreq=%d", AP_CSA_FINISHED, freq);
1038 		if (os_snprintf_error(WIFI_HOSTAPD_CB_CONTENT_LENGTH, result)) {
1039 			wpa_printf(MSG_ERROR, "hostapd_event_ch_switch AP_CSA_FINISHED os_snprintf_error");
1040 		} else {
1041 			hostapdApCbParm.id = 0;
1042 			wpa_printf(MSG_INFO, "1: %s AP_CSA_FINISHED %s", __func__, hostapdApCbParm.content);
1043 			HostapdEventReport(hapd->conf->iface, HOSTAPD_EVENT_AP_STATE, (void *) &hostapdApCbParm);
1044 		}
1045 #endif
1046 	} else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
1047 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
1048 			"freq=%d dfs=%d", freq, is_dfs);
1049 #ifdef CONFIG_LIBWPA_VENDOR
1050 		struct HostapdApCbParm hostapdApCbParm = {};
1051 		int result = os_snprintf((char*)hostapdApCbParm.content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
1052 			"%sfreq=%d", AP_CSA_FINISHED, freq);
1053 		if (os_snprintf_error(WIFI_HOSTAPD_CB_CONTENT_LENGTH, result)) {
1054 			wpa_printf(MSG_ERROR, "hostapd_event_ch_switch AP_CSA_FINISHED os_snprintf_error");
1055 		} else {
1056 			hostapdApCbParm.id = 0;
1057 			wpa_printf(MSG_INFO, "1: %s AP_CSA_FINISHED %s", __func__, hostapdApCbParm.content);
1058 			HostapdEventReport(hapd->conf->iface, HOSTAPD_EVENT_AP_STATE, (void *) &hostapdApCbParm);
1059 		}
1060 #endif
1061 	} else if (is_dfs &&
1062 		   hostapd_is_dfs_required(hapd->iface) &&
1063 		   !hostapd_is_dfs_chan_available(hapd->iface) &&
1064 		   !hapd->iface->cac_started) {
1065 		hostapd_disable_iface(hapd->iface);
1066 		hostapd_enable_iface(hapd->iface);
1067 	}
1068 
1069 	for (i = 0; i < hapd->iface->num_bss; i++)
1070 		hostapd_neighbor_set_own_report(hapd->iface->bss[i]);
1071 
1072 #ifdef CONFIG_OCV
1073 	if (hapd->conf->ocv) {
1074 		struct sta_info *sta;
1075 		bool check_sa_query = false;
1076 
1077 		for (sta = hapd->sta_list; sta; sta = sta->next) {
1078 			if (wpa_auth_uses_ocv(sta->wpa_sm) &&
1079 			    !(sta->flags & WLAN_STA_WNM_SLEEP_MODE)) {
1080 				sta->post_csa_sa_query = 1;
1081 				check_sa_query = true;
1082 			}
1083 		}
1084 
1085 		if (check_sa_query) {
1086 			wpa_printf(MSG_DEBUG,
1087 				   "OCV: Check post-CSA SA Query initiation in 15 seconds");
1088 			eloop_register_timeout(15, 0,
1089 					       hostapd_ocv_check_csa_sa_query,
1090 					       hapd, NULL);
1091 		}
1092 	}
1093 #endif /* CONFIG_OCV */
1094 #endif /* NEED_AP_MLME */
1095 }
1096 
1097 
hostapd_event_connect_failed_reason(struct hostapd_data *hapd, const u8 *addr, int reason_code)1098 void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
1099 					 const u8 *addr, int reason_code)
1100 {
1101 	switch (reason_code) {
1102 	case MAX_CLIENT_REACHED:
1103 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
1104 			MAC2STR(addr));
1105 		break;
1106 	case BLOCKED_CLIENT:
1107 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
1108 			MAC2STR(addr));
1109 		break;
1110 	}
1111 }
1112 
1113 
1114 #ifdef CONFIG_ACS
hostapd_acs_channel_selected(struct hostapd_data *hapd, struct acs_selected_channels *acs_res)1115 void hostapd_acs_channel_selected(struct hostapd_data *hapd,
1116 				  struct acs_selected_channels *acs_res)
1117 {
1118 	int ret, i;
1119 	int err = 0;
1120 	struct hostapd_channel_data *pri_chan;
1121 
1122 	if (hapd->iconf->channel) {
1123 		wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
1124 			   hapd->iconf->channel);
1125 		return;
1126 	}
1127 
1128 	hapd->iface->freq = acs_res->pri_freq;
1129 
1130 	if (!hapd->iface->current_mode) {
1131 		for (i = 0; i < hapd->iface->num_hw_features; i++) {
1132 			struct hostapd_hw_modes *mode =
1133 				&hapd->iface->hw_features[i];
1134 
1135 			if (mode->mode == acs_res->hw_mode) {
1136 				if (hapd->iface->freq > 0 &&
1137 				    !hw_get_chan(mode->mode,
1138 						 hapd->iface->freq,
1139 						 hapd->iface->hw_features,
1140 						 hapd->iface->num_hw_features))
1141 					continue;
1142 				hapd->iface->current_mode = mode;
1143 				break;
1144 			}
1145 		}
1146 		if (!hapd->iface->current_mode) {
1147 			hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1148 				       HOSTAPD_LEVEL_WARNING,
1149 				       "driver selected to bad hw_mode");
1150 			err = 1;
1151 			goto out;
1152 		}
1153 	}
1154 
1155 	if (!acs_res->pri_freq) {
1156 		hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
1157 			       HOSTAPD_LEVEL_WARNING,
1158 			       "driver switched to bad channel");
1159 		err = 1;
1160 		goto out;
1161 	}
1162 	pri_chan = hw_get_channel_freq(hapd->iface->current_mode->mode,
1163 				       acs_res->pri_freq, NULL,
1164 				       hapd->iface->hw_features,
1165 				       hapd->iface->num_hw_features);
1166 	if (!pri_chan) {
1167 		wpa_printf(MSG_ERROR,
1168 			   "ACS: Could not determine primary channel number from pri_freq %u",
1169 			   acs_res->pri_freq);
1170 		err = 1;
1171 		goto out;
1172 	}
1173 
1174 	hapd->iconf->channel = pri_chan->chan;
1175 	hapd->iconf->acs = 1;
1176 
1177 	if (acs_res->sec_freq == 0)
1178 		hapd->iconf->secondary_channel = 0;
1179 	else if (acs_res->sec_freq < acs_res->pri_freq)
1180 		hapd->iconf->secondary_channel = -1;
1181 	else if (acs_res->sec_freq > acs_res->pri_freq)
1182 		hapd->iconf->secondary_channel = 1;
1183 	else {
1184 		wpa_printf(MSG_ERROR, "Invalid secondary channel!");
1185 		err = 1;
1186 		goto out;
1187 	}
1188 
1189 	hapd->iconf->edmg_channel = acs_res->edmg_channel;
1190 
1191 	if (hapd->iface->conf->ieee80211ac || hapd->iface->conf->ieee80211ax) {
1192 		/* set defaults for backwards compatibility */
1193 		hostapd_set_oper_centr_freq_seg1_idx(hapd->iconf, 0);
1194 		hostapd_set_oper_centr_freq_seg0_idx(hapd->iconf, 0);
1195 		hostapd_set_oper_chwidth(hapd->iconf, CHANWIDTH_USE_HT);
1196 		if (acs_res->ch_width == 40) {
1197 			if (is_6ghz_freq(acs_res->pri_freq))
1198 				hostapd_set_oper_centr_freq_seg0_idx(
1199 					hapd->iconf,
1200 					acs_res->vht_seg0_center_ch);
1201 		} else if (acs_res->ch_width == 80) {
1202 			hostapd_set_oper_centr_freq_seg0_idx(
1203 				hapd->iconf, acs_res->vht_seg0_center_ch);
1204 			if (acs_res->vht_seg1_center_ch == 0) {
1205 				hostapd_set_oper_chwidth(hapd->iconf,
1206 							 CHANWIDTH_80MHZ);
1207 			} else {
1208 				hostapd_set_oper_chwidth(hapd->iconf,
1209 							 CHANWIDTH_80P80MHZ);
1210 				hostapd_set_oper_centr_freq_seg1_idx(
1211 					hapd->iconf,
1212 					acs_res->vht_seg1_center_ch);
1213 			}
1214 		} else if (acs_res->ch_width == 160) {
1215 			hostapd_set_oper_chwidth(hapd->iconf, CHANWIDTH_160MHZ);
1216 			hostapd_set_oper_centr_freq_seg0_idx(
1217 				hapd->iconf, acs_res->vht_seg1_center_ch);
1218 		}
1219 	}
1220 
1221 out:
1222 	ret = hostapd_acs_completed(hapd->iface, err);
1223 	if (ret) {
1224 		wpa_printf(MSG_ERROR,
1225 			   "ACS: Possibly channel configuration is invalid");
1226 	}
1227 }
1228 #endif /* CONFIG_ACS */
1229 
1230 
hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da, const u8 *bssid, const u8 *ie, size_t ie_len, int ssi_signal)1231 int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
1232 			 const u8 *bssid, const u8 *ie, size_t ie_len,
1233 			 int ssi_signal)
1234 {
1235 	size_t i;
1236 	int ret = 0;
1237 
1238 	if (sa == NULL || ie == NULL)
1239 		return -1;
1240 
1241 	random_add_randomness(sa, ETH_ALEN);
1242 	for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
1243 		if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
1244 					    sa, da, bssid, ie, ie_len,
1245 					    ssi_signal) > 0) {
1246 			ret = 1;
1247 			break;
1248 		}
1249 	}
1250 	return ret;
1251 }
1252 
1253 
1254 #ifdef HOSTAPD
1255 
1256 #ifdef CONFIG_IEEE80211R_AP
hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst, const u8 *bssid, u16 auth_transaction, u16 status, const u8 *ies, size_t ies_len)1257 static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
1258 					  const u8 *bssid,
1259 					  u16 auth_transaction, u16 status,
1260 					  const u8 *ies, size_t ies_len)
1261 {
1262 	struct hostapd_data *hapd = ctx;
1263 	struct sta_info *sta;
1264 
1265 	sta = ap_get_sta(hapd, dst);
1266 	if (sta == NULL)
1267 		return;
1268 
1269 	hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
1270 		       HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
1271 	sta->flags |= WLAN_STA_AUTH;
1272 
1273 	hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
1274 }
1275 #endif /* CONFIG_IEEE80211R_AP */
1276 
1277 
1278 #ifdef CONFIG_FILS
hostapd_notify_auth_fils_finish(struct hostapd_data *hapd, struct sta_info *sta, u16 resp, struct wpabuf *data, int pub)1279 static void hostapd_notify_auth_fils_finish(struct hostapd_data *hapd,
1280 					    struct sta_info *sta, u16 resp,
1281 					    struct wpabuf *data, int pub)
1282 {
1283 	if (resp == WLAN_STATUS_SUCCESS) {
1284 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1285 			       HOSTAPD_LEVEL_DEBUG, "authentication OK (FILS)");
1286 		sta->flags |= WLAN_STA_AUTH;
1287 		wpa_auth_sm_event(sta->wpa_sm, WPA_AUTH);
1288 		sta->auth_alg = WLAN_AUTH_FILS_SK;
1289 		mlme_authenticate_indication(hapd, sta);
1290 	} else {
1291 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1292 			       HOSTAPD_LEVEL_DEBUG,
1293 			       "authentication failed (FILS)");
1294 	}
1295 
1296 	hostapd_sta_auth(hapd, sta->addr, 2, resp,
1297 			 data ? wpabuf_head(data) : NULL,
1298 			 data ? wpabuf_len(data) : 0);
1299 	wpabuf_free(data);
1300 }
1301 #endif /* CONFIG_FILS */
1302 
1303 
hostapd_notif_auth(struct hostapd_data *hapd, struct auth_info *rx_auth)1304 static void hostapd_notif_auth(struct hostapd_data *hapd,
1305 			       struct auth_info *rx_auth)
1306 {
1307 	struct sta_info *sta;
1308 	u16 status = WLAN_STATUS_SUCCESS;
1309 	u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
1310 	size_t resp_ies_len = 0;
1311 
1312 	sta = ap_get_sta(hapd, rx_auth->peer);
1313 	if (!sta) {
1314 		sta = ap_sta_add(hapd, rx_auth->peer);
1315 		if (sta == NULL) {
1316 			status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
1317 			goto fail;
1318 		}
1319 	}
1320 	sta->flags &= ~WLAN_STA_PREAUTH;
1321 	ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
1322 #ifdef CONFIG_IEEE80211R_AP
1323 	if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
1324 		sta->auth_alg = WLAN_AUTH_FT;
1325 		if (sta->wpa_sm == NULL)
1326 			sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
1327 							sta->addr, NULL);
1328 		if (sta->wpa_sm == NULL) {
1329 			wpa_printf(MSG_DEBUG,
1330 				   "FT: Failed to initialize WPA state machine");
1331 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1332 			goto fail;
1333 		}
1334 		wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
1335 				    rx_auth->auth_transaction, rx_auth->ies,
1336 				    rx_auth->ies_len,
1337 				    hostapd_notify_auth_ft_finish, hapd);
1338 		return;
1339 	}
1340 #endif /* CONFIG_IEEE80211R_AP */
1341 
1342 #ifdef CONFIG_FILS
1343 	if (rx_auth->auth_type == WLAN_AUTH_FILS_SK) {
1344 		sta->auth_alg = WLAN_AUTH_FILS_SK;
1345 		handle_auth_fils(hapd, sta, rx_auth->ies, rx_auth->ies_len,
1346 				 rx_auth->auth_type, rx_auth->auth_transaction,
1347 				 rx_auth->status_code,
1348 				 hostapd_notify_auth_fils_finish);
1349 		return;
1350 	}
1351 #endif /* CONFIG_FILS */
1352 
1353 fail:
1354 	hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
1355 			 status, resp_ies, resp_ies_len);
1356 }
1357 
1358 
1359 #ifndef NEED_AP_MLME
hostapd_action_rx(struct hostapd_data *hapd, struct rx_mgmt *drv_mgmt)1360 static void hostapd_action_rx(struct hostapd_data *hapd,
1361 			      struct rx_mgmt *drv_mgmt)
1362 {
1363 	struct ieee80211_mgmt *mgmt;
1364 	struct sta_info *sta;
1365 	size_t plen __maybe_unused;
1366 	u16 fc;
1367 	u8 *action __maybe_unused;
1368 
1369 	if (drv_mgmt->frame_len < IEEE80211_HDRLEN + 2 + 1)
1370 		return;
1371 
1372 	plen = drv_mgmt->frame_len - IEEE80211_HDRLEN;
1373 
1374 	mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
1375 	fc = le_to_host16(mgmt->frame_control);
1376 	if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
1377 		return; /* handled by the driver */
1378 
1379 	action = (u8 *) &mgmt->u.action.u;
1380 	wpa_printf(MSG_DEBUG, "RX_ACTION category %u action %u sa " MACSTR_SEC
1381 		   " da " MACSTR_SEC " plen %d",
1382 		   mgmt->u.action.category, *action,
1383 		   MAC2STR_SEC(mgmt->sa), MAC2STR_SEC(mgmt->da), (int) plen);
1384 
1385 	sta = ap_get_sta(hapd, mgmt->sa);
1386 	if (sta == NULL) {
1387 		wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
1388 		return;
1389 	}
1390 #ifdef CONFIG_IEEE80211R_AP
1391 	if (mgmt->u.action.category == WLAN_ACTION_FT) {
1392 		wpa_ft_action_rx(sta->wpa_sm, (u8 *) &mgmt->u.action, plen);
1393 		return;
1394 	}
1395 #endif /* CONFIG_IEEE80211R_AP */
1396 	if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY) {
1397 		ieee802_11_sa_query_action(hapd, mgmt, drv_mgmt->frame_len);
1398 		return;
1399 	}
1400 #ifdef CONFIG_WNM_AP
1401 	if (mgmt->u.action.category == WLAN_ACTION_WNM) {
1402 		ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
1403 		return;
1404 	}
1405 #endif /* CONFIG_WNM_AP */
1406 #ifdef CONFIG_FST
1407 	if (mgmt->u.action.category == WLAN_ACTION_FST && hapd->iface->fst) {
1408 		fst_rx_action(hapd->iface->fst, mgmt, drv_mgmt->frame_len);
1409 		return;
1410 	}
1411 #endif /* CONFIG_FST */
1412 #ifdef CONFIG_DPP
1413 	if (plen >= 2 + 4 &&
1414 	    mgmt->u.action.u.vs_public_action.action ==
1415 	    WLAN_PA_VENDOR_SPECIFIC &&
1416 	    WPA_GET_BE24(mgmt->u.action.u.vs_public_action.oui) ==
1417 	    OUI_WFA &&
1418 	    mgmt->u.action.u.vs_public_action.variable[0] ==
1419 	    DPP_OUI_TYPE) {
1420 		const u8 *pos, *end;
1421 
1422 		pos = mgmt->u.action.u.vs_public_action.oui;
1423 		end = drv_mgmt->frame + drv_mgmt->frame_len;
1424 		hostapd_dpp_rx_action(hapd, mgmt->sa, pos, end - pos,
1425 				      drv_mgmt->freq);
1426 		return;
1427 	}
1428 #endif /* CONFIG_DPP */
1429 }
1430 #endif /* NEED_AP_MLME */
1431 
1432 
1433 #ifdef NEED_AP_MLME
1434 
1435 #define HAPD_BROADCAST ((struct hostapd_data *) -1)
1436 
get_hapd_bssid(struct hostapd_iface *iface, const u8 *bssid)1437 static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
1438 					    const u8 *bssid)
1439 {
1440 	size_t i;
1441 
1442 	if (bssid == NULL)
1443 		return NULL;
1444 	if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
1445 	    bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
1446 		return HAPD_BROADCAST;
1447 
1448 	for (i = 0; i < iface->num_bss; i++) {
1449 		if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
1450 			return iface->bss[i];
1451 	}
1452 
1453 	return NULL;
1454 }
1455 
1456 
hostapd_rx_from_unknown_sta(struct hostapd_data *hapd, const u8 *bssid, const u8 *addr, int wds)1457 static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
1458 					const u8 *bssid, const u8 *addr,
1459 					int wds)
1460 {
1461 	hapd = get_hapd_bssid(hapd->iface, bssid);
1462 	if (hapd == NULL || hapd == HAPD_BROADCAST)
1463 		return;
1464 
1465 	ieee802_11_rx_from_unknown(hapd, addr, wds);
1466 }
1467 
1468 
hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)1469 static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
1470 {
1471 	struct hostapd_iface *iface = hapd->iface;
1472 	const struct ieee80211_hdr *hdr;
1473 	const u8 *bssid;
1474 	struct hostapd_frame_info fi;
1475 	int ret;
1476 
1477 #ifdef CONFIG_TESTING_OPTIONS
1478 	if (hapd->ext_mgmt_frame_handling) {
1479 		size_t hex_len = 2 * rx_mgmt->frame_len + 1;
1480 		char *hex = os_malloc(hex_len);
1481 
1482 		if (hex) {
1483 			wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
1484 					 rx_mgmt->frame_len);
1485 			wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
1486 			os_free(hex);
1487 		}
1488 		return 1;
1489 	}
1490 #endif /* CONFIG_TESTING_OPTIONS */
1491 
1492 	hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
1493 	bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
1494 	if (bssid == NULL)
1495 		return 0;
1496 
1497 	hapd = get_hapd_bssid(iface, bssid);
1498 	if (hapd == NULL) {
1499 		u16 fc = le_to_host16(hdr->frame_control);
1500 
1501 		/*
1502 		 * Drop frames to unknown BSSIDs except for Beacon frames which
1503 		 * could be used to update neighbor information.
1504 		 */
1505 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1506 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1507 			hapd = iface->bss[0];
1508 		else
1509 			return 0;
1510 	}
1511 
1512 	os_memset(&fi, 0, sizeof(fi));
1513 	fi.freq = rx_mgmt->freq;
1514 	fi.datarate = rx_mgmt->datarate;
1515 	fi.ssi_signal = rx_mgmt->ssi_signal;
1516 
1517 	if (hapd == HAPD_BROADCAST) {
1518 		size_t i;
1519 
1520 		ret = 0;
1521 		for (i = 0; i < iface->num_bss; i++) {
1522 			/* if bss is set, driver will call this function for
1523 			 * each bss individually. */
1524 			if (rx_mgmt->drv_priv &&
1525 			    (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
1526 				continue;
1527 
1528 			if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
1529 					    rx_mgmt->frame_len, &fi) > 0)
1530 				ret = 1;
1531 		}
1532 	} else
1533 		ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
1534 				      &fi);
1535 
1536 	random_add_randomness(&fi, sizeof(fi));
1537 
1538 	return ret;
1539 }
1540 
1541 
hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf, size_t len, u16 stype, int ok)1542 static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
1543 			       size_t len, u16 stype, int ok)
1544 {
1545 	struct ieee80211_hdr *hdr;
1546 	struct hostapd_data *orig_hapd = hapd;
1547 
1548 	hdr = (struct ieee80211_hdr *) buf;
1549 	hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
1550 	if (!hapd)
1551 		return;
1552 	if (hapd == HAPD_BROADCAST) {
1553 		if (stype != WLAN_FC_STYPE_ACTION || len <= 25 ||
1554 		    buf[24] != WLAN_ACTION_PUBLIC)
1555 			return;
1556 		hapd = get_hapd_bssid(orig_hapd->iface, hdr->addr2);
1557 		if (!hapd || hapd == HAPD_BROADCAST)
1558 			return;
1559 		/*
1560 		 * Allow processing of TX status for a Public Action frame that
1561 		 * used wildcard BBSID.
1562 		 */
1563 	}
1564 	ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
1565 }
1566 
1567 #endif /* NEED_AP_MLME */
1568 
1569 
hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)1570 static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
1571 {
1572 	struct sta_info *sta = ap_get_sta(hapd, addr);
1573 
1574 	if (sta)
1575 		return 0;
1576 
1577 	wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR_SEC
1578 		   " - adding a new STA", MAC2STR_SEC(addr));
1579 	sta = ap_sta_add(hapd, addr);
1580 	if (sta) {
1581 		hostapd_new_assoc_sta(hapd, sta, 0);
1582 	} else {
1583 		wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR_SEC,
1584 			   MAC2STR_SEC(addr));
1585 		return -1;
1586 	}
1587 
1588 	return 0;
1589 }
1590 
1591 
hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src, const u8 *data, size_t data_len)1592 static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
1593 				   const u8 *data, size_t data_len)
1594 {
1595 	struct hostapd_iface *iface = hapd->iface;
1596 	struct sta_info *sta;
1597 	size_t j;
1598 
1599 	for (j = 0; j < iface->num_bss; j++) {
1600 		sta = ap_get_sta(iface->bss[j], src);
1601 		if (sta && sta->flags & WLAN_STA_ASSOC) {
1602 			hapd = iface->bss[j];
1603 			break;
1604 		}
1605 	}
1606 
1607 	ieee802_1x_receive(hapd, src, data, data_len);
1608 }
1609 
1610 #endif /* HOSTAPD */
1611 
1612 
1613 static struct hostapd_channel_data *
hostapd_get_mode_chan(struct hostapd_hw_modes *mode, unsigned int freq)1614 hostapd_get_mode_chan(struct hostapd_hw_modes *mode, unsigned int freq)
1615 {
1616 	int i;
1617 	struct hostapd_channel_data *chan;
1618 
1619 	for (i = 0; i < mode->num_channels; i++) {
1620 		chan = &mode->channels[i];
1621 		if ((unsigned int) chan->freq == freq)
1622 			return chan;
1623 	}
1624 
1625 	return NULL;
1626 }
1627 
1628 
hostapd_get_mode_channel( struct hostapd_iface *iface, unsigned int freq)1629 static struct hostapd_channel_data * hostapd_get_mode_channel(
1630 	struct hostapd_iface *iface, unsigned int freq)
1631 {
1632 	int i;
1633 	struct hostapd_channel_data *chan;
1634 
1635 	for (i = 0; i < iface->num_hw_features; i++) {
1636 		if (hostapd_hw_skip_mode(iface, &iface->hw_features[i]))
1637 			continue;
1638 		chan = hostapd_get_mode_chan(&iface->hw_features[i], freq);
1639 		if (chan)
1640 			return chan;
1641 	}
1642 
1643 	return NULL;
1644 }
1645 
1646 
hostapd_update_nf(struct hostapd_iface *iface, struct hostapd_channel_data *chan, struct freq_survey *survey)1647 static void hostapd_update_nf(struct hostapd_iface *iface,
1648 			      struct hostapd_channel_data *chan,
1649 			      struct freq_survey *survey)
1650 {
1651 	if (!iface->chans_surveyed) {
1652 		chan->min_nf = survey->nf;
1653 		iface->lowest_nf = survey->nf;
1654 	} else {
1655 		if (dl_list_empty(&chan->survey_list))
1656 			chan->min_nf = survey->nf;
1657 		else if (survey->nf < chan->min_nf)
1658 			chan->min_nf = survey->nf;
1659 		if (survey->nf < iface->lowest_nf)
1660 			iface->lowest_nf = survey->nf;
1661 	}
1662 }
1663 
1664 
hostapd_single_channel_get_survey(struct hostapd_iface *iface, struct survey_results *survey_res)1665 static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
1666 					      struct survey_results *survey_res)
1667 {
1668 	struct hostapd_channel_data *chan;
1669 	struct freq_survey *survey;
1670 	u64 divisor, dividend;
1671 
1672 	survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
1673 			       list);
1674 	if (!survey || !survey->freq)
1675 		return;
1676 
1677 	chan = hostapd_get_mode_channel(iface, survey->freq);
1678 	if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
1679 		return;
1680 
1681 	wpa_printf(MSG_DEBUG,
1682 		   "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
1683 		   survey->freq,
1684 		   (unsigned long int) survey->channel_time,
1685 		   (unsigned long int) survey->channel_time_busy);
1686 
1687 	if (survey->channel_time > iface->last_channel_time &&
1688 	    survey->channel_time > survey->channel_time_busy) {
1689 		dividend = survey->channel_time_busy -
1690 			iface->last_channel_time_busy;
1691 		divisor = survey->channel_time - iface->last_channel_time;
1692 
1693 		iface->channel_utilization = dividend * 255 / divisor;
1694 		wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
1695 			   iface->channel_utilization);
1696 	}
1697 	iface->last_channel_time = survey->channel_time;
1698 	iface->last_channel_time_busy = survey->channel_time_busy;
1699 }
1700 
1701 
hostapd_event_get_survey(struct hostapd_iface *iface, struct survey_results *survey_results)1702 void hostapd_event_get_survey(struct hostapd_iface *iface,
1703 			      struct survey_results *survey_results)
1704 {
1705 	struct freq_survey *survey, *tmp;
1706 	struct hostapd_channel_data *chan;
1707 
1708 	if (dl_list_empty(&survey_results->survey_list)) {
1709 		wpa_printf(MSG_DEBUG, "No survey data received");
1710 		return;
1711 	}
1712 
1713 	if (survey_results->freq_filter) {
1714 		hostapd_single_channel_get_survey(iface, survey_results);
1715 		return;
1716 	}
1717 
1718 	dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1719 			      struct freq_survey, list) {
1720 		chan = hostapd_get_mode_channel(iface, survey->freq);
1721 		if (!chan)
1722 			continue;
1723 		if (chan->flag & HOSTAPD_CHAN_DISABLED)
1724 			continue;
1725 
1726 		dl_list_del(&survey->list);
1727 		dl_list_add_tail(&chan->survey_list, &survey->list);
1728 
1729 		hostapd_update_nf(iface, chan, survey);
1730 
1731 		iface->chans_surveyed++;
1732 	}
1733 }
1734 
1735 
1736 #ifdef HOSTAPD
1737 #ifdef NEED_AP_MLME
1738 
hostapd_event_iface_unavailable(struct hostapd_data *hapd)1739 static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1740 {
1741 	wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1742 		   hapd->conf->iface);
1743 
1744 	if (hapd->csa_in_progress) {
1745 		wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1746 			   hapd->conf->iface);
1747 		hostapd_switch_channel_fallback(hapd->iface,
1748 						&hapd->cs_freq_params);
1749 	}
1750 }
1751 
1752 
hostapd_event_dfs_radar_detected(struct hostapd_data *hapd, struct dfs_event *radar)1753 static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1754 					     struct dfs_event *radar)
1755 {
1756 	wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
1757 	hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
1758 				   radar->chan_offset, radar->chan_width,
1759 				   radar->cf1, radar->cf2);
1760 }
1761 
1762 
hostapd_event_dfs_pre_cac_expired(struct hostapd_data *hapd, struct dfs_event *radar)1763 static void hostapd_event_dfs_pre_cac_expired(struct hostapd_data *hapd,
1764 					      struct dfs_event *radar)
1765 {
1766 	wpa_printf(MSG_DEBUG, "DFS Pre-CAC expired on %d MHz", radar->freq);
1767 	hostapd_dfs_pre_cac_expired(hapd->iface, radar->freq, radar->ht_enabled,
1768 				    radar->chan_offset, radar->chan_width,
1769 				    radar->cf1, radar->cf2);
1770 }
1771 
1772 
hostapd_event_dfs_cac_finished(struct hostapd_data *hapd, struct dfs_event *radar)1773 static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1774 					   struct dfs_event *radar)
1775 {
1776 	wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
1777 	hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
1778 				 radar->chan_offset, radar->chan_width,
1779 				 radar->cf1, radar->cf2);
1780 }
1781 
1782 
hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd, struct dfs_event *radar)1783 static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1784 					  struct dfs_event *radar)
1785 {
1786 	wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
1787 	hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
1788 				 radar->chan_offset, radar->chan_width,
1789 				 radar->cf1, radar->cf2);
1790 }
1791 
1792 
hostapd_event_dfs_nop_finished(struct hostapd_data *hapd, struct dfs_event *radar)1793 static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1794 					   struct dfs_event *radar)
1795 {
1796 	wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
1797 	hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
1798 				 radar->chan_offset, radar->chan_width,
1799 				 radar->cf1, radar->cf2);
1800 }
1801 
1802 
hostapd_event_dfs_cac_started(struct hostapd_data *hapd, struct dfs_event *radar)1803 static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1804 					  struct dfs_event *radar)
1805 {
1806 	wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1807 	hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1808 			      radar->chan_offset, radar->chan_width,
1809 			      radar->cf1, radar->cf2);
1810 }
1811 
1812 #endif /* NEED_AP_MLME */
1813 
1814 
hostapd_event_wds_sta_interface_status(struct hostapd_data *hapd, int istatus, const char *ifname, const u8 *addr)1815 static void hostapd_event_wds_sta_interface_status(struct hostapd_data *hapd,
1816 						   int istatus,
1817 						   const char *ifname,
1818 						   const u8 *addr)
1819 {
1820 	struct sta_info *sta = ap_get_sta(hapd, addr);
1821 
1822 	if (sta) {
1823 		os_free(sta->ifname_wds);
1824 		if (istatus == INTERFACE_ADDED)
1825 			sta->ifname_wds = os_strdup(ifname);
1826 		else
1827 			sta->ifname_wds = NULL;
1828 	}
1829 
1830 	wpa_msg(hapd->msg_ctx, MSG_INFO, "%sifname=%s sta_addr=" MACSTR,
1831 		istatus == INTERFACE_ADDED ?
1832 		WDS_STA_INTERFACE_ADDED : WDS_STA_INTERFACE_REMOVED,
1833 		ifname, MAC2STR(addr));
1834 }
1835 
1836 
1837 #ifdef CONFIG_OWE
hostapd_notif_update_dh_ie(struct hostapd_data *hapd, const u8 *peer, const u8 *ie, size_t ie_len)1838 static int hostapd_notif_update_dh_ie(struct hostapd_data *hapd,
1839 				      const u8 *peer, const u8 *ie,
1840 				      size_t ie_len)
1841 {
1842 	u16 status;
1843 	struct sta_info *sta;
1844 	struct ieee802_11_elems elems;
1845 
1846 	if (!hapd || !hapd->wpa_auth) {
1847 		wpa_printf(MSG_DEBUG, "OWE: Invalid hapd context");
1848 		return -1;
1849 	}
1850 	if (!peer) {
1851 		wpa_printf(MSG_DEBUG, "OWE: Peer unknown");
1852 		return -1;
1853 	}
1854 	if (!(hapd->conf->wpa_key_mgmt & WPA_KEY_MGMT_OWE)) {
1855 		wpa_printf(MSG_DEBUG, "OWE: No OWE AKM configured");
1856 		status = WLAN_STATUS_AKMP_NOT_VALID;
1857 		goto err;
1858 	}
1859 	if (ieee802_11_parse_elems(ie, ie_len, &elems, 1) == ParseFailed) {
1860 		wpa_printf(MSG_DEBUG, "OWE: Failed to parse OWE IE for "
1861 			   MACSTR_SEC, MAC2STR_SEC(peer));
1862 		status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1863 		goto err;
1864 	}
1865 	status = owe_validate_request(hapd, peer, elems.rsn_ie,
1866 				      elems.rsn_ie_len,
1867 				      elems.owe_dh, elems.owe_dh_len);
1868 	if (status != WLAN_STATUS_SUCCESS)
1869 		goto err;
1870 
1871 	sta = ap_get_sta(hapd, peer);
1872 	if (sta) {
1873 		ap_sta_no_session_timeout(hapd, sta);
1874 		accounting_sta_stop(hapd, sta);
1875 
1876 		/*
1877 		 * Make sure that the previously registered inactivity timer
1878 		 * will not remove the STA immediately.
1879 		 */
1880 		sta->timeout_next = STA_NULLFUNC;
1881 	} else {
1882 		sta = ap_sta_add(hapd, peer);
1883 		if (!sta) {
1884 			status = WLAN_STATUS_UNSPECIFIED_FAILURE;
1885 			goto err;
1886 		}
1887 	}
1888 	sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
1889 
1890 	status = owe_process_rsn_ie(hapd, sta, elems.rsn_ie,
1891 				    elems.rsn_ie_len, elems.owe_dh,
1892 				    elems.owe_dh_len);
1893 	if (status != WLAN_STATUS_SUCCESS)
1894 		ap_free_sta(hapd, sta);
1895 
1896 	return 0;
1897 err:
1898 	hostapd_drv_update_dh_ie(hapd, peer, status, NULL, 0);
1899 	return 0;
1900 }
1901 #endif /* CONFIG_OWE */
1902 
1903 
wpa_supplicant_event_hapd(void *ctx, enum wpa_event_type event, union wpa_event_data *data)1904 void wpa_supplicant_event_hapd(void *ctx, enum wpa_event_type event,
1905 			  union wpa_event_data *data)
1906 {
1907 	struct hostapd_data *hapd = ctx;
1908 #ifndef CONFIG_NO_STDOUT_DEBUG
1909 	int level = MSG_DEBUG;
1910 
1911 	if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
1912 	    data->rx_mgmt.frame_len >= 24) {
1913 		const struct ieee80211_hdr *hdr;
1914 		u16 fc;
1915 
1916 		hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1917 		fc = le_to_host16(hdr->frame_control);
1918 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1919 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1920 			level = MSG_EXCESSIVE;
1921 		if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1922 		    WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1923 			level = MSG_EXCESSIVE;
1924 	}
1925 
1926 	wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1927 		event_to_string(event), event);
1928 #endif /* CONFIG_NO_STDOUT_DEBUG */
1929 
1930 	switch (event) {
1931 	case EVENT_MICHAEL_MIC_FAILURE:
1932 		michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1933 		break;
1934 	case EVENT_SCAN_RESULTS:
1935 		if (hapd->iface->scan_cb)
1936 			hapd->iface->scan_cb(hapd->iface);
1937 		break;
1938 	case EVENT_WPS_BUTTON_PUSHED:
1939 		hostapd_wps_button_pushed(hapd, NULL);
1940 		break;
1941 #ifdef NEED_AP_MLME
1942 	case EVENT_TX_STATUS:
1943 		switch (data->tx_status.type) {
1944 		case WLAN_FC_TYPE_MGMT:
1945 			hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1946 					   data->tx_status.data_len,
1947 					   data->tx_status.stype,
1948 					   data->tx_status.ack);
1949 			break;
1950 		case WLAN_FC_TYPE_DATA:
1951 			hostapd_tx_status(hapd, data->tx_status.dst,
1952 					  data->tx_status.data,
1953 					  data->tx_status.data_len,
1954 					  data->tx_status.ack);
1955 			break;
1956 		}
1957 		break;
1958 	case EVENT_EAPOL_TX_STATUS:
1959 		hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1960 					data->eapol_tx_status.data,
1961 					data->eapol_tx_status.data_len,
1962 					data->eapol_tx_status.ack);
1963 		break;
1964 	case EVENT_DRIVER_CLIENT_POLL_OK:
1965 		hostapd_client_poll_ok(hapd, data->client_poll.addr);
1966 		break;
1967 	case EVENT_RX_FROM_UNKNOWN:
1968 		hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1969 					    data->rx_from_unknown.addr,
1970 					    data->rx_from_unknown.wds);
1971 		break;
1972 #endif /* NEED_AP_MLME */
1973 	case EVENT_RX_MGMT:
1974 		if (!data->rx_mgmt.frame)
1975 			break;
1976 #ifdef NEED_AP_MLME
1977 		hostapd_mgmt_rx(hapd, &data->rx_mgmt);
1978 #else /* NEED_AP_MLME */
1979 		hostapd_action_rx(hapd, &data->rx_mgmt);
1980 #endif /* NEED_AP_MLME */
1981 		break;
1982 	case EVENT_RX_PROBE_REQ:
1983 		if (data->rx_probe_req.sa == NULL ||
1984 		    data->rx_probe_req.ie == NULL)
1985 			break;
1986 		hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
1987 				     data->rx_probe_req.da,
1988 				     data->rx_probe_req.bssid,
1989 				     data->rx_probe_req.ie,
1990 				     data->rx_probe_req.ie_len,
1991 				     data->rx_probe_req.ssi_signal);
1992 		break;
1993 	case EVENT_NEW_STA:
1994 		hostapd_event_new_sta(hapd, data->new_sta.addr);
1995 		break;
1996 	case EVENT_EAPOL_RX:
1997 		hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1998 				       data->eapol_rx.data,
1999 				       data->eapol_rx.data_len);
2000 		break;
2001 	case EVENT_ASSOC:
2002 		if (!data)
2003 			return;
2004 		hostapd_notif_assoc(hapd, data->assoc_info.addr,
2005 				    data->assoc_info.req_ies,
2006 				    data->assoc_info.req_ies_len,
2007 				    data->assoc_info.reassoc);
2008 		break;
2009 #ifdef CONFIG_OWE
2010 	case EVENT_UPDATE_DH:
2011 		if (!data)
2012 			return;
2013 		hostapd_notif_update_dh_ie(hapd, data->update_dh.peer,
2014 					   data->update_dh.ie,
2015 					   data->update_dh.ie_len);
2016 		break;
2017 #endif /* CONFIG_OWE */
2018 	case EVENT_DISASSOC:
2019 		if (data)
2020 			hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
2021 		break;
2022 	case EVENT_DEAUTH:
2023 		if (data)
2024 			hostapd_notif_disassoc(hapd, data->deauth_info.addr);
2025 		break;
2026 	case EVENT_STATION_LOW_ACK:
2027 		if (!data)
2028 			break;
2029 		hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
2030 		break;
2031 	case EVENT_AUTH:
2032 		hostapd_notif_auth(hapd, &data->auth);
2033 		break;
2034 	case EVENT_CH_SWITCH_STARTED:
2035 	case EVENT_CH_SWITCH:
2036 		if (!data)
2037 			break;
2038 		hostapd_event_ch_switch(hapd, data->ch_switch.freq,
2039 					data->ch_switch.ht_enabled,
2040 					data->ch_switch.ch_offset,
2041 					data->ch_switch.ch_width,
2042 					data->ch_switch.cf1,
2043 					data->ch_switch.cf2,
2044 					event == EVENT_CH_SWITCH);
2045 		break;
2046 	case EVENT_CONNECT_FAILED_REASON:
2047 		if (!data)
2048 			break;
2049 		hostapd_event_connect_failed_reason(
2050 			hapd, data->connect_failed_reason.addr,
2051 			data->connect_failed_reason.code);
2052 		break;
2053 	case EVENT_SURVEY:
2054 		hostapd_event_get_survey(hapd->iface, &data->survey_results);
2055 		break;
2056 #ifdef NEED_AP_MLME
2057 	case EVENT_INTERFACE_UNAVAILABLE:
2058 		hostapd_event_iface_unavailable(hapd);
2059 		break;
2060 	case EVENT_DFS_RADAR_DETECTED:
2061 		if (!data)
2062 			break;
2063 		hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
2064 		break;
2065 	case EVENT_DFS_PRE_CAC_EXPIRED:
2066 		if (!data)
2067 			break;
2068 		hostapd_event_dfs_pre_cac_expired(hapd, &data->dfs_event);
2069 		break;
2070 	case EVENT_DFS_CAC_FINISHED:
2071 		if (!data)
2072 			break;
2073 		hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
2074 		break;
2075 	case EVENT_DFS_CAC_ABORTED:
2076 		if (!data)
2077 			break;
2078 		hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
2079 		break;
2080 	case EVENT_DFS_NOP_FINISHED:
2081 		if (!data)
2082 			break;
2083 		hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
2084 		break;
2085 	case EVENT_CHANNEL_LIST_CHANGED:
2086 		/* channel list changed (regulatory?), update channel list */
2087 		/* TODO: check this. hostapd_get_hw_features() initializes
2088 		 * too much stuff. */
2089 		/* hostapd_get_hw_features(hapd->iface); */
2090 		hostapd_channel_list_updated(
2091 			hapd->iface, data->channel_list_changed.initiator);
2092 		break;
2093 	case EVENT_DFS_CAC_STARTED:
2094 		if (!data)
2095 			break;
2096 		hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
2097 		break;
2098 #endif /* NEED_AP_MLME */
2099 	case EVENT_INTERFACE_ENABLED:
2100 		wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
2101 		if (hapd->disabled && hapd->started) {
2102 			hapd->disabled = 0;
2103 			/*
2104 			 * Try to re-enable interface if the driver stopped it
2105 			 * when the interface got disabled.
2106 			 */
2107 			if (hapd->wpa_auth)
2108 				wpa_auth_reconfig_group_keys(hapd->wpa_auth);
2109 			else
2110 				hostapd_reconfig_encryption(hapd);
2111 			hapd->reenable_beacon = 1;
2112 			ieee802_11_set_beacon(hapd);
2113 #ifdef NEED_AP_MLME
2114 		} else if (hapd->disabled && hapd->iface->cac_started) {
2115 			wpa_printf(MSG_DEBUG, "DFS: restarting pending CAC");
2116 			hostapd_handle_dfs(hapd->iface);
2117 #endif /* NEED_AP_MLME */
2118 		}
2119 		break;
2120 	case EVENT_INTERFACE_DISABLED:
2121 		hostapd_free_stas(hapd);
2122 		wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
2123 		hapd->disabled = 1;
2124 		break;
2125 #ifdef CONFIG_ACS
2126 	case EVENT_ACS_CHANNEL_SELECTED:
2127 		hostapd_acs_channel_selected(hapd,
2128 					     &data->acs_selected_channels);
2129 		break;
2130 #endif /* CONFIG_ACS */
2131 	case EVENT_STATION_OPMODE_CHANGED:
2132 		hostapd_event_sta_opmode_changed(hapd, data->sta_opmode.addr,
2133 						 data->sta_opmode.smps_mode,
2134 						 data->sta_opmode.chan_width,
2135 						 data->sta_opmode.rx_nss);
2136 		break;
2137 	case EVENT_WDS_STA_INTERFACE_STATUS:
2138 		hostapd_event_wds_sta_interface_status(
2139 			hapd, data->wds_sta_interface.istatus,
2140 			data->wds_sta_interface.ifname,
2141 			data->wds_sta_interface.sta_addr);
2142 		break;
2143 	default:
2144 		wpa_printf(MSG_DEBUG, "Unknown event %d", event);
2145 		break;
2146 	}
2147 }
2148 
2149 
wpa_supplicant_event_global_hapd(void *ctx, enum wpa_event_type event, union wpa_event_data *data)2150 void wpa_supplicant_event_global_hapd(void *ctx, enum wpa_event_type event,
2151 				 union wpa_event_data *data)
2152 {
2153 	struct hapd_interfaces *interfaces = ctx;
2154 	struct hostapd_data *hapd;
2155 
2156 	if (event != EVENT_INTERFACE_STATUS)
2157 		return;
2158 
2159 	hapd = hostapd_get_iface(interfaces, data->interface_status.ifname);
2160 	if (hapd && hapd->driver && hapd->driver->get_ifindex &&
2161 	    hapd->drv_priv) {
2162 		unsigned int ifindex;
2163 
2164 		ifindex = hapd->driver->get_ifindex(hapd->drv_priv);
2165 		if (ifindex != data->interface_status.ifindex) {
2166 			wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
2167 				"interface status ifindex %d mismatch (%d)",
2168 				ifindex, data->interface_status.ifindex);
2169 			return;
2170 		}
2171 	}
2172 	if (hapd)
2173 		wpa_supplicant_event_hapd(hapd, event, data);
2174 }
2175 
2176 #endif /* HOSTAPD */
2177