• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2017, 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 "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/sae.h"
16 #include "common/dpp.h"
17 #include "radius/radius.h"
18 #include "radius/radius_client.h"
19 #include "p2p/p2p.h"
20 #include "fst/fst.h"
21 #include "crypto/crypto.h"
22 #include "hostapd.h"
23 #include "accounting.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11.h"
26 #include "ieee802_11_auth.h"
27 #include "wpa_auth.h"
28 #include "preauth_auth.h"
29 #include "ap_config.h"
30 #include "beacon.h"
31 #include "ap_mlme.h"
32 #include "vlan_init.h"
33 #include "p2p_hostapd.h"
34 #include "ap_drv_ops.h"
35 #include "gas_serv.h"
36 #include "wnm_ap.h"
37 #include "mbo_ap.h"
38 #include "ndisc_snoop.h"
39 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
40 #include "hm_miracast_sink.h"
41 #endif
42 #include "sta_info.h"
43 #include "vlan.h"
44 #include "wps_hostapd.h"
45 #ifdef CONFIG_LIBWPA_VENDOR
46 #include "hostapd_client.h"
47 #include "wpa_client.h"
48 #endif
49 #ifdef CONFIG_VENDOR_EXT
50 #include "vendor_ext.h"
51 #endif
52 #if defined(CONFIG_LIBWPA_VENDOR) || defined(CONFIG_VENDOR_EXT)
53 #include "wpa_supplicant_i.h"
54 #endif
55 #ifdef CONFIG_P2P_CHR
56 #include "wpa_hw_p2p_chr.h"
57 #endif
58 
59 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
60 				       struct sta_info *sta);
61 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
62 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
63 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
64 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
65 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
66 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
67 static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx);
68 
ap_for_each_sta(struct hostapd_data * hapd,int (* cb)(struct hostapd_data * hapd,struct sta_info * sta,void * ctx),void * ctx)69 int ap_for_each_sta(struct hostapd_data *hapd,
70 		    int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
71 			      void *ctx),
72 		    void *ctx)
73 {
74 	struct sta_info *sta;
75 
76 	for (sta = hapd->sta_list; sta; sta = sta->next) {
77 		if (cb(hapd, sta, ctx))
78 			return 1;
79 	}
80 
81 	return 0;
82 }
83 
84 
ap_get_sta(struct hostapd_data * hapd,const u8 * sta)85 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
86 {
87 	struct sta_info *s;
88 
89 	s = hapd->sta_hash[STA_HASH(sta)];
90 	while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
91 		s = s->hnext;
92 	return s;
93 }
94 
95 
96 #ifdef CONFIG_P2P
ap_get_sta_p2p(struct hostapd_data * hapd,const u8 * addr)97 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
98 {
99 	struct sta_info *sta;
100 
101 	for (sta = hapd->sta_list; sta; sta = sta->next) {
102 		const u8 *p2p_dev_addr;
103 
104 		if (sta->p2p_ie == NULL)
105 			continue;
106 
107 		p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
108 		if (p2p_dev_addr == NULL)
109 			continue;
110 
111 		if (ether_addr_equal(p2p_dev_addr, addr))
112 			return sta;
113 	}
114 
115 	return NULL;
116 }
117 #endif /* CONFIG_P2P */
118 
119 
ap_sta_list_del(struct hostapd_data * hapd,struct sta_info * sta)120 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
121 {
122 	struct sta_info *tmp;
123 
124 	if (hapd->sta_list == sta) {
125 		hapd->sta_list = sta->next;
126 		return;
127 	}
128 
129 	tmp = hapd->sta_list;
130 	while (tmp != NULL && tmp->next != sta)
131 		tmp = tmp->next;
132 	if (tmp == NULL) {
133 		wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR_SEC " from "
134 			   "list.", MAC2STR_SEC(sta->addr));
135 	} else
136 		tmp->next = sta->next;
137 }
138 
139 
ap_sta_hash_add(struct hostapd_data * hapd,struct sta_info * sta)140 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
141 {
142 	sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
143 	hapd->sta_hash[STA_HASH(sta->addr)] = sta;
144 }
145 
146 
ap_sta_hash_del(struct hostapd_data * hapd,struct sta_info * sta)147 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
148 {
149 	struct sta_info *s;
150 
151 	s = hapd->sta_hash[STA_HASH(sta->addr)];
152 	if (s == NULL) return;
153 	if (os_memcmp(s->addr, sta->addr, 6) == 0) {
154 		hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
155 		return;
156 	}
157 
158 	while (s->hnext != NULL &&
159 	       !ether_addr_equal(s->hnext->addr, sta->addr))
160 		s = s->hnext;
161 	if (s->hnext != NULL)
162 		s->hnext = s->hnext->hnext;
163 	else
164 		wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR_SEC
165 			   " from hash table", MAC2STR_SEC(sta->addr));
166 }
167 
168 
ap_sta_ip6addr_del(struct hostapd_data * hapd,struct sta_info * sta)169 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
170 {
171 	sta_ip6addr_del(hapd, sta);
172 }
173 
174 
175 #ifdef CONFIG_PASN
176 
ap_free_sta_pasn(struct hostapd_data * hapd,struct sta_info * sta)177 void ap_free_sta_pasn(struct hostapd_data *hapd, struct sta_info *sta)
178 {
179 	if (sta->pasn) {
180 		wpa_printf(MSG_DEBUG, "PASN: Free PASN context: " MACSTR_SEC,
181 			   MAC2STR_SEC(sta->addr));
182 
183 		if (sta->pasn->ecdh)
184 			crypto_ecdh_deinit(sta->pasn->ecdh);
185 
186 		wpabuf_free(sta->pasn->secret);
187 		sta->pasn->secret = NULL;
188 
189 #ifdef CONFIG_SAE
190 		sae_clear_data(&sta->pasn->sae);
191 #endif /* CONFIG_SAE */
192 
193 #ifdef CONFIG_FILS
194 		/* In practice this pointer should be NULL */
195 		wpabuf_free(sta->pasn->fils.erp_resp);
196 		sta->pasn->fils.erp_resp = NULL;
197 #endif /* CONFIG_FILS */
198 
199 		pasn_data_deinit(sta->pasn);
200 		sta->pasn = NULL;
201 	}
202 }
203 
204 #endif /* CONFIG_PASN */
205 
206 
__ap_free_sta(struct hostapd_data * hapd,struct sta_info * sta)207 static void __ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
208 {
209 #ifdef CONFIG_IEEE80211BE
210 	if (hostapd_sta_is_link_sta(hapd, sta) &&
211 	    !hostapd_drv_link_sta_remove(hapd, sta->addr))
212 		return;
213 #endif /* CONFIG_IEEE80211BE */
214 
215 	hostapd_drv_sta_remove(hapd, sta->addr);
216 }
217 
218 
219 #ifdef CONFIG_IEEE80211BE
clear_wpa_sm_for_each_partner_link(struct hostapd_data * hapd,struct sta_info * psta)220 static void clear_wpa_sm_for_each_partner_link(struct hostapd_data *hapd,
221 					       struct sta_info *psta)
222 {
223 	struct sta_info *lsta;
224 	struct hostapd_data *lhapd;
225 
226 	if (!ap_sta_is_mld(hapd, psta))
227 		return;
228 
229 	for_each_mld_link(lhapd, hapd) {
230 		if (lhapd == hapd)
231 			continue;
232 
233 		lsta = ap_get_sta(lhapd, psta->addr);
234 		if (lsta)
235 			lsta->wpa_sm = NULL;
236 	}
237 }
238 #endif /* CONFIG_IEEE80211BE */
239 
240 
ap_free_sta(struct hostapd_data * hapd,struct sta_info * sta)241 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
242 {
243 	int set_beacon = 0;
244 
245 	accounting_sta_stop(hapd, sta);
246 
247 	/* just in case */
248 	ap_sta_set_authorized(hapd, sta, 0);
249 	hostapd_set_sta_flags(hapd, sta);
250 
251 	if ((sta->flags & WLAN_STA_WDS) ||
252 	    (sta->flags & WLAN_STA_MULTI_AP &&
253 	     (hapd->conf->multi_ap & BACKHAUL_BSS) &&
254 	     hapd->conf->wds_sta &&
255 	     !(sta->flags & WLAN_STA_WPS)))
256 		hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
257 
258 	if (sta->ipaddr)
259 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
260 	ap_sta_ip6addr_del(hapd, sta);
261 
262 	if (!hapd->iface->driver_ap_teardown &&
263 	    !(sta->flags & WLAN_STA_PREAUTH)) {
264 		__ap_free_sta(hapd, sta);
265 		sta->added_unassoc = 0;
266 	}
267 
268 	ap_sta_hash_del(hapd, sta);
269 	ap_sta_list_del(hapd, sta);
270 
271 	if (sta->aid > 0)
272 		hapd->sta_aid[(sta->aid - 1) / 32] &=
273 			~BIT((sta->aid - 1) % 32);
274 
275 	hapd->num_sta--;
276 	if (sta->nonerp_set) {
277 		sta->nonerp_set = 0;
278 		hapd->iface->num_sta_non_erp--;
279 		if (hapd->iface->num_sta_non_erp == 0)
280 			set_beacon++;
281 	}
282 
283 	if (sta->no_short_slot_time_set) {
284 		sta->no_short_slot_time_set = 0;
285 		hapd->iface->num_sta_no_short_slot_time--;
286 		if (hapd->iface->current_mode &&
287 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
288 		    && hapd->iface->num_sta_no_short_slot_time == 0)
289 			set_beacon++;
290 	}
291 
292 	if (sta->no_short_preamble_set) {
293 		sta->no_short_preamble_set = 0;
294 		hapd->iface->num_sta_no_short_preamble--;
295 		if (hapd->iface->current_mode &&
296 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
297 		    && hapd->iface->num_sta_no_short_preamble == 0)
298 			set_beacon++;
299 	}
300 
301 	if (sta->no_ht_gf_set) {
302 		sta->no_ht_gf_set = 0;
303 		hapd->iface->num_sta_ht_no_gf--;
304 	}
305 
306 	if (sta->no_ht_set) {
307 		sta->no_ht_set = 0;
308 		hapd->iface->num_sta_no_ht--;
309 	}
310 
311 	if (sta->ht_20mhz_set) {
312 		sta->ht_20mhz_set = 0;
313 		hapd->iface->num_sta_ht_20mhz--;
314 	}
315 
316 #ifdef CONFIG_TAXONOMY
317 	wpabuf_free(sta->probe_ie_taxonomy);
318 	sta->probe_ie_taxonomy = NULL;
319 	wpabuf_free(sta->assoc_ie_taxonomy);
320 	sta->assoc_ie_taxonomy = NULL;
321 #endif /* CONFIG_TAXONOMY */
322 
323 	ht40_intolerant_remove(hapd->iface, sta);
324 
325 #ifdef CONFIG_P2P
326 	if (sta->no_p2p_set) {
327 		sta->no_p2p_set = 0;
328 		hapd->num_sta_no_p2p--;
329 		if (hapd->num_sta_no_p2p == 0)
330 			hostapd_p2p_non_p2p_sta_disconnected(hapd);
331 	}
332 #endif /* CONFIG_P2P */
333 
334 #ifdef NEED_AP_MLME
335 	if (hostapd_ht_operation_update(hapd->iface) > 0)
336 		set_beacon++;
337 #endif /* NEED_AP_MLME */
338 
339 #ifdef CONFIG_MESH
340 	if (hapd->mesh_sta_free_cb)
341 		hapd->mesh_sta_free_cb(hapd, sta);
342 #endif /* CONFIG_MESH */
343 
344 	if (set_beacon)
345 		ieee802_11_update_beacons(hapd->iface);
346 
347 	wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR_SEC,
348 		   __func__, MAC2STR_SEC(sta->addr));
349 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
350 	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
351 	eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
352 	ap_sta_clear_disconnect_timeouts(hapd, sta);
353 	sae_clear_retransmit_timer(hapd, sta);
354 
355 	ieee802_1x_free_station(hapd, sta);
356 
357 #ifdef CONFIG_IEEE80211BE
358 	if (!ap_sta_is_mld(hapd, sta) ||
359 	    hapd->mld_link_id == sta->mld_assoc_link_id) {
360 		wpa_auth_sta_deinit(sta->wpa_sm);
361 		/* Remove references from partner links. */
362 		clear_wpa_sm_for_each_partner_link(hapd, sta);
363 	}
364 
365 	/* Release group references in case non-association link STA is removed
366 	 * before association link STA */
367 	if (hostapd_sta_is_link_sta(hapd, sta))
368 		wpa_release_link_auth_ref(sta->wpa_sm, hapd->mld_link_id);
369 #else /* CONFIG_IEEE80211BE */
370 	wpa_auth_sta_deinit(sta->wpa_sm);
371 #endif /* CONFIG_IEEE80211BE */
372 
373 	rsn_preauth_free_station(hapd, sta);
374 #ifndef CONFIG_NO_RADIUS
375 	if (hapd->radius)
376 		radius_client_flush_auth(hapd->radius, sta->addr);
377 #endif /* CONFIG_NO_RADIUS */
378 
379 #ifndef CONFIG_NO_VLAN
380 	/*
381 	 * sta->wpa_sm->group needs to be released before so that
382 	 * vlan_remove_dynamic() can check that no stations are left on the
383 	 * AP_VLAN netdev.
384 	 */
385 	if (sta->vlan_id)
386 		vlan_remove_dynamic(hapd, sta->vlan_id);
387 	if (sta->vlan_id_bound) {
388 		/*
389 		 * Need to remove the STA entry before potentially removing the
390 		 * VLAN.
391 		 */
392 		if (hapd->iface->driver_ap_teardown &&
393 		    !(sta->flags & WLAN_STA_PREAUTH) &&
394 			!(sta->flags & WLAN_STA_P2PGO_WPS_NO_SECOND_DISASSOC)) {
395 			wpa_printf(MSG_INFO,
396 				"WLAN_STA_P2PGO_WPS_NO_SECOND_DISASSOC is not set, not in P2P_GO_WSC ending process");
397 			hostapd_drv_sta_remove(hapd, sta->addr);
398 			sta->added_unassoc = 0;
399 		}
400 		vlan_remove_dynamic(hapd, sta->vlan_id_bound);
401 	}
402 #endif /* CONFIG_NO_VLAN */
403 
404 	os_free(sta->challenge);
405 
406 	os_free(sta->sa_query_trans_id);
407 	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
408 
409 #ifdef CONFIG_P2P
410 	p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
411 #endif /* CONFIG_P2P */
412 
413 #ifdef CONFIG_INTERWORKING
414 	if (sta->gas_dialog) {
415 		int i;
416 
417 		for (i = 0; i < GAS_DIALOG_MAX; i++)
418 			gas_serv_dialog_clear(&sta->gas_dialog[i]);
419 		os_free(sta->gas_dialog);
420 	}
421 #endif /* CONFIG_INTERWORKING */
422 
423 	wpabuf_free(sta->wps_ie);
424 	wpabuf_free(sta->p2p_ie);
425 	wpabuf_free(sta->hs20_ie);
426 	wpabuf_free(sta->roaming_consortium);
427 #ifdef CONFIG_FST
428 	wpabuf_free(sta->mb_ies);
429 #endif /* CONFIG_FST */
430 
431 	os_free(sta->ht_capabilities);
432 	os_free(sta->vht_capabilities);
433 	os_free(sta->vht_operation);
434 	os_free(sta->he_capab);
435 	os_free(sta->he_6ghz_capab);
436 	os_free(sta->eht_capab);
437 	hostapd_free_psk_list(sta->psk);
438 	os_free(sta->identity);
439 	os_free(sta->radius_cui);
440 	os_free(sta->remediation_url);
441 	os_free(sta->t_c_url);
442 	wpabuf_free(sta->hs20_deauth_req);
443 	os_free(sta->hs20_session_info_url);
444 
445 #ifdef CONFIG_SAE
446 	sae_clear_data(sta->sae);
447 	os_free(sta->sae);
448 #endif /* CONFIG_SAE */
449 
450 	mbo_ap_sta_free(sta);
451 	os_free(sta->supp_op_classes);
452 
453 #ifdef CONFIG_FILS
454 	os_free(sta->fils_pending_assoc_req);
455 	wpabuf_free(sta->fils_hlp_resp);
456 	wpabuf_free(sta->hlp_dhcp_discover);
457 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
458 #ifdef CONFIG_FILS_SK_PFS
459 	crypto_ecdh_deinit(sta->fils_ecdh);
460 	wpabuf_clear_free(sta->fils_dh_ss);
461 	wpabuf_free(sta->fils_g_sta);
462 #endif /* CONFIG_FILS_SK_PFS */
463 #endif /* CONFIG_FILS */
464 
465 #ifdef CONFIG_OWE
466 	bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
467 	crypto_ecdh_deinit(sta->owe_ecdh);
468 #endif /* CONFIG_OWE */
469 
470 #ifdef CONFIG_DPP2
471 	dpp_pfs_free(sta->dpp_pfs);
472 	sta->dpp_pfs = NULL;
473 #endif /* CONFIG_DPP2 */
474 
475 	os_free(sta->ext_capability);
476 
477 #ifdef CONFIG_WNM_AP
478 	eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
479 #endif /* CONFIG_WNM_AP */
480 
481 #ifdef CONFIG_PASN
482 	ap_free_sta_pasn(hapd, sta);
483 #endif /* CONFIG_PASN */
484 
485 	os_free(sta->ifname_wds);
486 
487 #ifdef CONFIG_IEEE80211BE
488 	ap_sta_free_sta_profile(&sta->mld_info);
489 #endif /* CONFIG_IEEE80211BE */
490 
491 #ifdef CONFIG_TESTING_OPTIONS
492 	os_free(sta->sae_postponed_commit);
493 	forced_memzero(sta->last_tk, WPA_TK_MAX_LEN);
494 #endif /* CONFIG_TESTING_OPTIONS */
495 
496 	os_free(sta);
497 }
498 
499 
hostapd_free_stas(struct hostapd_data * hapd)500 void hostapd_free_stas(struct hostapd_data *hapd)
501 {
502 	struct sta_info *sta, *prev;
503 
504 	sta = hapd->sta_list;
505 
506 	while (sta) {
507 		prev = sta;
508 		if (sta->flags & WLAN_STA_AUTH) {
509 			mlme_deauthenticate_indication(
510 				hapd, sta, WLAN_REASON_UNSPECIFIED);
511 		}
512 		sta = sta->next;
513 		wpa_printf(MSG_DEBUG, "Removing station " MACSTR_SEC,
514 			   MAC2STR_SEC(prev->addr));
515 		ap_free_sta(hapd, prev);
516 	}
517 }
518 
519 
520 #ifdef CONFIG_IEEE80211BE
hostapd_free_link_stas(struct hostapd_data * hapd)521 void hostapd_free_link_stas(struct hostapd_data *hapd)
522 {
523 	struct sta_info *sta, *prev;
524 
525 	sta = hapd->sta_list;
526 	while (sta) {
527 		prev = sta;
528 		sta = sta->next;
529 
530 		if (!hostapd_sta_is_link_sta(hapd, prev))
531 			continue;
532 
533 		wpa_printf(MSG_DEBUG, "Removing link station from MLD " MACSTR,
534 			   MAC2STR(prev->addr));
535 		ap_free_sta(hapd, prev);
536 	}
537 }
538 #endif /* CONFIG_IEEE80211BE */
539 
540 
541 /**
542  * ap_handle_timer - Per STA timer handler
543  * @eloop_ctx: struct hostapd_data *
544  * @timeout_ctx: struct sta_info *
545  *
546  * This function is called to check station activity and to remove inactive
547  * stations.
548  */
ap_handle_timer(void * eloop_ctx,void * timeout_ctx)549 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
550 {
551 	struct hostapd_data *hapd = eloop_ctx;
552 	struct sta_info *sta = timeout_ctx;
553 	unsigned long next_time = 0;
554 	int reason;
555 	int max_inactivity = hapd->conf->ap_max_inactivity;
556 
557 	wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR_SEC " flags=0x%x timeout_next=%d",
558 		   hapd->conf->iface, __func__, MAC2STR_SEC(sta->addr), sta->flags,
559 		   sta->timeout_next);
560 	if (sta->timeout_next == STA_REMOVE) {
561 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
562 			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
563 			       "local deauth request");
564 		ap_free_sta(hapd, sta);
565 		return;
566 	}
567 
568 	if (sta->max_idle_period)
569 		max_inactivity = (sta->max_idle_period * 1024 + 999) / 1000;
570 
571 	if ((sta->flags & WLAN_STA_ASSOC) &&
572 	    (sta->timeout_next == STA_NULLFUNC ||
573 	     sta->timeout_next == STA_DISASSOC)) {
574 		int inactive_sec;
575 		/*
576 		 * Add random value to timeout so that we don't end up bouncing
577 		 * all stations at the same time if we have lots of associated
578 		 * stations that are idle (but keep re-associating).
579 		 */
580 		int fuzz = os_random() % 20;
581 		inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
582 		if (inactive_sec == -1) {
583 			wpa_msg_only_for_cb(hapd->msg_ctx, MSG_DEBUG,
584 				"Check inactivity: Could not "
585 				"get station info from kernel driver for "
586 				MACSTR, MAC2STR(sta->addr));
587 			wpa_printf(MSG_DEBUG, "Check inactivity: Could not "
588 				"get station info from kernel driver for "
589 				MACSTR_SEC, MAC2STR_SEC(sta->addr));
590 			/*
591 			 * The driver may not support this functionality.
592 			 * Anyway, try again after the next inactivity timeout,
593 			 * but do not disconnect the station now.
594 			 */
595 			next_time = max_inactivity + fuzz;
596 		} else if (inactive_sec == -ENOENT) {
597 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
598 				"Station " MACSTR " has lost its driver entry",
599 				MAC2STR(sta->addr));
600 
601 			/* Avoid sending client probe on removed client */
602 			sta->timeout_next = STA_DISASSOC;
603 			goto skip_poll;
604 		} else if (inactive_sec < max_inactivity) {
605 			/* station activity detected; reset timeout state */
606 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
607 				"Station " MACSTR " has been active %is ago",
608 				MAC2STR(sta->addr), inactive_sec);
609 			sta->timeout_next = STA_NULLFUNC;
610 			next_time = max_inactivity + fuzz - inactive_sec;
611 		} else {
612 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
613 				"Station " MACSTR " has been "
614 				"inactive too long: %d sec, max allowed: %d",
615 				MAC2STR(sta->addr), inactive_sec,
616 				max_inactivity);
617 
618 			if (hapd->conf->skip_inactivity_poll)
619 				sta->timeout_next = STA_DISASSOC;
620 		}
621 	}
622 
623 	if ((sta->flags & WLAN_STA_ASSOC) &&
624 	    sta->timeout_next == STA_DISASSOC &&
625 	    !(sta->flags & WLAN_STA_PENDING_POLL) &&
626 	    !hapd->conf->skip_inactivity_poll) {
627 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
628 			" has ACKed data poll", MAC2STR(sta->addr));
629 		/* data nullfunc frame poll did not produce TX errors; assume
630 		 * station ACKed it */
631 		sta->timeout_next = STA_NULLFUNC;
632 		next_time = max_inactivity;
633 	}
634 
635 skip_poll:
636 	if (next_time) {
637 		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
638 			   "for " MACSTR_SEC " (%lu seconds)",
639 			   __func__, MAC2STR_SEC(sta->addr), next_time);
640 		eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
641 				       sta);
642 		return;
643 	}
644 
645 	if (sta->timeout_next == STA_NULLFUNC &&
646 	    (sta->flags & WLAN_STA_ASSOC)) {
647 		wpa_printf(MSG_DEBUG, "  Polling STA");
648 		sta->flags |= WLAN_STA_PENDING_POLL;
649 		hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
650 					sta->flags & WLAN_STA_WMM);
651 	} else if (sta->timeout_next != STA_REMOVE) {
652 		int deauth = sta->timeout_next == STA_DEAUTH;
653 
654 		if (!deauth && !(sta->flags & WLAN_STA_ASSOC)) {
655 			/* Cannot disassociate not-associated STA, so move
656 			 * directly to deauthentication. */
657 			sta->timeout_next = STA_DEAUTH;
658 			deauth = 1;
659 		}
660 
661 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
662 			"Timeout, sending %s info to STA " MACSTR,
663 			deauth ? "deauthentication" : "disassociation",
664 			MAC2STR(sta->addr));
665 
666 		if (deauth) {
667 			hostapd_drv_sta_deauth(
668 				hapd, sta->addr,
669 				WLAN_REASON_PREV_AUTH_NOT_VALID);
670 		} else {
671 			reason = (sta->timeout_next == STA_DISASSOC) ?
672 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
673 				WLAN_REASON_PREV_AUTH_NOT_VALID;
674 
675 			hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
676 		}
677 	}
678 
679 	switch (sta->timeout_next) {
680 	case STA_NULLFUNC:
681 		sta->timeout_next = STA_DISASSOC;
682 		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
683 			   "for " MACSTR_SEC " (%d seconds - AP_DISASSOC_DELAY)",
684 			   __func__, MAC2STR_SEC(sta->addr), AP_DISASSOC_DELAY);
685 		eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
686 				       hapd, sta);
687 		break;
688 	case STA_DISASSOC:
689 	case STA_DISASSOC_FROM_CLI:
690 		ap_sta_set_authorized(hapd, sta, 0);
691 		sta->flags &= ~WLAN_STA_ASSOC;
692 		hostapd_set_sta_flags(hapd, sta);
693 		ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
694 		if (!sta->acct_terminate_cause)
695 			sta->acct_terminate_cause =
696 				RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
697 		accounting_sta_stop(hapd, sta);
698 		ieee802_1x_free_station(hapd, sta);
699 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
700 			       HOSTAPD_LEVEL_INFO, "disassociated due to "
701 			       "inactivity");
702 		reason = (sta->timeout_next == STA_DISASSOC) ?
703 			WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
704 			WLAN_REASON_PREV_AUTH_NOT_VALID;
705 		sta->timeout_next = STA_DEAUTH;
706 		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
707 			   "for " MACSTR_SEC " (%d seconds - AP_DEAUTH_DELAY)",
708 			   __func__, MAC2STR_SEC(sta->addr), AP_DEAUTH_DELAY);
709 		eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
710 				       hapd, sta);
711 		mlme_disassociate_indication(hapd, sta, reason);
712 		break;
713 	case STA_DEAUTH:
714 	case STA_REMOVE:
715 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
716 			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
717 			       "inactivity (timer DEAUTH/REMOVE)");
718 		if (!sta->acct_terminate_cause)
719 			sta->acct_terminate_cause =
720 				RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
721 		mlme_deauthenticate_indication(
722 			hapd, sta,
723 			WLAN_REASON_PREV_AUTH_NOT_VALID);
724 		ap_free_sta(hapd, sta);
725 		break;
726 	}
727 }
728 
729 
ap_handle_session_timer(void * eloop_ctx,void * timeout_ctx)730 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
731 {
732 	struct hostapd_data *hapd = eloop_ctx;
733 	struct sta_info *sta = timeout_ctx;
734 
735 	wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR_SEC,
736 		   hapd->conf->iface, MAC2STR_SEC(sta->addr));
737 	if (!(sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC |
738 			    WLAN_STA_AUTHORIZED))) {
739 		if (sta->flags & WLAN_STA_GAS) {
740 			wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
741 				   "entry " MACSTR_SEC, MAC2STR_SEC(sta->addr));
742 			ap_free_sta(hapd, sta);
743 		}
744 		return;
745 	}
746 
747 	hostapd_drv_sta_deauth(hapd, sta->addr,
748 			       WLAN_REASON_PREV_AUTH_NOT_VALID);
749 	mlme_deauthenticate_indication(hapd, sta,
750 				       WLAN_REASON_PREV_AUTH_NOT_VALID);
751 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
752 		       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
753 		       "session timeout");
754 	sta->acct_terminate_cause =
755 		RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
756 	ap_free_sta(hapd, sta);
757 }
758 
759 
ap_sta_replenish_timeout(struct hostapd_data * hapd,struct sta_info * sta,u32 session_timeout)760 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
761 			      u32 session_timeout)
762 {
763 	if (eloop_replenish_timeout(session_timeout, 0,
764 				    ap_handle_session_timer, hapd, sta) == 1) {
765 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
766 			       HOSTAPD_LEVEL_DEBUG, "setting session timeout "
767 			       "to %d seconds", session_timeout);
768 	}
769 }
770 
771 
ap_sta_session_timeout(struct hostapd_data * hapd,struct sta_info * sta,u32 session_timeout)772 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
773 			    u32 session_timeout)
774 {
775 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
776 		       HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
777 		       "seconds", session_timeout);
778 	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
779 	eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
780 			       hapd, sta);
781 }
782 
783 
ap_sta_no_session_timeout(struct hostapd_data * hapd,struct sta_info * sta)784 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
785 {
786 	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
787 }
788 
789 
ap_handle_session_warning_timer(void * eloop_ctx,void * timeout_ctx)790 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
791 {
792 #ifdef CONFIG_WNM_AP
793 	struct hostapd_data *hapd = eloop_ctx;
794 	struct sta_info *sta = timeout_ctx;
795 
796 	wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
797 		   MACSTR_SEC, hapd->conf->iface, MAC2STR_SEC(sta->addr));
798 	if (sta->hs20_session_info_url == NULL)
799 		return;
800 
801 	wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
802 				       sta->hs20_disassoc_timer);
803 #endif /* CONFIG_WNM_AP */
804 }
805 
806 
ap_sta_session_warning_timeout(struct hostapd_data * hapd,struct sta_info * sta,int warning_time)807 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
808 				    struct sta_info *sta, int warning_time)
809 {
810 	eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
811 	eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
812 			       hapd, sta);
813 }
814 
815 
ap_sta_add(struct hostapd_data * hapd,const u8 * addr)816 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
817 {
818 	struct sta_info *sta;
819 	int i;
820 	int max_inactivity = hapd->conf->ap_max_inactivity;
821 
822 	sta = ap_get_sta(hapd, addr);
823 	if (sta)
824 		return sta;
825 
826 	wpa_printf(MSG_DEBUG, "  New STA");
827 	if (hapd->num_sta >= hapd->conf->max_num_sta) {
828 		/* FIX: might try to remove some old STAs first? */
829 		wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
830 			   hapd->num_sta, hapd->conf->max_num_sta);
831 		return NULL;
832 	}
833 
834 	sta = os_zalloc(sizeof(struct sta_info));
835 	if (sta == NULL) {
836 		wpa_printf(MSG_ERROR, "malloc failed");
837 		return NULL;
838 	}
839 	sta->acct_interim_interval = hapd->conf->acct_interim_interval;
840 	if (accounting_sta_get_id(hapd, sta) < 0) {
841 		os_free(sta);
842 		return NULL;
843 	}
844 
845 	for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
846 		if (!hapd->iface->basic_rates)
847 			break;
848 		if (hapd->iface->basic_rates[i] < 0)
849 			break;
850 		sta->supported_rates[i] = hapd->iface->basic_rates[i] / 5;
851 	}
852 	sta->supported_rates_len = i;
853 
854 	if (sta->max_idle_period)
855 		max_inactivity = (sta->max_idle_period * 1024 + 999) / 1000;
856 
857 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
858 		wpa_printf(MSG_EXCESSIVE, "%s: register ap_handle_timer timeout "
859 			   "for " MACSTR_SEC " (%d seconds - ap_max_inactivity)",
860 			   __func__, MAC2STR_SEC(addr),
861 			   max_inactivity);
862 		eloop_register_timeout(max_inactivity, 0,
863 				       ap_handle_timer, hapd, sta);
864 	}
865 
866 	/* initialize STA info data */
867 	os_memcpy(sta->addr, addr, ETH_ALEN);
868 	sta->next = hapd->sta_list;
869 	hapd->sta_list = sta;
870 	hapd->num_sta++;
871 	ap_sta_hash_add(hapd, sta);
872 	ap_sta_remove_in_other_bss(hapd, sta);
873 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
874 	dl_list_init(&sta->ip6addr);
875 
876 #ifdef CONFIG_TAXONOMY
877 	sta_track_claim_taxonomy_info(hapd->iface, addr,
878 				      &sta->probe_ie_taxonomy);
879 #endif /* CONFIG_TAXONOMY */
880 
881 	return sta;
882 }
883 
884 
ap_sta_remove(struct hostapd_data * hapd,struct sta_info * sta)885 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
886 {
887 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
888 
889 	if (sta->ipaddr)
890 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
891 	ap_sta_ip6addr_del(hapd, sta);
892 
893 	wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR_SEC " from kernel driver",
894 		   hapd->conf->iface, MAC2STR_SEC(sta->addr));
895 	if (hostapd_drv_sta_remove(hapd, sta->addr) &&
896 	    sta->flags & WLAN_STA_ASSOC) {
897 		wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR_SEC
898 			   " from kernel driver",
899 			   hapd->conf->iface, MAC2STR_SEC(sta->addr));
900 		return -1;
901 	}
902 	sta->added_unassoc = 0;
903 	return 0;
904 }
905 
906 
ap_sta_remove_in_other_bss(struct hostapd_data * hapd,struct sta_info * sta)907 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
908 				       struct sta_info *sta)
909 {
910 	struct hostapd_iface *iface = hapd->iface;
911 	size_t i;
912 
913 	for (i = 0; i < iface->num_bss; i++) {
914 		struct hostapd_data *bss = iface->bss[i];
915 		struct sta_info *sta2;
916 		/* bss should always be set during operation, but it may be
917 		 * NULL during reconfiguration. Assume the STA is not
918 		 * associated to another BSS in that case to avoid NULL pointer
919 		 * dereferences. */
920 		if (bss == hapd || bss == NULL)
921 			continue;
922 		sta2 = ap_get_sta(bss, sta->addr);
923 		if (!sta2)
924 			continue;
925 
926 		wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR_SEC
927 			   " association from another BSS %s",
928 			   hapd->conf->iface, MAC2STR_SEC(sta2->addr),
929 			   bss->conf->iface);
930 		ap_sta_disconnect(bss, sta2, sta2->addr,
931 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
932 	}
933 }
934 
935 
ap_sta_disassoc_cb_timeout(void * eloop_ctx,void * timeout_ctx)936 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
937 {
938 	struct hostapd_data *hapd = eloop_ctx;
939 	struct sta_info *sta = timeout_ctx;
940 
941 	wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR_SEC,
942 		   hapd->conf->iface, MAC2STR_SEC(sta->addr));
943 	ap_sta_remove(hapd, sta);
944 	mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
945 }
946 
947 
ap_sta_disconnect_common(struct hostapd_data * hapd,struct sta_info * sta,unsigned int timeout)948 static void ap_sta_disconnect_common(struct hostapd_data *hapd,
949 				     struct sta_info *sta, unsigned int timeout)
950 {
951 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
952 
953 	ap_sta_set_authorized(hapd, sta, 0);
954 	hostapd_set_sta_flags(hapd, sta);
955 
956 	wpa_printf(MSG_DEBUG,
957 		   "reschedule ap_handle_timer timeout (%u sec) for " MACSTR_SEC,
958 		   timeout, MAC2STR_SEC(sta->addr));
959 
960 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
961 	eloop_register_timeout(timeout, 0, ap_handle_timer, hapd, sta);
962 	accounting_sta_stop(hapd, sta);
963 	ieee802_1x_free_station(hapd, sta);
964 #ifdef CONFIG_IEEE80211BE
965 	if (!hapd->conf->mld_ap ||
966 	    hapd->mld_link_id == sta->mld_assoc_link_id) {
967 		wpa_auth_sta_deinit(sta->wpa_sm);
968 		clear_wpa_sm_for_each_partner_link(hapd, sta);
969 	}
970 #else /* CONFIG_IEEE80211BE */
971 	wpa_auth_sta_deinit(sta->wpa_sm);
972 #endif /* CONFIG_IEEE80211BE */
973 
974 	sta->wpa_sm = NULL;
975 }
976 
977 
ap_sta_handle_disassociate(struct hostapd_data * hapd,struct sta_info * sta,u16 reason)978 static void ap_sta_handle_disassociate(struct hostapd_data *hapd,
979 				       struct sta_info *sta, u16 reason)
980 {
981 	wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR_SEC,
982 		   hapd->conf->iface, MAC2STR_SEC(sta->addr));
983 
984 	if (hapd->iface->current_mode &&
985 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
986 		/* Skip deauthentication in DMG/IEEE 802.11ad */
987 		sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
988 				WLAN_STA_ASSOC_REQ_OK);
989 		sta->timeout_next = STA_REMOVE;
990 	} else {
991 		sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
992 		sta->timeout_next = STA_DEAUTH;
993 	}
994 
995 	ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DISASSOC);
996 
997 	sta->disassoc_reason = reason;
998 	sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
999 	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1000 	eloop_register_timeout(hapd->iface->drv_flags &
1001 			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1002 			       ap_sta_disassoc_cb_timeout, hapd, sta);
1003 }
1004 
1005 
ap_sta_deauth_cb_timeout(void * eloop_ctx,void * timeout_ctx)1006 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
1007 {
1008 	struct hostapd_data *hapd = eloop_ctx;
1009 	struct sta_info *sta = timeout_ctx;
1010 
1011 	wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR_SEC,
1012 		   hapd->conf->iface, MAC2STR_SEC(sta->addr));
1013 	ap_sta_remove(hapd, sta);
1014 	mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
1015 }
1016 
1017 
ap_sta_handle_deauthenticate(struct hostapd_data * hapd,struct sta_info * sta,u16 reason)1018 static void ap_sta_handle_deauthenticate(struct hostapd_data *hapd,
1019 					 struct sta_info *sta, u16 reason)
1020 {
1021 	if (hapd->iface->current_mode &&
1022 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1023 		/* Deauthentication is not used in DMG/IEEE 802.11ad;
1024 		 * disassociate the STA instead. */
1025 		ap_sta_disassociate(hapd, sta, reason);
1026 		return;
1027 	}
1028 
1029 	wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR_SEC,
1030 		   hapd->conf->iface, MAC2STR_SEC(sta->addr));
1031 
1032 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1033 
1034 	sta->timeout_next = STA_REMOVE;
1035 	ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DEAUTH);
1036 
1037 	sta->deauth_reason = reason;
1038 	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1039 	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1040 	eloop_register_timeout(hapd->iface->drv_flags &
1041 			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1042 			       ap_sta_deauth_cb_timeout, hapd, sta);
1043 }
1044 
1045 
ap_sta_ml_disconnect(struct hostapd_data * hapd,struct sta_info * sta,u16 reason,bool disassoc)1046 static bool ap_sta_ml_disconnect(struct hostapd_data *hapd,
1047 				 struct sta_info *sta, u16 reason,
1048 				 bool disassoc)
1049 {
1050 #ifdef CONFIG_IEEE80211BE
1051 	struct hostapd_data *assoc_hapd, *tmp_hapd;
1052 	struct sta_info *assoc_sta;
1053 	unsigned int i, link_id;
1054 	struct hapd_interfaces *interfaces;
1055 
1056 	if (!hostapd_is_mld_ap(hapd))
1057 		return false;
1058 
1059 	/*
1060 	 * Get the station on which the association was performed, as it holds
1061 	 * the information about all the other links.
1062 	 */
1063 	assoc_sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
1064 	if (!assoc_sta)
1065 		return false;
1066 	interfaces = assoc_hapd->iface->interfaces;
1067 
1068 	for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
1069 		if (!assoc_sta->mld_info.links[link_id].valid)
1070 			continue;
1071 
1072 		for (i = 0; i < interfaces->count; i++) {
1073 			struct sta_info *tmp_sta;
1074 
1075 			tmp_hapd = interfaces->iface[i]->bss[0];
1076 
1077 			if (!hostapd_is_ml_partner(tmp_hapd, assoc_hapd))
1078 				continue;
1079 
1080 			for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
1081 			     tmp_sta = tmp_sta->next) {
1082 				/*
1083 				 * Handle the station on which the association
1084 				 * was done only after all other link station
1085 				 * are removed. Since there is a only a single
1086 				 * station per hapd with the same association
1087 				 * link simply break;
1088 				 */
1089 				if (tmp_sta == assoc_sta)
1090 					break;
1091 
1092 				if (tmp_sta->mld_assoc_link_id !=
1093 				    assoc_sta->mld_assoc_link_id ||
1094 				    tmp_sta->aid != assoc_sta->aid)
1095 					continue;
1096 
1097 				if (disassoc)
1098 					ap_sta_handle_disassociate(tmp_hapd,
1099 								   tmp_sta,
1100 								   reason);
1101 				else
1102 					ap_sta_handle_deauthenticate(tmp_hapd,
1103 								     tmp_sta,
1104 								     reason);
1105 
1106 				break;
1107 			}
1108 		}
1109 	}
1110 
1111 	/* Disconnect the station on which the association was performed. */
1112 	if (disassoc)
1113 		ap_sta_handle_disassociate(assoc_hapd, assoc_sta, reason);
1114 	else
1115 		ap_sta_handle_deauthenticate(assoc_hapd, assoc_sta, reason);
1116 
1117 	return true;
1118 #else /* CONFIG_IEEE80211BE */
1119 	return false;
1120 #endif /* CONFIG_IEEE80211BE */
1121 }
1122 
1123 
ap_sta_disassociate(struct hostapd_data * hapd,struct sta_info * sta,u16 reason)1124 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
1125 			 u16 reason)
1126 {
1127 	if (ap_sta_ml_disconnect(hapd, sta, reason, true))
1128 		return;
1129 
1130 	ap_sta_handle_disassociate(hapd, sta, reason);
1131 }
1132 
1133 
ap_sta_deauthenticate(struct hostapd_data * hapd,struct sta_info * sta,u16 reason)1134 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
1135 			   u16 reason)
1136 {
1137 	if (ap_sta_ml_disconnect(hapd, sta, reason, false))
1138 		return;
1139 
1140 	ap_sta_handle_deauthenticate(hapd, sta, reason);
1141 }
1142 
1143 
1144 #ifdef CONFIG_WPS
ap_sta_wps_cancel(struct hostapd_data * hapd,struct sta_info * sta,void * ctx)1145 int ap_sta_wps_cancel(struct hostapd_data *hapd,
1146 		      struct sta_info *sta, void *ctx)
1147 {
1148 	if (sta && (sta->flags & WLAN_STA_WPS)) {
1149 		ap_sta_deauthenticate(hapd, sta,
1150 				      WLAN_REASON_PREV_AUTH_NOT_VALID);
1151 		wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR_SEC,
1152 			   __func__, MAC2STR_SEC(sta->addr));
1153 		return 1;
1154 	}
1155 
1156 	return 0;
1157 }
1158 #endif /* CONFIG_WPS */
1159 
1160 
ap_sta_get_free_vlan_id(struct hostapd_data * hapd)1161 static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
1162 {
1163 	struct hostapd_vlan *vlan;
1164 	int vlan_id = MAX_VLAN_ID + 2;
1165 
1166 retry:
1167 	for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1168 		if (vlan->vlan_id == vlan_id) {
1169 			vlan_id++;
1170 			goto retry;
1171 		}
1172 	}
1173 	return vlan_id;
1174 }
1175 
1176 
ap_sta_set_vlan(struct hostapd_data * hapd,struct sta_info * sta,struct vlan_description * vlan_desc)1177 int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
1178 		    struct vlan_description *vlan_desc)
1179 {
1180 	struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
1181 	int old_vlan_id, vlan_id = 0, ret = 0;
1182 
1183 	/* Check if there is something to do */
1184 	if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
1185 		/* This sta is lacking its own vif */
1186 	} else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
1187 		   !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
1188 		/* sta->vlan_id needs to be reset */
1189 	} else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
1190 		return 0; /* nothing to change */
1191 	}
1192 
1193 	/* Now the real VLAN changed or the STA just needs its own vif */
1194 	if (hapd->conf->ssid.per_sta_vif) {
1195 		/* Assign a new vif, always */
1196 		/* find a free vlan_id sufficiently big */
1197 		vlan_id = ap_sta_get_free_vlan_id(hapd);
1198 		/* Get wildcard VLAN */
1199 		for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1200 			if (vlan->vlan_id == VLAN_ID_WILDCARD)
1201 				break;
1202 		}
1203 		if (!vlan) {
1204 			hostapd_logger(hapd, sta->addr,
1205 				       HOSTAPD_MODULE_IEEE80211,
1206 				       HOSTAPD_LEVEL_DEBUG,
1207 				       "per_sta_vif missing wildcard");
1208 			vlan_id = 0;
1209 			ret = -1;
1210 			goto done;
1211 		}
1212 	} else if (vlan_desc && vlan_desc->notempty) {
1213 		for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1214 			if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
1215 				break;
1216 			if (vlan->vlan_id == VLAN_ID_WILDCARD)
1217 				wildcard_vlan = vlan;
1218 		}
1219 		if (vlan) {
1220 			vlan_id = vlan->vlan_id;
1221 		} else if (wildcard_vlan) {
1222 			vlan = wildcard_vlan;
1223 			vlan_id = vlan_desc->untagged;
1224 			if (vlan_desc->tagged[0]) {
1225 				/* Tagged VLAN configuration */
1226 				vlan_id = ap_sta_get_free_vlan_id(hapd);
1227 			}
1228 		} else {
1229 			hostapd_logger(hapd, sta->addr,
1230 				       HOSTAPD_MODULE_IEEE80211,
1231 				       HOSTAPD_LEVEL_DEBUG,
1232 				       "missing vlan and wildcard for vlan=%d%s",
1233 				       vlan_desc->untagged,
1234 				       vlan_desc->tagged[0] ? "+" : "");
1235 			vlan_id = 0;
1236 			ret = -1;
1237 			goto done;
1238 		}
1239 	}
1240 
1241 	if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
1242 		vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
1243 		if (vlan == NULL) {
1244 			hostapd_logger(hapd, sta->addr,
1245 				       HOSTAPD_MODULE_IEEE80211,
1246 				       HOSTAPD_LEVEL_DEBUG,
1247 				       "could not add dynamic VLAN interface for vlan=%d%s",
1248 				       vlan_desc ? vlan_desc->untagged : -1,
1249 				       (vlan_desc && vlan_desc->tagged[0]) ?
1250 				       "+" : "");
1251 			vlan_id = 0;
1252 			ret = -1;
1253 			goto done;
1254 		}
1255 
1256 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1257 			       HOSTAPD_LEVEL_DEBUG,
1258 			       "added new dynamic VLAN interface '%s'",
1259 			       vlan->ifname);
1260 	} else if (vlan && vlan->dynamic_vlan > 0) {
1261 		vlan->dynamic_vlan++;
1262 		hostapd_logger(hapd, sta->addr,
1263 			       HOSTAPD_MODULE_IEEE80211,
1264 			       HOSTAPD_LEVEL_DEBUG,
1265 			       "updated existing dynamic VLAN interface '%s'",
1266 			       vlan->ifname);
1267 	}
1268 done:
1269 	old_vlan_id = sta->vlan_id;
1270 	sta->vlan_id = vlan_id;
1271 	sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
1272 
1273 	if (vlan_id != old_vlan_id && old_vlan_id)
1274 		vlan_remove_dynamic(hapd, old_vlan_id);
1275 
1276 	return ret;
1277 }
1278 
1279 
ap_sta_bind_vlan(struct hostapd_data * hapd,struct sta_info * sta)1280 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
1281 {
1282 #ifndef CONFIG_NO_VLAN
1283 	const char *iface;
1284 	struct hostapd_vlan *vlan = NULL;
1285 	int ret;
1286 	int old_vlanid = sta->vlan_id_bound;
1287 	int mld_link_id = -1;
1288 
1289 #ifdef CONFIG_IEEE80211BE
1290 	if (hapd->conf->mld_ap)
1291 		mld_link_id = hapd->mld_link_id;
1292 #endif /* CONFIG_IEEE80211BE */
1293 
1294 	if ((sta->flags & WLAN_STA_WDS) && sta->vlan_id == 0) {
1295 		wpa_printf(MSG_DEBUG,
1296 			   "Do not override WDS VLAN assignment for STA "
1297 			   MACSTR_SEC, MAC2STR_SEC(sta->addr));
1298 		return 0;
1299 	}
1300 
1301 	iface = hapd->conf->iface;
1302 	if (hapd->conf->ssid.vlan[0])
1303 		iface = hapd->conf->ssid.vlan;
1304 
1305 	if (sta->vlan_id > 0) {
1306 		for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1307 			if (vlan->vlan_id == sta->vlan_id)
1308 				break;
1309 		}
1310 		if (vlan)
1311 			iface = vlan->ifname;
1312 	}
1313 
1314 	/*
1315 	 * Do not increment ref counters if the VLAN ID remains same, but do
1316 	 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1317 	 * have been called before.
1318 	 */
1319 	if (sta->vlan_id == old_vlanid)
1320 		goto skip_counting;
1321 
1322 	if (sta->vlan_id > 0 && !vlan &&
1323 	    !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
1324 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1325 			       HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1326 			       "binding station to (vlan_id=%d)",
1327 			       sta->vlan_id);
1328 		ret = -1;
1329 		goto done;
1330 	} else if (vlan && vlan->dynamic_vlan > 0) {
1331 		vlan->dynamic_vlan++;
1332 		hostapd_logger(hapd, sta->addr,
1333 			       HOSTAPD_MODULE_IEEE80211,
1334 			       HOSTAPD_LEVEL_DEBUG,
1335 			       "updated existing dynamic VLAN interface '%s'",
1336 			       iface);
1337 	}
1338 
1339 	/* ref counters have been increased, so mark the station */
1340 	sta->vlan_id_bound = sta->vlan_id;
1341 
1342 skip_counting:
1343 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1344 		       HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1345 		       "'%s'", iface);
1346 
1347 	if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1348 		wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1349 
1350 	ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id,
1351 				       mld_link_id);
1352 	if (ret < 0) {
1353 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1354 			       HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1355 			       "entry to vlan_id=%d", sta->vlan_id);
1356 	}
1357 
1358 	/* During 1x reauth, if the vlan id changes, then remove the old id. */
1359 	if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
1360 		vlan_remove_dynamic(hapd, old_vlanid);
1361 done:
1362 
1363 	return ret;
1364 #else /* CONFIG_NO_VLAN */
1365 	return 0;
1366 #endif /* CONFIG_NO_VLAN */
1367 }
1368 
1369 
ap_check_sa_query_timeout(struct hostapd_data * hapd,struct sta_info * sta)1370 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1371 {
1372 	u32 tu;
1373 	struct os_reltime now, passed;
1374 	os_get_reltime(&now);
1375 	os_reltime_sub(&now, &sta->sa_query_start, &passed);
1376 	tu = (passed.sec * 1000000 + passed.usec) / 1024;
1377 	if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1378 		hostapd_logger(hapd, sta->addr,
1379 			       HOSTAPD_MODULE_IEEE80211,
1380 			       HOSTAPD_LEVEL_DEBUG,
1381 			       "association SA Query timed out");
1382 		sta->sa_query_timed_out = 1;
1383 		os_free(sta->sa_query_trans_id);
1384 		sta->sa_query_trans_id = NULL;
1385 		sta->sa_query_count = 0;
1386 		eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1387 		return 1;
1388 	}
1389 
1390 	return 0;
1391 }
1392 
1393 
ap_sa_query_timer(void * eloop_ctx,void * timeout_ctx)1394 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1395 {
1396 	struct hostapd_data *hapd = eloop_ctx;
1397 	struct sta_info *sta = timeout_ctx;
1398 	unsigned int timeout, sec, usec;
1399 	u8 *trans_id, *nbuf;
1400 
1401 	wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR_SEC
1402 		   " (count=%d)",
1403 		   hapd->conf->iface, MAC2STR_SEC(sta->addr), sta->sa_query_count);
1404 
1405 	if (sta->sa_query_count > 0 &&
1406 	    ap_check_sa_query_timeout(hapd, sta))
1407 		return;
1408 	if (sta->sa_query_count >= 1000)
1409 		return;
1410 
1411 	nbuf = os_realloc_array(sta->sa_query_trans_id,
1412 				sta->sa_query_count + 1,
1413 				WLAN_SA_QUERY_TR_ID_LEN);
1414 	if (nbuf == NULL)
1415 		return;
1416 	if (sta->sa_query_count == 0) {
1417 		/* Starting a new SA Query procedure */
1418 		os_get_reltime(&sta->sa_query_start);
1419 	}
1420 	trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1421 	sta->sa_query_trans_id = nbuf;
1422 	sta->sa_query_count++;
1423 
1424 	if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1425 		/*
1426 		 * We don't really care which ID is used here, so simply
1427 		 * hardcode this if the mostly theoretical os_get_random()
1428 		 * failure happens.
1429 		 */
1430 		trans_id[0] = 0x12;
1431 		trans_id[1] = 0x34;
1432 	}
1433 
1434 	timeout = hapd->conf->assoc_sa_query_retry_timeout;
1435 	sec = ((timeout / 1000) * 1024) / 1000;
1436 	usec = (timeout % 1000) * 1024;
1437 	eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1438 
1439 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1440 		       HOSTAPD_LEVEL_DEBUG,
1441 		       "association SA Query attempt %d", sta->sa_query_count);
1442 
1443 	ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
1444 }
1445 
1446 
ap_sta_start_sa_query(struct hostapd_data * hapd,struct sta_info * sta)1447 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1448 {
1449 	ap_sa_query_timer(hapd, sta);
1450 }
1451 
1452 
ap_sta_stop_sa_query(struct hostapd_data * hapd,struct sta_info * sta)1453 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1454 {
1455 	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1456 	os_free(sta->sa_query_trans_id);
1457 	sta->sa_query_trans_id = NULL;
1458 	sta->sa_query_count = 0;
1459 }
1460 
1461 
ap_sta_wpa_get_keyid(struct hostapd_data * hapd,struct sta_info * sta)1462 const char * ap_sta_wpa_get_keyid(struct hostapd_data *hapd,
1463 				  struct sta_info *sta)
1464 {
1465 	struct hostapd_wpa_psk *psk;
1466 	struct hostapd_ssid *ssid;
1467 	const u8 *pmk;
1468 	int pmk_len;
1469 
1470 	ssid = &hapd->conf->ssid;
1471 
1472 	pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1473 	if (!pmk || pmk_len != PMK_LEN)
1474 		return NULL;
1475 
1476 	for (psk = ssid->wpa_psk; psk; psk = psk->next)
1477 		if (os_memcmp(pmk, psk->psk, PMK_LEN) == 0)
1478 			break;
1479 	if (!psk || !psk->keyid[0])
1480 		return NULL;
1481 
1482 	return psk->keyid;
1483 }
1484 
1485 
ap_sta_wpa_get_dpp_pkhash(struct hostapd_data * hapd,struct sta_info * sta)1486 const u8 * ap_sta_wpa_get_dpp_pkhash(struct hostapd_data *hapd,
1487 				     struct sta_info *sta)
1488 {
1489 	return wpa_auth_get_dpp_pkhash(sta->wpa_sm);
1490 }
1491 
1492 
ap_sta_set_authorized_flag(struct hostapd_data * hapd,struct sta_info * sta,int authorized)1493 bool ap_sta_set_authorized_flag(struct hostapd_data *hapd, struct sta_info *sta,
1494 				int authorized)
1495 {
1496 	if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1497 		return false;
1498 
1499 	if (authorized) {
1500 		int mld_assoc_link_id = -1;
1501 
1502 #ifdef CONFIG_IEEE80211BE
1503 		if (ap_sta_is_mld(hapd, sta)) {
1504 			if (sta->mld_assoc_link_id == hapd->mld_link_id)
1505 				mld_assoc_link_id = sta->mld_assoc_link_id;
1506 			else
1507 				mld_assoc_link_id = -2;
1508 		}
1509 #endif /* CONFIG_IEEE80211BE */
1510 		if (mld_assoc_link_id != -2)
1511 			hostapd_prune_associations(hapd, sta->addr,
1512 						   mld_assoc_link_id);
1513 		sta->flags |= WLAN_STA_AUTHORIZED;
1514 	} else {
1515 		sta->flags &= ~WLAN_STA_AUTHORIZED;
1516 	}
1517 
1518 	return true;
1519 }
1520 
1521 
ap_sta_set_authorized_event(struct hostapd_data * hapd,struct sta_info * sta,int authorized)1522 void ap_sta_set_authorized_event(struct hostapd_data *hapd,
1523 				 struct sta_info *sta, int authorized)
1524 {
1525 	const u8 *dev_addr = NULL;
1526 	char buf[100];
1527 	char log_buf[100];
1528 #ifdef CONFIG_P2P
1529 	u8 addr[ETH_ALEN];
1530 	u8 ip_addr_buf[4];
1531 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1532 	struct hm_p2p_pvt_peer *peer = NULL;
1533 #endif
1534 #endif /* CONFIG_P2P */
1535 #ifdef CONFIG_LIBWPA_VENDOR
1536 	int result;
1537 #endif /* CONFIG_P2P */
1538 	const u8 *ip_ptr = NULL;
1539 
1540 #ifdef CONFIG_P2P
1541 	if (hapd->p2p_group == NULL) {
1542 		if (sta->p2p_ie != NULL &&
1543 		    p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1544 			dev_addr = addr;
1545 	} else
1546 		dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1547 
1548 	if (dev_addr) {
1549 		os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1550 			MAC2STR(sta->addr), MAC2STR(dev_addr));
1551 		os_snprintf(log_buf, sizeof(log_buf), MACSTR_SEC " p2p_dev_addr=" MACSTR_SEC,
1552 			MAC2STR_SEC(sta->addr), MAC2STR_SEC(dev_addr));
1553 		wpa_printf(MSG_INFO, MACSTR_SEC " p2p_dev_addr=" MACSTR_SEC,
1554 			MAC2STR_SEC(sta->addr), MAC2STR_SEC(dev_addr));
1555 	}
1556 	else {
1557 #endif /* CONFIG_P2P */
1558 		os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1559 		os_snprintf(log_buf, sizeof(log_buf), MACSTR_SEC, MAC2STR_SEC(sta->addr));
1560 		wpa_printf(MSG_INFO, MACSTR_SEC, MAC2STR_SEC(sta->addr));
1561 	}
1562 
1563 	if (authorized) {
1564 		const u8 *dpp_pkhash;
1565 		const char *keyid;
1566 		char dpp_pkhash_buf[100];
1567 		char keyid_buf[100];
1568 		char ip_addr[100];
1569 
1570 		dpp_pkhash_buf[0] = '\0';
1571 		keyid_buf[0] = '\0';
1572 		ip_addr[0] = '\0';
1573 #ifdef CONFIG_P2P
1574 		if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1575 			os_snprintf(ip_addr, sizeof(ip_addr),
1576 				    " ip_addr=%u.%u.%u.%u",
1577 				    ip_addr_buf[0], ip_addr_buf[1],
1578 				    ip_addr_buf[2], ip_addr_buf[3]);
1579 			ip_ptr = ip_addr_buf;
1580 		}
1581 #if defined(CONFIG_OPEN_HARMONY_PATCH) && defined(OPEN_HARMONY_MIRACAST_SINK_OPT)
1582 		peer = hm_p2p_find_peer(dev_addr);
1583 		if (peer != NULL) {
1584 			peer->go_req_cnt = 0;
1585 			wpa_printf(MSG_DEBUG, "p2p connected reset go_req_cnt");
1586 		}
1587 #endif
1588 #endif /* CONFIG_P2P */
1589 
1590 		keyid = ap_sta_wpa_get_keyid(hapd, sta);
1591 		if (keyid) {
1592 			os_snprintf(keyid_buf, sizeof(keyid_buf),
1593 				    " keyid=%s", keyid);
1594 		}
1595 #ifdef CONFIG_VENDOR_EXT
1596 		int res = wpa_vendor_ext_msg_for_cb(hapd, buf, ip_addr, keyid_buf, sta->addr);
1597 		if (res != 0) {
1598 #ifdef CONFIG_LIBWPA_VENDOR
1599 			struct HostapdApCbParm hostapdApCbParm = {};
1600 			result = os_snprintf((char *)hostapdApCbParm.content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,  AP_STA_CONNECTED "%s%s%s",
1601 				buf, ip_addr, keyid_buf);
1602 			if (os_snprintf_error(WIFI_HOSTAPD_CB_CONTENT_LENGTH, result)) {
1603 				wpa_printf(MSG_ERROR, "ap_sta_set_authorized AP_STA_CONNECTED os_snprintf_error");
1604 			} else {
1605 				hostapdApCbParm.id = 0;
1606 				wpa_printf(MSG_INFO, "%s HOSTAPD_EVENT_STA_JOIN AP_STA_CONNECTED %s%s%s%d", __func__,
1607 					log_buf, anonymize_ip(ip_addr), keyid_buf, hostapdApCbParm.id);
1608 				HostapdEventReport(hapd->conf->iface, HOSTAPD_EVENT_STA_JOIN, (void *) &hostapdApCbParm);
1609 			}
1610 #endif
1611 		}
1612 #else
1613 		wpa_msg_only_for_cb(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s",
1614 			buf, ip_addr,
1615 			keyid_buf);
1616 #ifdef CONFIG_LIBWPA_VENDOR
1617 		struct HostapdApCbParm hostapdApCbParm = {};
1618 		result = os_snprintf((char *)hostapdApCbParm.content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
1619 			AP_STA_CONNECTED "%s%s%s", buf, ip_addr, keyid_buf);
1620 		if (os_snprintf_error(WIFI_HOSTAPD_CB_CONTENT_LENGTH, result)) {
1621 			wpa_printf(MSG_ERROR, "ap_sta_set_authorized AP_STA_CONNECTED os_snprintf_error");
1622 		} else {
1623 			hostapdApCbParm.id = 0;
1624 			wpa_printf(MSG_INFO, "%s HOSTAPD_EVENT_STA_JOIN AP_STA_CONNECTED %s%s%s%d", __func__,
1625 				log_buf, anonymize_ip(ip_addr), keyid_buf, hostapdApCbParm.id);
1626 			HostapdEventReport(hapd->conf->iface, HOSTAPD_EVENT_STA_JOIN, (void *) &hostapdApCbParm);
1627 		}
1628 #endif
1629 #endif
1630 #ifdef CONFIG_LIBWPA_VENDOR
1631 #ifdef CONFIG_VENDOR_EXT
1632 		if (hapd->p2p_group != NULL && !wpa_vendor_ext_is_p2p_enhance_mode(hapd->msg_ctx)) {
1633 #endif
1634 			struct P2pStaConnectStateParam p2pStaConnectStateParam;
1635 			p2pStaConnectStateParam.state = 1;
1636 			os_memcpy(p2pStaConnectStateParam.srcAddress, sta->addr, ETH_ALEN);
1637 #ifdef CONFIG_WIFI_RPT
1638 			if (hapd->p2p != NULL && hapd->p2p->p2p_rpt == TRUE) {
1639 				os_memcpy(p2pStaConnectStateParam.p2pDeviceAddress, sta->addr, ETH_ALEN);
1640 			} else
1641 #endif /* CONFIG_WIFI_RPT */
1642 			if (dev_addr) {
1643 				os_memcpy(p2pStaConnectStateParam.p2pDeviceAddress, dev_addr, ETH_ALEN);
1644 			} else {
1645 				wpa_printf(MSG_INFO, "dev_addr is null");
1646 			}
1647 			wpa_printf(MSG_INFO, "WPA_EVENT_STA_CONNECT_STATE 1 " MACSTR_SEC " p2p_dev_addr=" MACSTR_SEC,
1648 				MAC2STR_SEC(p2pStaConnectStateParam.srcAddress), MAC2STR_SEC(p2pStaConnectStateParam.p2pDeviceAddress));
1649 			WpaEventReport(((struct wpa_supplicant *) hapd->msg_ctx)->ifname, WPA_EVENT_STA_CONNECT_STATE,
1650 				(void *) &p2pStaConnectStateParam);
1651 #ifdef CONFIG_VENDOR_EXT
1652 		}
1653 #endif
1654 #endif
1655 		wpa_printf(MSG_INFO, AP_STA_CONNECTED);
1656 
1657 		dpp_pkhash = ap_sta_wpa_get_dpp_pkhash(hapd, sta);
1658 		if (dpp_pkhash) {
1659 			const char *prefix = " dpp_pkhash=";
1660 			size_t plen = os_strlen(prefix);
1661 
1662 			os_strlcpy(dpp_pkhash_buf, prefix,
1663 				   sizeof(dpp_pkhash_buf));
1664 			wpa_snprintf_hex(&dpp_pkhash_buf[plen],
1665 					 sizeof(dpp_pkhash_buf) - plen,
1666 					 dpp_pkhash, SHA256_MAC_LEN);
1667 		}
1668 
1669 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s%s",
1670 			buf, ip_addr, keyid_buf, dpp_pkhash_buf);
1671 
1672 		if (hapd->msg_ctx_parent &&
1673 		    hapd->msg_ctx_parent != hapd->msg_ctx) {
1674 			wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1675 					  AP_STA_CONNECTED "%s%s%s%s",
1676 					  buf, ip_addr, keyid_buf,
1677 					  dpp_pkhash_buf);
1678 			wpa_printf(MSG_INFO, AP_STA_CONNECTED);
1679 		}
1680 	} else {
1681 #ifdef CONFIG_VENDOR_EXT
1682 		int ret = wpa_vendor_ext_ap_disconnect(hapd, buf);
1683 		if (ret != 0) {
1684 #ifdef CONFIG_LIBWPA_VENDOR
1685 			struct HostapdApCbParm hostapdApCbParm = {};
1686 			result = os_snprintf((char *)hostapdApCbParm.content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
1687 				AP_STA_DISCONNECTED "%s", buf);
1688 			if (os_snprintf_error(WIFI_HOSTAPD_CB_CONTENT_LENGTH, result)) {
1689 				wpa_printf(MSG_ERROR, "ap_sta_set_authorized AP_STA_DISCONNECTED os_snprintf_error");
1690 			} else {
1691 				hostapdApCbParm.id = 0;
1692 				wpa_printf(MSG_INFO, "%s HOSTAPD_EVENT_STA_JOIN AP_STA_DISCONNECTED %s%d", __func__,
1693                     log_buf, hostapdApCbParm.id);
1694 				HostapdEventReport(hapd->conf->iface, HOSTAPD_EVENT_STA_JOIN, (void *) &hostapdApCbParm);
1695 			}
1696 #endif
1697 		}
1698 #else
1699 		wpa_msg_only_for_cb(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1700 #ifdef CONFIG_LIBWPA_VENDOR
1701 		struct HostapdApCbParm hostapdApCbParm = {};
1702 		result = os_snprintf((char *)hostapdApCbParm.content, WIFI_HOSTAPD_CB_CONTENT_LENGTH,
1703 			AP_STA_DISCONNECTED "%s", buf);
1704 		if (os_snprintf_error(WIFI_HOSTAPD_CB_CONTENT_LENGTH, result)) {
1705 			wpa_printf(MSG_ERROR, "ap_sta_set_authorized AP_STA_DISCONNECTED os_snprintf_error");
1706 		} else {
1707 			hostapdApCbParm.id = 0;
1708 			wpa_printf(MSG_INFO, "%s HOSTAPD_EVENT_STA_JOIN AP_STA_DISCONNECTED %s%d", __func__,
1709                 log_buf, hostapdApCbParm.id);
1710 			HostapdEventReport(hapd->conf->iface, HOSTAPD_EVENT_STA_JOIN, (void *) &hostapdApCbParm);
1711 		}
1712 #endif
1713 #endif
1714 #ifdef CONFIG_LIBWPA_VENDOR
1715 #ifdef CONFIG_VENDOR_EXT
1716 		if (hapd->p2p_group != NULL && !wpa_vendor_ext_is_p2p_enhance_mode(hapd->msg_ctx)) {
1717 #endif
1718 			struct P2pStaConnectStateParam p2pStaConnectStateParam;
1719 			p2pStaConnectStateParam.state = 0;
1720 			os_memcpy(p2pStaConnectStateParam.srcAddress, sta->addr, ETH_ALEN);
1721 #ifdef CONFIG_WIFI_RPT
1722 			if (hapd->p2p != NULL && hapd->p2p->p2p_rpt == TRUE) {
1723 				os_memcpy(p2pStaConnectStateParam.p2pDeviceAddress, sta->addr, ETH_ALEN);
1724 			} else
1725 #endif /* CONFIG_WIFI_RPT */
1726 			if (dev_addr) {
1727 				os_memcpy(p2pStaConnectStateParam.p2pDeviceAddress, dev_addr, ETH_ALEN);
1728 			} else {
1729 				wpa_printf(MSG_INFO, "dev_addr is null for sta_connect_state 0");
1730 			}
1731 			wpa_printf(MSG_INFO, "WPA_EVENT_STA_CONNECT_STATE 0 " MACSTR_SEC " p2p_dev_addr=" MACSTR_SEC,
1732 				MAC2STR_SEC(p2pStaConnectStateParam.srcAddress), MAC2STR_SEC(p2pStaConnectStateParam.p2pDeviceAddress));
1733 			WpaEventReport(((struct wpa_supplicant *) hapd->msg_ctx)->ifname, WPA_EVENT_STA_CONNECT_STATE,
1734 				(void *) &p2pStaConnectStateParam);
1735 #ifdef CONFIG_VENDOR_EXT
1736 		}
1737 #endif
1738 #endif
1739 		wpa_printf(MSG_INFO, AP_STA_DISCONNECTED);
1740 
1741 		if (hapd->msg_ctx_parent &&
1742 		    hapd->msg_ctx_parent != hapd->msg_ctx) {
1743 			wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1744 					  AP_STA_DISCONNECTED "%s", buf);
1745 			wpa_printf(MSG_INFO, AP_STA_DISCONNECTED);
1746 		}
1747 	}
1748 
1749 	if (hapd->sta_authorized_cb)
1750 		hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx, sta->addr, authorized,
1751 #ifdef CONFIG_WIFI_RPT
1752 			(dev_addr ? dev_addr : sta->addr),
1753 #else
1754 			dev_addr,
1755 #endif /* CONFIG_WIFI_RPT */
1756 			ip_ptr);
1757 
1758 #ifdef CONFIG_FST
1759 	if (hapd->iface->fst) {
1760 		if (authorized)
1761 			fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1762 		else
1763 			fst_notify_peer_disconnected(hapd->iface->fst,
1764 						     sta->addr);
1765 	}
1766 #endif /* CONFIG_FST */
1767 }
1768 
1769 
ap_sta_set_authorized(struct hostapd_data * hapd,struct sta_info * sta,int authorized)1770 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1771 			   int authorized)
1772 {
1773 	if (!ap_sta_set_authorized_flag(hapd, sta, authorized))
1774 		return;
1775 	ap_sta_set_authorized_event(hapd, sta, authorized);
1776 }
1777 
1778 
ap_sta_disconnect(struct hostapd_data * hapd,struct sta_info * sta,const u8 * addr,u16 reason)1779 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1780 		       const u8 *addr, u16 reason)
1781 {
1782 	if (sta) {
1783 		wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR_SEC " reason=%u",
1784 			   hapd->conf->iface, __func__, MAC2STR_SEC(sta->addr),
1785 			   reason);
1786 #ifdef CONFIG_P2P_CHR
1787 		wpa_supplicant_upload_go_p2p_state(hapd, sta->addr,
1788 			P2P_INTERFACE_STATE_DISCONNECTED, reason);
1789 #endif
1790 	} else if (addr) {
1791 		wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR_SEC " reason=%u",
1792 			   hapd->conf->iface, __func__, MAC2STR_SEC(addr),
1793 			   reason);
1794 #ifdef CONFIG_P2P_CHR
1795 		wpa_supplicant_upload_go_p2p_state(hapd, addr,
1796 			P2P_INTERFACE_STATE_DISCONNECTED, reason);
1797 #endif
1798 	}
1799 	if (sta == NULL && addr)
1800 		sta = ap_get_sta(hapd, addr);
1801 
1802 	if (addr)
1803 		hostapd_drv_sta_deauth(hapd, addr, reason);
1804 
1805 	if (sta == NULL)
1806 		return;
1807 	ap_sta_set_authorized(hapd, sta, 0);
1808 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1809 	hostapd_set_sta_flags(hapd, sta);
1810 	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1811 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1812 	wpa_printf(MSG_EXCESSIVE, "%s: %s: reschedule ap_handle_timer timeout "
1813 		   "for " MACSTR_SEC " (%d seconds - "
1814 		   "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1815 		   hapd->conf->iface, __func__, MAC2STR_SEC(sta->addr),
1816 		   AP_MAX_INACTIVITY_AFTER_DEAUTH);
1817 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1818 	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1819 			       ap_handle_timer, hapd, sta);
1820 	sta->timeout_next = STA_REMOVE;
1821 
1822 	if (hapd->iface->current_mode &&
1823 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1824 		/* Deauthentication is not used in DMG/IEEE 802.11ad;
1825 		 * disassociate the STA instead. */
1826 		sta->disassoc_reason = reason;
1827 		sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1828 		eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1829 		eloop_register_timeout(hapd->iface->drv_flags &
1830 				       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ?
1831 				       2 : 0, 0, ap_sta_disassoc_cb_timeout,
1832 				       hapd, sta);
1833 		return;
1834 	}
1835 
1836 	sta->deauth_reason = reason;
1837 	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1838 	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1839 	eloop_register_timeout(hapd->iface->drv_flags &
1840 			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1841 			       ap_sta_deauth_cb_timeout, hapd, sta);
1842 }
1843 
1844 
ap_sta_deauth_cb(struct hostapd_data * hapd,struct sta_info * sta)1845 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1846 {
1847 	if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1848 		wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1849 		return;
1850 	}
1851 	sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1852 	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1853 	ap_sta_deauth_cb_timeout(hapd, sta);
1854 }
1855 
1856 
ap_sta_disassoc_cb(struct hostapd_data * hapd,struct sta_info * sta)1857 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1858 {
1859 	if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1860 		wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1861 		return;
1862 	}
1863 	sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1864 	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1865 	ap_sta_disassoc_cb_timeout(hapd, sta);
1866 }
1867 
1868 
ap_sta_clear_disconnect_timeouts(struct hostapd_data * hapd,struct sta_info * sta)1869 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1870 				      struct sta_info *sta)
1871 {
1872 	if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1873 		wpa_printf(MSG_DEBUG,
1874 			   "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1875 			   MACSTR_SEC,
1876 			   hapd->conf->iface, MAC2STR_SEC(sta->addr));
1877 	if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1878 		wpa_printf(MSG_DEBUG,
1879 			   "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1880 			   MACSTR_SEC,
1881 			   hapd->conf->iface, MAC2STR_SEC(sta->addr));
1882 	if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1883 	{
1884 		wpa_printf(MSG_EXCESSIVE,
1885 			   "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1886 			   MACSTR_SEC,
1887 			   hapd->conf->iface, MAC2STR_SEC(sta->addr));
1888 		if (sta->flags & WLAN_STA_WPS)
1889 			hostapd_wps_eap_completed(hapd);
1890 	}
1891 }
1892 
1893 
ap_sta_flags_txt(u32 flags,char * buf,size_t buflen)1894 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1895 {
1896 	int res;
1897 
1898 	buf[0] = '\0';
1899 	res = os_snprintf(buf, buflen,
1900 			  "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1901 			  (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1902 			  (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1903 			  (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1904 			  (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1905 			   ""),
1906 			  (flags & WLAN_STA_SHORT_PREAMBLE ?
1907 			   "[SHORT_PREAMBLE]" : ""),
1908 			  (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1909 			  (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1910 			  (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1911 			  (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1912 			  (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1913 			  (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1914 			  (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1915 			  (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1916 			  (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1917 			  (flags & WLAN_STA_HT ? "[HT]" : ""),
1918 			  (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1919 			  (flags & WLAN_STA_HE ? "[HE]" : ""),
1920 			  (flags & WLAN_STA_EHT ? "[EHT]" : ""),
1921 			  (flags & WLAN_STA_6GHZ ? "[6GHZ]" : ""),
1922 			  (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1923 			  (flags & WLAN_STA_WNM_SLEEP_MODE ?
1924 			   "[WNM_SLEEP_MODE]" : ""));
1925 	if (os_snprintf_error(buflen, res))
1926 		res = -1;
1927 
1928 	return res;
1929 }
1930 
1931 
ap_sta_delayed_1x_auth_fail_cb(void * eloop_ctx,void * timeout_ctx)1932 static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1933 {
1934 	/* Only P2P_GO in WSC process invokes this function */
1935 	struct hostapd_data *hapd = eloop_ctx;
1936 	struct sta_info *sta = timeout_ctx;
1937 	u16 reason;
1938 
1939 	wpa_msg_only_for_cb(hapd->msg_ctx, MSG_DEBUG,
1940 		"IEEE 802.1X: Scheduled disconnection of " MACSTR
1941 		" after EAP-Failure", MAC2STR(sta->addr));
1942 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Scheduled disconnection of " MACSTR_SEC
1943 		" after EAP-Failure", MAC2STR_SEC(sta->addr));
1944 
1945 	reason = sta->disconnect_reason_code;
1946 	if (!reason)
1947 		reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1948 	ap_sta_disconnect(hapd, sta, sta->addr, reason);
1949 	/**
1950 	 * The sending EAP-FAIL message indicates that it's the ending of
1951 	 * P2P GP WSC process. Set WLAN_STA_P2PGO_WPS_NO_SECOND_DISASSOC
1952 	 * so that not delivered DISASSOC repeatedly.
1953 	*/
1954 	sta->flags |= WLAN_STA_P2PGO_WPS_NO_SECOND_DISASSOC;
1955 	if (sta->flags & WLAN_STA_WPS)
1956 		hostapd_wps_eap_completed(hapd);
1957 }
1958 
1959 
ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data * hapd,struct sta_info * sta,unsigned timeout)1960 void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1961 					    struct sta_info *sta,
1962 					    unsigned timeout)
1963 {
1964 	wpa_msg_only_for_cb(hapd->msg_ctx, MSG_DEBUG,
1965 		"IEEE 802.1X: Force disconnection of " MACSTR
1966 		" after EAP-Failure in %u ms", MAC2STR(sta->addr), timeout);
1967 	wpa_printf(MSG_DEBUG, "IEEE 802.1X: Force disconnection of " MACSTR_SEC
1968 		" after EAP-Failure in %u ms", MAC2STR_SEC(sta->addr), timeout);
1969 
1970 	/*
1971 	 * Add a small sleep to increase likelihood of previously requested
1972 	 * EAP-Failure TX getting out before this should the driver reorder
1973 	 * operations.
1974 	 */
1975 	eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
1976 	eloop_register_timeout(0, timeout * 1000,
1977 			       ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
1978 }
1979 
1980 
ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data * hapd,struct sta_info * sta)1981 int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1982 						   struct sta_info *sta)
1983 {
1984 	return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1985 					   hapd, sta);
1986 }
1987 
1988 
1989 #ifdef CONFIG_IEEE80211BE
ap_sta_remove_link_sta(struct hostapd_data * hapd,struct sta_info * sta)1990 static void ap_sta_remove_link_sta(struct hostapd_data *hapd,
1991 				   struct sta_info *sta)
1992 {
1993 	struct hostapd_data *tmp_hapd;
1994 
1995 	for_each_mld_link(tmp_hapd, hapd) {
1996 		struct sta_info *tmp_sta;
1997 
1998 		if (hapd == tmp_hapd)
1999 			continue;
2000 
2001 		for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
2002 		     tmp_sta = tmp_sta->next) {
2003 			if (tmp_sta == sta ||
2004 			    !ether_addr_equal(tmp_sta->addr, sta->addr))
2005 				continue;
2006 
2007 			ap_free_sta(tmp_hapd, tmp_sta);
2008 			break;
2009 		}
2010 	}
2011 }
2012 #endif /* CONFIG_IEEE80211BE */
2013 
2014 
ap_sta_re_add(struct hostapd_data * hapd,struct sta_info * sta)2015 int ap_sta_re_add(struct hostapd_data *hapd, struct sta_info *sta)
2016 {
2017 	const u8 *mld_link_addr = NULL;
2018 	bool mld_link_sta = false;
2019 
2020 	/*
2021 	 * If a station that is already associated to the AP, is trying to
2022 	 * authenticate again, remove the STA entry, in order to make sure the
2023 	 * STA PS state gets cleared and configuration gets updated. To handle
2024 	 * this, station's added_unassoc flag is cleared once the station has
2025 	 * completed association.
2026 	 */
2027 
2028 #ifdef CONFIG_IEEE80211BE
2029 	if (ap_sta_is_mld(hapd, sta)) {
2030 		u8 mld_link_id = hapd->mld_link_id;
2031 
2032 		mld_link_sta = sta->mld_assoc_link_id != mld_link_id;
2033 		mld_link_addr = sta->mld_info.links[mld_link_id].peer_addr;
2034 
2035 		/*
2036 		 * In case the AP is affiliated with an AP MLD, we need to
2037 		 * remove the station from all relevant links/APs.
2038 		 */
2039 		ap_sta_remove_link_sta(hapd, sta);
2040 	}
2041 #endif /* CONFIG_IEEE80211BE */
2042 
2043 	ap_sta_set_authorized(hapd, sta, 0);
2044 	hostapd_drv_sta_remove(hapd, sta->addr);
2045 	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH | WLAN_STA_AUTHORIZED);
2046 
2047 	if (hostapd_sta_add(hapd, sta->addr, 0, 0,
2048 			    sta->supported_rates,
2049 			    sta->supported_rates_len,
2050 			    0, NULL, NULL, NULL, 0, NULL, 0, NULL,
2051 			    sta->flags, 0, 0, 0, 0,
2052 			    mld_link_addr, mld_link_sta)) {
2053 		hostapd_logger(hapd, sta->addr,
2054 			       HOSTAPD_MODULE_IEEE80211,
2055 			       HOSTAPD_LEVEL_NOTICE,
2056 			       "Could not add STA to kernel driver");
2057 		return -1;
2058 	}
2059 
2060 	sta->added_unassoc = 1;
2061 	return 0;
2062 }
2063 
2064 
2065 #ifdef CONFIG_IEEE80211BE
ap_sta_free_sta_profile(struct mld_info * info)2066 void ap_sta_free_sta_profile(struct mld_info *info)
2067 {
2068 	int i;
2069 
2070 	if (!info)
2071 		return;
2072 
2073 	for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
2074 		os_free(info->links[i].resp_sta_profile);
2075 		info->links[i].resp_sta_profile = NULL;
2076 	}
2077 }
2078 #endif /* CONFIG_IEEE80211BE */
2079