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