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