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