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