blob: 9d49569c8e831075286869f5f250211fa9c332cd [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Station table
Dmitry Shmidtd2986c22017-10-23 14:22:09 -07003 * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080015#include "common/sae.h"
Hai Shalom021b0b52019-04-10 11:17:58 -070016#include "common/dpp.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "radius/radius.h"
18#include "radius/radius_client.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070019#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080020#include "fst/fst.h"
Dmitry Shmidtd2986c22017-10-23 14:22:09 -070021#include "crypto/crypto.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "hostapd.h"
23#include "accounting.h"
24#include "ieee802_1x.h"
25#include "ieee802_11.h"
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080026#include "ieee802_11_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#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"
Dmitry Shmidt04949592012-07-19 12:16:46 -070035#include "gas_serv.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080036#include "wnm_ap.h"
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080037#include "mbo_ap.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080038#include "ndisc_snoop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#include "sta_info.h"
Dmitry Shmidt57c2d392016-02-23 13:40:19 -080040#include "vlan.h"
Dmitry Shmidt29333592017-01-09 12:27:11 -080041#include "wps_hostapd.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070042
43static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
44 struct sta_info *sta);
45static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080046static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080047static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
48static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070049static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080050static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
Dmitry Shmidt29333592017-01-09 12:27:11 -080051static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
53int 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
69struct 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
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070080#ifdef CONFIG_P2P
81struct 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
Sunil Ravib0ac25f2024-07-12 01:42:03 +000095 if (ether_addr_equal(p2p_dev_addr, addr))
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070096 return sta;
97 }
98
99 return NULL;
100}
101#endif /* CONFIG_P2P */
102
103
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700104static 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
124void 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
131static 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 &&
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000143 !ether_addr_equal(s->hnext->addr, sta->addr))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700144 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
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800153void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
154{
155 sta_ip6addr_del(hapd, sta);
156}
157
158
Hai Shalom60840252021-02-19 19:02:11 -0800159#ifdef CONFIG_PASN
160
161void 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
Sunil Ravi99c035e2024-07-12 01:42:03 +0000183 pasn_data_deinit(sta->pasn);
Hai Shalom60840252021-02-19 19:02:11 -0800184 sta->pasn = NULL;
185 }
186}
187
188#endif /* CONFIG_PASN */
189
Sunil Ravi99c035e2024-07-12 01:42:03 +0000190
191static 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
Sunil Ravi7f769292024-07-23 22:21:32 +0000203#ifdef CONFIG_IEEE80211BE
204static 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225void 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);
Hai Shalomfdcde762020-04-02 11:19:20 -0700233 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234
Sunil Ravi640215c2023-06-28 23:08:09 +0000235 if ((sta->flags & WLAN_STA_WDS) ||
236 (sta->flags & WLAN_STA_MULTI_AP &&
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000237 (hapd->conf->multi_ap & BACKHAUL_BSS) &&
Sunil Ravi7f769292024-07-23 22:21:32 +0000238 hapd->conf->wds_sta &&
Sunil Ravi640215c2023-06-28 23:08:09 +0000239 !(sta->flags & WLAN_STA_WPS)))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700240 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700241
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800242 if (sta->ipaddr)
243 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
244 ap_sta_ip6addr_del(hapd, sta);
245
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800246 if (!hapd->iface->driver_ap_teardown &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800247 !(sta->flags & WLAN_STA_PREAUTH)) {
Sunil Ravi99c035e2024-07-12 01:42:03 +0000248 __ap_free_sta(hapd, sta);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800249 sta->added_unassoc = 0;
250 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700251
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--;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700270 if (hapd->iface->current_mode &&
271 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 && 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--;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700279 if (hapd->iface->current_mode &&
280 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 && 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
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700300#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
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700307 ht40_intolerant_remove(hapd->iface, sta);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700308
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309#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
Hai Shalomfdcde762020-04-02 11:19:20 -0700318#ifdef NEED_AP_MLME
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 if (hostapd_ht_operation_update(hapd->iface) > 0)
320 set_beacon++;
Hai Shalomfdcde762020-04-02 11:19:20 -0700321#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800323#ifdef CONFIG_MESH
324 if (hapd->mesh_sta_free_cb)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800325 hapd->mesh_sta_free_cb(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800326#endif /* CONFIG_MESH */
327
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 if (set_beacon)
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000329 ieee802_11_update_beacons(hapd->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330
Dmitry Shmidt04949592012-07-19 12:16:46 -0700331 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
332 __func__, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
334 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800335 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800336 ap_sta_clear_disconnect_timeouts(hapd, sta);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000337 ap_sta_clear_assoc_timeout(hapd, sta);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800338 sae_clear_retransmit_timer(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800340 ieee802_1x_free_station(hapd, sta);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000341
342#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000343 if (!ap_sta_is_mld(hapd, sta) ||
Sunil Ravi7f769292024-07-23 22:21:32 +0000344 hapd->mld_link_id == sta->mld_assoc_link_id) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000345 wpa_auth_sta_deinit(sta->wpa_sm);
Sunil Ravi7f769292024-07-23 22:21:32 +0000346 /* 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 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 wpa_auth_sta_deinit(sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000356#endif /* CONFIG_IEEE80211BE */
357
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 rsn_preauth_free_station(hapd, sta);
359#ifndef CONFIG_NO_RADIUS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700360 if (hapd->radius)
361 radius_client_flush_auth(hapd->radius, sta->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362#endif /* CONFIG_NO_RADIUS */
363
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800364#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 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800370 if (sta->vlan_id)
371 vlan_remove_dynamic(hapd, sta->vlan_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800372 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 &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800378 !(sta->flags & WLAN_STA_PREAUTH)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800379 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800380 sta->added_unassoc = 0;
381 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800382 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
383 }
384#endif /* CONFIG_NO_VLAN */
385
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700386 os_free(sta->challenge);
387
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 os_free(sta->sa_query_trans_id);
389 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700390
391#ifdef CONFIG_P2P
392 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
393#endif /* CONFIG_P2P */
394
Dmitry Shmidt04949592012-07-19 12:16:46 -0700395#ifdef CONFIG_INTERWORKING
396 if (sta->gas_dialog) {
397 int i;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000398
Dmitry Shmidt04949592012-07-19 12:16:46 -0700399 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700405 wpabuf_free(sta->wps_ie);
406 wpabuf_free(sta->p2p_ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800407 wpabuf_free(sta->hs20_ie);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700408 wpabuf_free(sta->roaming_consortium);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800409#ifdef CONFIG_FST
410 wpabuf_free(sta->mb_ies);
411#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412
413 os_free(sta->ht_capabilities);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800414 os_free(sta->vht_capabilities);
Hai Shalom74f70d42019-02-11 14:42:39 -0800415 os_free(sta->vht_operation);
Hai Shalom81f62d82019-07-22 12:10:00 -0700416 os_free(sta->he_capab);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700417 os_free(sta->he_6ghz_capab);
Sunil Ravia04bd252022-05-02 22:54:18 -0700418 os_free(sta->eht_capab);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800419 hostapd_free_psk_list(sta->psk);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700420 os_free(sta->identity);
421 os_free(sta->radius_cui);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800422 os_free(sta->remediation_url);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700423 os_free(sta->t_c_url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800424 wpabuf_free(sta->hs20_deauth_req);
425 os_free(sta->hs20_session_info_url);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800427#ifdef CONFIG_SAE
428 sae_clear_data(sta->sae);
429 os_free(sta->sae);
430#endif /* CONFIG_SAE */
431
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800432 mbo_ap_sta_free(sta);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800433 os_free(sta->supp_op_classes);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800434
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800435#ifdef CONFIG_FILS
436 os_free(sta->fils_pending_assoc_req);
437 wpabuf_free(sta->fils_hlp_resp);
438 wpabuf_free(sta->hlp_dhcp_discover);
439 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700440#ifdef CONFIG_FILS_SK_PFS
441 crypto_ecdh_deinit(sta->fils_ecdh);
442 wpabuf_clear_free(sta->fils_dh_ss);
443 wpabuf_free(sta->fils_g_sta);
444#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800445#endif /* CONFIG_FILS */
446
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700447#ifdef CONFIG_OWE
448 bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
449 crypto_ecdh_deinit(sta->owe_ecdh);
450#endif /* CONFIG_OWE */
451
Hai Shalom021b0b52019-04-10 11:17:58 -0700452#ifdef CONFIG_DPP2
453 dpp_pfs_free(sta->dpp_pfs);
454 sta->dpp_pfs = NULL;
455#endif /* CONFIG_DPP2 */
456
Roshan Pius3a1667e2018-07-03 15:17:14 -0700457 os_free(sta->ext_capability);
458
459#ifdef CONFIG_WNM_AP
460 eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
461#endif /* CONFIG_WNM_AP */
462
Hai Shalom60840252021-02-19 19:02:11 -0800463#ifdef CONFIG_PASN
464 ap_free_sta_pasn(hapd, sta);
465#endif /* CONFIG_PASN */
466
Roshan Pius3a1667e2018-07-03 15:17:14 -0700467 os_free(sta->ifname_wds);
468
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000469#ifdef CONFIG_IEEE80211BE
470 ap_sta_free_sta_profile(&sta->mld_info);
471#endif /* CONFIG_IEEE80211BE */
472
Hai Shalomfdcde762020-04-02 11:19:20 -0700473#ifdef CONFIG_TESTING_OPTIONS
474 os_free(sta->sae_postponed_commit);
Sunil Ravia04bd252022-05-02 22:54:18 -0700475 forced_memzero(sta->last_tk, WPA_TK_MAX_LEN);
Hai Shalomfdcde762020-04-02 11:19:20 -0700476#endif /* CONFIG_TESTING_OPTIONS */
477
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700478 os_free(sta);
479}
480
481
482void hostapd_free_stas(struct hostapd_data *hapd)
483{
484 struct sta_info *sta, *prev;
485
486 sta = hapd->sta_list;
487
488 while (sta) {
489 prev = sta;
490 if (sta->flags & WLAN_STA_AUTH) {
491 mlme_deauthenticate_indication(
492 hapd, sta, WLAN_REASON_UNSPECIFIED);
493 }
494 sta = sta->next;
495 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
496 MAC2STR(prev->addr));
497 ap_free_sta(hapd, prev);
498 }
499}
500
501
Sunil Ravi99c035e2024-07-12 01:42:03 +0000502#ifdef CONFIG_IEEE80211BE
503void hostapd_free_link_stas(struct hostapd_data *hapd)
504{
505 struct sta_info *sta, *prev;
506
507 sta = hapd->sta_list;
508 while (sta) {
509 prev = sta;
510 sta = sta->next;
511
512 if (!hostapd_sta_is_link_sta(hapd, prev))
513 continue;
514
515 wpa_printf(MSG_DEBUG, "Removing link station from MLD " MACSTR,
516 MAC2STR(prev->addr));
517 ap_free_sta(hapd, prev);
518 }
519}
520#endif /* CONFIG_IEEE80211BE */
521
522
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523/**
524 * ap_handle_timer - Per STA timer handler
525 * @eloop_ctx: struct hostapd_data *
526 * @timeout_ctx: struct sta_info *
527 *
528 * This function is called to check station activity and to remove inactive
529 * stations.
530 */
531void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
532{
533 struct hostapd_data *hapd = eloop_ctx;
534 struct sta_info *sta = timeout_ctx;
535 unsigned long next_time = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700536 int reason;
Sunil Ravi7f769292024-07-23 22:21:32 +0000537 int max_inactivity = hapd->conf->ap_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700538
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800539 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
540 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700541 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700542 if (sta->timeout_next == STA_REMOVE) {
543 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
544 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
545 "local deauth request");
546 ap_free_sta(hapd, sta);
547 return;
548 }
549
Sunil Ravi7f769292024-07-23 22:21:32 +0000550 if (sta->max_idle_period)
551 max_inactivity = (sta->max_idle_period * 1024 + 999) / 1000;
552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700553 if ((sta->flags & WLAN_STA_ASSOC) &&
554 (sta->timeout_next == STA_NULLFUNC ||
555 sta->timeout_next == STA_DISASSOC)) {
556 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700557 /*
558 * Add random value to timeout so that we don't end up bouncing
559 * all stations at the same time if we have lots of associated
560 * stations that are idle (but keep re-associating).
561 */
562 int fuzz = os_random() % 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
564 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800565 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
566 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800567 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800569 /*
570 * The driver may not support this functionality.
571 * Anyway, try again after the next inactivity timeout,
572 * but do not disconnect the station now.
573 */
Sunil Ravi7f769292024-07-23 22:21:32 +0000574 next_time = max_inactivity + fuzz;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800575 } else if (inactive_sec == -ENOENT) {
576 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
577 "Station " MACSTR " has lost its driver entry",
578 MAC2STR(sta->addr));
579
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800580 /* Avoid sending client probe on removed client */
581 sta->timeout_next = STA_DISASSOC;
582 goto skip_poll;
Sunil Ravi7f769292024-07-23 22:21:32 +0000583 } else if (inactive_sec < max_inactivity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700584 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800585 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
586 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700587 MAC2STR(sta->addr), inactive_sec);
588 sta->timeout_next = STA_NULLFUNC;
Sunil Ravi7f769292024-07-23 22:21:32 +0000589 next_time = max_inactivity + fuzz - inactive_sec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800591 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
592 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700593 "inactive too long: %d sec, max allowed: %d",
594 MAC2STR(sta->addr), inactive_sec,
Sunil Ravi7f769292024-07-23 22:21:32 +0000595 max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800596
597 if (hapd->conf->skip_inactivity_poll)
598 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700599 }
600 }
601
602 if ((sta->flags & WLAN_STA_ASSOC) &&
603 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800604 !(sta->flags & WLAN_STA_PENDING_POLL) &&
605 !hapd->conf->skip_inactivity_poll) {
606 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
607 " has ACKed data poll", MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 /* data nullfunc frame poll did not produce TX errors; assume
609 * station ACKed it */
610 sta->timeout_next = STA_NULLFUNC;
Sunil Ravi7f769292024-07-23 22:21:32 +0000611 next_time = max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 }
613
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800614skip_poll:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700616 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
617 "for " MACSTR " (%lu seconds)",
618 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
620 sta);
621 return;
622 }
623
624 if (sta->timeout_next == STA_NULLFUNC &&
625 (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800626 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700627 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800628 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
629 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630 } else if (sta->timeout_next != STA_REMOVE) {
631 int deauth = sta->timeout_next == STA_DEAUTH;
632
Hai Shalom74f70d42019-02-11 14:42:39 -0800633 if (!deauth && !(sta->flags & WLAN_STA_ASSOC)) {
634 /* Cannot disassociate not-associated STA, so move
635 * directly to deauthentication. */
636 sta->timeout_next = STA_DEAUTH;
637 deauth = 1;
638 }
639
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800640 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
641 "Timeout, sending %s info to STA " MACSTR,
642 deauth ? "deauthentication" : "disassociation",
643 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644
645 if (deauth) {
646 hostapd_drv_sta_deauth(
647 hapd, sta->addr,
648 WLAN_REASON_PREV_AUTH_NOT_VALID);
649 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700650 reason = (sta->timeout_next == STA_DISASSOC) ?
651 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
652 WLAN_REASON_PREV_AUTH_NOT_VALID;
653
654 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700655 }
656 }
657
658 switch (sta->timeout_next) {
659 case STA_NULLFUNC:
660 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700661 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
662 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
663 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700664 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
665 hapd, sta);
666 break;
667 case STA_DISASSOC:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700668 case STA_DISASSOC_FROM_CLI:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800669 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700670 sta->flags &= ~WLAN_STA_ASSOC;
Hai Shalomfdcde762020-04-02 11:19:20 -0700671 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
673 if (!sta->acct_terminate_cause)
674 sta->acct_terminate_cause =
675 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
676 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800677 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700678 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
679 HOSTAPD_LEVEL_INFO, "disassociated due to "
680 "inactivity");
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700681 reason = (sta->timeout_next == STA_DISASSOC) ?
682 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
683 WLAN_REASON_PREV_AUTH_NOT_VALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700684 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700685 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
686 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
687 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700688 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
689 hapd, sta);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700690 mlme_disassociate_indication(hapd, sta, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700691 break;
692 case STA_DEAUTH:
693 case STA_REMOVE:
694 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
695 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800696 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 if (!sta->acct_terminate_cause)
698 sta->acct_terminate_cause =
699 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
700 mlme_deauthenticate_indication(
701 hapd, sta,
702 WLAN_REASON_PREV_AUTH_NOT_VALID);
703 ap_free_sta(hapd, sta);
704 break;
705 }
706}
707
708
709static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
710{
711 struct hostapd_data *hapd = eloop_ctx;
712 struct sta_info *sta = timeout_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800714 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
715 hapd->conf->iface, MAC2STR(sta->addr));
Hai Shalomfdcde762020-04-02 11:19:20 -0700716 if (!(sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC |
717 WLAN_STA_AUTHORIZED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700718 if (sta->flags & WLAN_STA_GAS) {
719 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
720 "entry " MACSTR, MAC2STR(sta->addr));
721 ap_free_sta(hapd, sta);
722 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700724 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700726 hostapd_drv_sta_deauth(hapd, sta->addr,
727 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700728 mlme_deauthenticate_indication(hapd, sta,
729 WLAN_REASON_PREV_AUTH_NOT_VALID);
730 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
731 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
732 "session timeout");
733 sta->acct_terminate_cause =
734 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735 ap_free_sta(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700736}
737
738
Dmitry Shmidt54605472013-11-08 11:10:19 -0800739void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
740 u32 session_timeout)
741{
742 if (eloop_replenish_timeout(session_timeout, 0,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800743 ap_handle_session_timer, hapd, sta) == 1) {
Dmitry Shmidt54605472013-11-08 11:10:19 -0800744 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
745 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
746 "to %d seconds", session_timeout);
747 }
748}
749
750
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700751void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
752 u32 session_timeout)
753{
754 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
755 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
756 "seconds", session_timeout);
757 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
758 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
759 hapd, sta);
760}
761
762
763void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
764{
765 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
766}
767
768
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800769static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
770{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700771#ifdef CONFIG_WNM_AP
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800772 struct hostapd_data *hapd = eloop_ctx;
773 struct sta_info *sta = timeout_ctx;
774
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800775 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
776 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800777 if (sta->hs20_session_info_url == NULL)
778 return;
779
780 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
781 sta->hs20_disassoc_timer);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700782#endif /* CONFIG_WNM_AP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800783}
784
785
786void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
787 struct sta_info *sta, int warning_time)
788{
789 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
790 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
791 hapd, sta);
792}
793
794
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000795static void ap_sta_assoc_timeout(void *eloop_ctx, void *timeout_ctx)
796{
797 struct hostapd_data *hapd = eloop_ctx;
798 struct sta_info *sta = timeout_ctx;
799
800 if (sta->flags & WLAN_STA_ASSOC)
801 return;
802
803 wpa_printf(MSG_DEBUG, "STA " MACSTR
804 " did not complete association in time - remove it",
805 MAC2STR(sta->addr));
806 if (sta->flags & WLAN_STA_AUTH)
807 ap_sta_deauthenticate(hapd, sta,
808 WLAN_REASON_PREV_AUTH_NOT_VALID);
809 else
810 ap_free_sta(hapd, sta);
811}
812
813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700814struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
815{
816 struct sta_info *sta;
Hai Shalom81f62d82019-07-22 12:10:00 -0700817 int i;
Sunil Ravi7f769292024-07-23 22:21:32 +0000818 int max_inactivity = hapd->conf->ap_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819
820 sta = ap_get_sta(hapd, addr);
821 if (sta)
822 return sta;
823
824 wpa_printf(MSG_DEBUG, " New STA");
825 if (hapd->num_sta >= hapd->conf->max_num_sta) {
826 /* FIX: might try to remove some old STAs first? */
827 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
828 hapd->num_sta, hapd->conf->max_num_sta);
829 return NULL;
830 }
831
832 sta = os_zalloc(sizeof(struct sta_info));
833 if (sta == NULL) {
834 wpa_printf(MSG_ERROR, "malloc failed");
835 return NULL;
836 }
837 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800838 if (accounting_sta_get_id(hapd, sta) < 0) {
839 os_free(sta);
840 return NULL;
841 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700842
Hai Shalom81f62d82019-07-22 12:10:00 -0700843 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
844 if (!hapd->iface->basic_rates)
845 break;
846 if (hapd->iface->basic_rates[i] < 0)
847 break;
848 sta->supported_rates[i] = hapd->iface->basic_rates[i] / 5;
849 }
850 sta->supported_rates_len = i;
851
Sunil Ravi7f769292024-07-23 22:21:32 +0000852 if (sta->max_idle_period)
853 max_inactivity = (sta->max_idle_period * 1024 + 999) / 1000;
854
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800855 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
856 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
857 "for " MACSTR " (%d seconds - ap_max_inactivity)",
858 __func__, MAC2STR(addr),
Sunil Ravi7f769292024-07-23 22:21:32 +0000859 max_inactivity);
860 eloop_register_timeout(max_inactivity, 0,
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800861 ap_handle_timer, hapd, sta);
862 }
863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 /* initialize STA info data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700865 os_memcpy(sta->addr, addr, ETH_ALEN);
866 sta->next = hapd->sta_list;
867 hapd->sta_list = sta;
868 hapd->num_sta++;
869 ap_sta_hash_add(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700870 ap_sta_remove_in_other_bss(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800871 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
872 dl_list_init(&sta->ip6addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700873
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700874#ifdef CONFIG_TAXONOMY
875 sta_track_claim_taxonomy_info(hapd->iface, addr,
876 &sta->probe_ie_taxonomy);
877#endif /* CONFIG_TAXONOMY */
878
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000879 if (!(hapd->conf->mesh & MESH_ENABLED))
880 eloop_register_timeout(60, 0, ap_sta_assoc_timeout, hapd, sta);
881
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 return sta;
883}
884
885
886static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
887{
888 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
889
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800890 if (sta->ipaddr)
891 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
892 ap_sta_ip6addr_del(hapd, sta);
893
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800894 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
895 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
897 sta->flags & WLAN_STA_ASSOC) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800898 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
899 " from kernel driver",
900 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700901 return -1;
902 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800903 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 return 0;
905}
906
907
908static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
909 struct sta_info *sta)
910{
911 struct hostapd_iface *iface = hapd->iface;
912 size_t i;
913
914 for (i = 0; i < iface->num_bss; i++) {
915 struct hostapd_data *bss = iface->bss[i];
916 struct sta_info *sta2;
917 /* bss should always be set during operation, but it may be
918 * NULL during reconfiguration. Assume the STA is not
919 * associated to another BSS in that case to avoid NULL pointer
920 * dereferences. */
921 if (bss == hapd || bss == NULL)
922 continue;
923 sta2 = ap_get_sta(bss, sta->addr);
924 if (!sta2)
925 continue;
926
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800927 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
928 " association from another BSS %s",
929 hapd->conf->iface, MAC2STR(sta2->addr),
930 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700931 ap_sta_disconnect(bss, sta2, sta2->addr,
932 WLAN_REASON_PREV_AUTH_NOT_VALID);
933 }
934}
935
936
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800937static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
938{
939 struct hostapd_data *hapd = eloop_ctx;
940 struct sta_info *sta = timeout_ctx;
941
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800942 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
943 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800944 ap_sta_remove(hapd, sta);
945 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
946}
947
948
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000949static void ap_sta_disconnect_common(struct hostapd_data *hapd,
950 struct sta_info *sta, unsigned int timeout)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800952 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000953
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800954 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700955 hostapd_set_sta_flags(hapd, sta);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000956
957 wpa_printf(MSG_DEBUG,
958 "reschedule ap_handle_timer timeout (%u sec) for " MACSTR,
959 MAC2STR(sta->addr), timeout);
960
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700961 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000962 eloop_register_timeout(timeout, 0, ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700963 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800964 ieee802_1x_free_station(hapd, sta);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000965#ifdef CONFIG_IEEE80211BE
966 if (!hapd->conf->mld_ap ||
Sunil Ravi7f769292024-07-23 22:21:32 +0000967 hapd->mld_link_id == sta->mld_assoc_link_id) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000968 wpa_auth_sta_deinit(sta->wpa_sm);
Sunil Ravi7f769292024-07-23 22:21:32 +0000969 clear_wpa_sm_for_each_partner_link(hapd, sta);
970 }
971#else /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -0700972 wpa_auth_sta_deinit(sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000973#endif /* CONFIG_IEEE80211BE */
974
Hai Shalom81f62d82019-07-22 12:10:00 -0700975 sta->wpa_sm = NULL;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000976}
977
978
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000979static void ap_sta_disassociate_common(struct hostapd_data *hapd,
980 struct sta_info *sta, u16 reason)
981{
982 sta->disassoc_reason = reason;
983 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
984 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
985 eloop_register_timeout(hapd->iface->drv_flags &
986 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
987 ap_sta_disassoc_cb_timeout, hapd, sta);
988}
989
990
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000991static void ap_sta_handle_disassociate(struct hostapd_data *hapd,
992 struct sta_info *sta, u16 reason)
993{
994 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
995 hapd->conf->iface, MAC2STR(sta->addr));
996
997 if (hapd->iface->current_mode &&
998 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
999 /* Skip deauthentication in DMG/IEEE 802.11ad */
1000 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
1001 WLAN_STA_ASSOC_REQ_OK);
1002 sta->timeout_next = STA_REMOVE;
1003 } else {
1004 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1005 sta->timeout_next = STA_DEAUTH;
1006 }
1007
1008 ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DISASSOC);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001009 ap_sta_disassociate_common(hapd, sta, reason);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001010}
1011
1012
1013static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
1014{
1015 struct hostapd_data *hapd = eloop_ctx;
1016 struct sta_info *sta = timeout_ctx;
1017
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001018 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
1019 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001020 ap_sta_remove(hapd, sta);
1021 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001022}
1023
1024
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001025static void ap_sta_deauthenticate_common(struct hostapd_data *hapd,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001026 struct sta_info *sta, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001028 sta->deauth_reason = reason;
1029 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1030 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1031 eloop_register_timeout(hapd->iface->drv_flags &
1032 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1033 ap_sta_deauth_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001034}
1035
1036
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001037static void ap_sta_handle_deauthenticate(struct hostapd_data *hapd,
1038 struct sta_info *sta, u16 reason)
1039{
1040 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
1041 hapd->conf->iface, MAC2STR(sta->addr));
1042
1043 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1044
1045 sta->timeout_next = STA_REMOVE;
1046 ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DEAUTH);
1047 ap_sta_deauthenticate_common(hapd, sta, reason);
1048}
1049
1050
1051static void ap_sta_handle_disconnect(struct hostapd_data *hapd,
1052 struct sta_info *sta, u16 reason)
1053{
1054 wpa_printf(MSG_DEBUG, "%s: disconnect STA " MACSTR,
1055 hapd->conf->iface, MAC2STR(sta->addr));
1056
1057 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1058 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1059 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1060 sta->timeout_next = STA_REMOVE;
1061
1062 sta->deauth_reason = reason;
1063 ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DEAUTH);
1064 ap_sta_deauthenticate_common(hapd, sta, reason);
1065}
1066
1067
1068enum ap_sta_disconnect_op {
1069 AP_STA_DEAUTHENTICATE,
1070 AP_STA_DISASSOCIATE,
1071 AP_STA_DISCONNECT
1072};
1073
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001074static bool ap_sta_ml_disconnect(struct hostapd_data *hapd,
1075 struct sta_info *sta, u16 reason,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001076 enum ap_sta_disconnect_op op)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001077{
1078#ifdef CONFIG_IEEE80211BE
1079 struct hostapd_data *assoc_hapd, *tmp_hapd;
1080 struct sta_info *assoc_sta;
1081 unsigned int i, link_id;
1082 struct hapd_interfaces *interfaces;
1083
1084 if (!hostapd_is_mld_ap(hapd))
1085 return false;
1086
1087 /*
1088 * Get the station on which the association was performed, as it holds
1089 * the information about all the other links.
1090 */
1091 assoc_sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
1092 if (!assoc_sta)
1093 return false;
1094 interfaces = assoc_hapd->iface->interfaces;
1095
1096 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001097 if (!assoc_sta->mld_info.links[link_id].valid)
1098 continue;
1099
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001100 for (i = 0; i < interfaces->count; i++) {
1101 struct sta_info *tmp_sta;
1102
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001103 tmp_hapd = interfaces->iface[i]->bss[0];
1104
Sunil Ravi99c035e2024-07-12 01:42:03 +00001105 if (!hostapd_is_ml_partner(tmp_hapd, assoc_hapd))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001106 continue;
1107
1108 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
1109 tmp_sta = tmp_sta->next) {
1110 /*
1111 * Handle the station on which the association
1112 * was done only after all other link station
1113 * are removed. Since there is a only a single
1114 * station per hapd with the same association
1115 * link simply break;
1116 */
1117 if (tmp_sta == assoc_sta)
1118 break;
1119
1120 if (tmp_sta->mld_assoc_link_id !=
1121 assoc_sta->mld_assoc_link_id ||
1122 tmp_sta->aid != assoc_sta->aid)
1123 continue;
1124
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001125 if (op == AP_STA_DISASSOCIATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001126 ap_sta_handle_disassociate(tmp_hapd,
1127 tmp_sta,
1128 reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001129 else if (op == AP_STA_DEAUTHENTICATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001130 ap_sta_handle_deauthenticate(tmp_hapd,
1131 tmp_sta,
1132 reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001133 else
1134 ap_sta_handle_disconnect(tmp_hapd,
1135 tmp_sta,
1136 reason);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001137 break;
1138 }
1139 }
1140 }
1141
1142 /* Disconnect the station on which the association was performed. */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001143 if (op == AP_STA_DISASSOCIATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001144 ap_sta_handle_disassociate(assoc_hapd, assoc_sta, reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001145 else if (op == AP_STA_DEAUTHENTICATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001146 ap_sta_handle_deauthenticate(assoc_hapd, assoc_sta, reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001147 else
1148 ap_sta_handle_disconnect(assoc_hapd, assoc_sta, reason);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001149
1150 return true;
1151#else /* CONFIG_IEEE80211BE */
1152 return false;
1153#endif /* CONFIG_IEEE80211BE */
1154}
1155
1156
1157void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
1158 u16 reason)
1159{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001160 if (ap_sta_ml_disconnect(hapd, sta, reason, AP_STA_DISASSOCIATE))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001161 return;
1162
1163 ap_sta_handle_disassociate(hapd, sta, reason);
1164}
1165
1166
1167void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
1168 u16 reason)
1169{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001170 if (hapd->iface->current_mode &&
1171 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1172 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1173 * disassociate the STA instead. */
1174 ap_sta_disassociate(hapd, sta, reason);
1175 return;
1176 }
1177
1178 if (ap_sta_ml_disconnect(hapd, sta, reason, AP_STA_DEAUTHENTICATE))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001179 return;
1180
1181 ap_sta_handle_deauthenticate(hapd, sta, reason);
1182}
1183
1184
Dmitry Shmidt04949592012-07-19 12:16:46 -07001185#ifdef CONFIG_WPS
1186int ap_sta_wps_cancel(struct hostapd_data *hapd,
1187 struct sta_info *sta, void *ctx)
1188{
1189 if (sta && (sta->flags & WLAN_STA_WPS)) {
1190 ap_sta_deauthenticate(hapd, sta,
1191 WLAN_REASON_PREV_AUTH_NOT_VALID);
1192 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
1193 __func__, MAC2STR(sta->addr));
1194 return 1;
1195 }
1196
1197 return 0;
1198}
1199#endif /* CONFIG_WPS */
1200
1201
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001202static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
1203{
1204 struct hostapd_vlan *vlan;
1205 int vlan_id = MAX_VLAN_ID + 2;
1206
1207retry:
1208 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1209 if (vlan->vlan_id == vlan_id) {
1210 vlan_id++;
1211 goto retry;
1212 }
1213 }
1214 return vlan_id;
1215}
1216
1217
1218int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
1219 struct vlan_description *vlan_desc)
1220{
1221 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
1222 int old_vlan_id, vlan_id = 0, ret = 0;
1223
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001224 /* Check if there is something to do */
1225 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
1226 /* This sta is lacking its own vif */
1227 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
1228 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
1229 /* sta->vlan_id needs to be reset */
1230 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
1231 return 0; /* nothing to change */
1232 }
1233
1234 /* Now the real VLAN changed or the STA just needs its own vif */
1235 if (hapd->conf->ssid.per_sta_vif) {
1236 /* Assign a new vif, always */
1237 /* find a free vlan_id sufficiently big */
1238 vlan_id = ap_sta_get_free_vlan_id(hapd);
1239 /* Get wildcard VLAN */
1240 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1241 if (vlan->vlan_id == VLAN_ID_WILDCARD)
1242 break;
1243 }
1244 if (!vlan) {
1245 hostapd_logger(hapd, sta->addr,
1246 HOSTAPD_MODULE_IEEE80211,
1247 HOSTAPD_LEVEL_DEBUG,
1248 "per_sta_vif missing wildcard");
1249 vlan_id = 0;
1250 ret = -1;
1251 goto done;
1252 }
1253 } else if (vlan_desc && vlan_desc->notempty) {
1254 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1255 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
1256 break;
1257 if (vlan->vlan_id == VLAN_ID_WILDCARD)
1258 wildcard_vlan = vlan;
1259 }
1260 if (vlan) {
1261 vlan_id = vlan->vlan_id;
1262 } else if (wildcard_vlan) {
1263 vlan = wildcard_vlan;
1264 vlan_id = vlan_desc->untagged;
1265 if (vlan_desc->tagged[0]) {
1266 /* Tagged VLAN configuration */
1267 vlan_id = ap_sta_get_free_vlan_id(hapd);
1268 }
1269 } else {
1270 hostapd_logger(hapd, sta->addr,
1271 HOSTAPD_MODULE_IEEE80211,
1272 HOSTAPD_LEVEL_DEBUG,
1273 "missing vlan and wildcard for vlan=%d%s",
1274 vlan_desc->untagged,
1275 vlan_desc->tagged[0] ? "+" : "");
1276 vlan_id = 0;
1277 ret = -1;
1278 goto done;
1279 }
1280 }
1281
1282 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
1283 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
1284 if (vlan == NULL) {
1285 hostapd_logger(hapd, sta->addr,
1286 HOSTAPD_MODULE_IEEE80211,
1287 HOSTAPD_LEVEL_DEBUG,
1288 "could not add dynamic VLAN interface for vlan=%d%s",
1289 vlan_desc ? vlan_desc->untagged : -1,
1290 (vlan_desc && vlan_desc->tagged[0]) ?
1291 "+" : "");
1292 vlan_id = 0;
1293 ret = -1;
1294 goto done;
1295 }
1296
1297 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1298 HOSTAPD_LEVEL_DEBUG,
1299 "added new dynamic VLAN interface '%s'",
1300 vlan->ifname);
1301 } else if (vlan && vlan->dynamic_vlan > 0) {
1302 vlan->dynamic_vlan++;
1303 hostapd_logger(hapd, sta->addr,
1304 HOSTAPD_MODULE_IEEE80211,
1305 HOSTAPD_LEVEL_DEBUG,
1306 "updated existing dynamic VLAN interface '%s'",
1307 vlan->ifname);
1308 }
1309done:
1310 old_vlan_id = sta->vlan_id;
1311 sta->vlan_id = vlan_id;
1312 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
1313
1314 if (vlan_id != old_vlan_id && old_vlan_id)
1315 vlan_remove_dynamic(hapd, old_vlan_id);
1316
1317 return ret;
1318}
1319
1320
Dmitry Shmidt83474442015-04-15 13:47:09 -07001321int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001322{
1323#ifndef CONFIG_NO_VLAN
1324 const char *iface;
1325 struct hostapd_vlan *vlan = NULL;
1326 int ret;
Dmitry Shmidt83474442015-04-15 13:47:09 -07001327 int old_vlanid = sta->vlan_id_bound;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001328 int mld_link_id = -1;
1329
1330#ifdef CONFIG_IEEE80211BE
1331 if (hapd->conf->mld_ap)
1332 mld_link_id = hapd->mld_link_id;
1333#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334
Hai Shalomfdcde762020-04-02 11:19:20 -07001335 if ((sta->flags & WLAN_STA_WDS) && sta->vlan_id == 0) {
1336 wpa_printf(MSG_DEBUG,
1337 "Do not override WDS VLAN assignment for STA "
1338 MACSTR, MAC2STR(sta->addr));
1339 return 0;
1340 }
1341
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001342 iface = hapd->conf->iface;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001343 if (hapd->conf->ssid.vlan[0])
1344 iface = hapd->conf->ssid.vlan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001346 if (sta->vlan_id > 0) {
1347 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001348 if (vlan->vlan_id == sta->vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001350 }
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001351 if (vlan)
1352 iface = vlan->ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 }
1354
Dmitry Shmidt83474442015-04-15 13:47:09 -07001355 /*
1356 * Do not increment ref counters if the VLAN ID remains same, but do
1357 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1358 * have been called before.
1359 */
1360 if (sta->vlan_id == old_vlanid)
1361 goto skip_counting;
1362
Hai Shalomfdcde762020-04-02 11:19:20 -07001363 if (sta->vlan_id > 0 && !vlan &&
1364 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001365 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1366 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1367 "binding station to (vlan_id=%d)",
1368 sta->vlan_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001369 ret = -1;
1370 goto done;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001371 } else if (vlan && vlan->dynamic_vlan > 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001372 vlan->dynamic_vlan++;
1373 hostapd_logger(hapd, sta->addr,
1374 HOSTAPD_MODULE_IEEE80211,
1375 HOSTAPD_LEVEL_DEBUG,
1376 "updated existing dynamic VLAN interface '%s'",
1377 iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001378 }
1379
Dmitry Shmidt83474442015-04-15 13:47:09 -07001380 /* ref counters have been increased, so mark the station */
1381 sta->vlan_id_bound = sta->vlan_id;
1382
1383skip_counting:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1385 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1386 "'%s'", iface);
1387
1388 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1389 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1390
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001391 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id,
1392 mld_link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001393 if (ret < 0) {
1394 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1395 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1396 "entry to vlan_id=%d", sta->vlan_id);
1397 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001398
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001399 /* During 1x reauth, if the vlan id changes, then remove the old id. */
Dmitry Shmidt83474442015-04-15 13:47:09 -07001400 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001401 vlan_remove_dynamic(hapd, old_vlanid);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001402done:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001404 return ret;
1405#else /* CONFIG_NO_VLAN */
1406 return 0;
1407#endif /* CONFIG_NO_VLAN */
1408}
1409
1410
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001411int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1412{
1413 u32 tu;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001414 struct os_reltime now, passed;
1415 os_get_reltime(&now);
1416 os_reltime_sub(&now, &sta->sa_query_start, &passed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001417 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1418 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1419 hostapd_logger(hapd, sta->addr,
1420 HOSTAPD_MODULE_IEEE80211,
1421 HOSTAPD_LEVEL_DEBUG,
1422 "association SA Query timed out");
1423 sta->sa_query_timed_out = 1;
1424 os_free(sta->sa_query_trans_id);
1425 sta->sa_query_trans_id = NULL;
1426 sta->sa_query_count = 0;
1427 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1428 return 1;
1429 }
1430
1431 return 0;
1432}
1433
1434
1435static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1436{
1437 struct hostapd_data *hapd = eloop_ctx;
1438 struct sta_info *sta = timeout_ctx;
1439 unsigned int timeout, sec, usec;
1440 u8 *trans_id, *nbuf;
1441
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001442 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1443 " (count=%d)",
1444 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1445
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001446 if (sta->sa_query_count > 0 &&
1447 ap_check_sa_query_timeout(hapd, sta))
1448 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07001449 if (sta->sa_query_count >= 1000)
1450 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001452 nbuf = os_realloc_array(sta->sa_query_trans_id,
1453 sta->sa_query_count + 1,
1454 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 if (nbuf == NULL)
1456 return;
1457 if (sta->sa_query_count == 0) {
1458 /* Starting a new SA Query procedure */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001459 os_get_reltime(&sta->sa_query_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001460 }
1461 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1462 sta->sa_query_trans_id = nbuf;
1463 sta->sa_query_count++;
1464
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001465 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1466 /*
1467 * We don't really care which ID is used here, so simply
1468 * hardcode this if the mostly theoretical os_get_random()
1469 * failure happens.
1470 */
1471 trans_id[0] = 0x12;
1472 trans_id[1] = 0x34;
1473 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001474
1475 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1476 sec = ((timeout / 1000) * 1024) / 1000;
1477 usec = (timeout % 1000) * 1024;
1478 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1479
1480 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1481 HOSTAPD_LEVEL_DEBUG,
1482 "association SA Query attempt %d", sta->sa_query_count);
1483
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001484 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001485}
1486
1487
1488void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1489{
1490 ap_sa_query_timer(hapd, sta);
1491}
1492
1493
1494void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1495{
1496 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1497 os_free(sta->sa_query_trans_id);
1498 sta->sa_query_trans_id = NULL;
1499 sta->sa_query_count = 0;
1500}
1501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502
Hai Shalom74f70d42019-02-11 14:42:39 -08001503const char * ap_sta_wpa_get_keyid(struct hostapd_data *hapd,
1504 struct sta_info *sta)
1505{
1506 struct hostapd_wpa_psk *psk;
1507 struct hostapd_ssid *ssid;
1508 const u8 *pmk;
1509 int pmk_len;
1510
1511 ssid = &hapd->conf->ssid;
1512
1513 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1514 if (!pmk || pmk_len != PMK_LEN)
1515 return NULL;
1516
1517 for (psk = ssid->wpa_psk; psk; psk = psk->next)
1518 if (os_memcmp(pmk, psk->psk, PMK_LEN) == 0)
1519 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08001520 if (!psk || !psk->keyid[0])
1521 return NULL;
1522
1523 return psk->keyid;
1524}
1525
1526
Sunil Ravi77d572f2023-01-17 23:58:31 +00001527const u8 * ap_sta_wpa_get_dpp_pkhash(struct hostapd_data *hapd,
1528 struct sta_info *sta)
1529{
1530 return wpa_auth_get_dpp_pkhash(sta->wpa_sm);
1531}
1532
1533
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001534bool ap_sta_set_authorized_flag(struct hostapd_data *hapd, struct sta_info *sta,
1535 int authorized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001536{
1537 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001538 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001539
Sunil Ravi640215c2023-06-28 23:08:09 +00001540 if (authorized) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001541 int mld_assoc_link_id = -1;
1542
1543#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001544 if (ap_sta_is_mld(hapd, sta)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001545 if (sta->mld_assoc_link_id == hapd->mld_link_id)
1546 mld_assoc_link_id = sta->mld_assoc_link_id;
1547 else
1548 mld_assoc_link_id = -2;
1549 }
1550#endif /* CONFIG_IEEE80211BE */
1551 if (mld_assoc_link_id != -2)
1552 hostapd_prune_associations(hapd, sta->addr,
1553 mld_assoc_link_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001554 sta->flags |= WLAN_STA_AUTHORIZED;
Sunil Ravi640215c2023-06-28 23:08:09 +00001555 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001556 sta->flags &= ~WLAN_STA_AUTHORIZED;
Sunil Ravi640215c2023-06-28 23:08:09 +00001557 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001558
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001559 return true;
1560}
1561
1562
1563void ap_sta_set_authorized_event(struct hostapd_data *hapd,
1564 struct sta_info *sta, int authorized)
1565{
1566 const u8 *dev_addr = NULL;
1567 char buf[100];
1568#ifdef CONFIG_P2P
1569 u8 addr[ETH_ALEN];
1570 u8 ip_addr_buf[4];
1571#endif /* CONFIG_P2P */
Sunil Ravi99c035e2024-07-12 01:42:03 +00001572 const u8 *ip_ptr = NULL;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001573
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001574#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001575 if (hapd->p2p_group == NULL) {
1576 if (sta->p2p_ie != NULL &&
1577 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1578 dev_addr = addr;
1579 } else
1580 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001581
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001582 if (dev_addr)
1583 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1584 MAC2STR(sta->addr), MAC2STR(dev_addr));
1585 else
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001586#endif /* CONFIG_P2P */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001587 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1588
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001589 if (authorized) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001590 const u8 *dpp_pkhash;
Hai Shalom74f70d42019-02-11 14:42:39 -08001591 const char *keyid;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001592 char dpp_pkhash_buf[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001593 char keyid_buf[100];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001594 char ip_addr[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001595
Sunil Ravi77d572f2023-01-17 23:58:31 +00001596 dpp_pkhash_buf[0] = '\0';
Hai Shalom74f70d42019-02-11 14:42:39 -08001597 keyid_buf[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001598 ip_addr[0] = '\0';
1599#ifdef CONFIG_P2P
1600 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1601 os_snprintf(ip_addr, sizeof(ip_addr),
1602 " ip_addr=%u.%u.%u.%u",
1603 ip_addr_buf[0], ip_addr_buf[1],
1604 ip_addr_buf[2], ip_addr_buf[3]);
Sunil Ravid8128a22023-11-06 23:53:58 +00001605 ip_ptr = ip_addr_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001606 }
1607#endif /* CONFIG_P2P */
1608
Hai Shalom74f70d42019-02-11 14:42:39 -08001609 keyid = ap_sta_wpa_get_keyid(hapd, sta);
1610 if (keyid) {
1611 os_snprintf(keyid_buf, sizeof(keyid_buf),
1612 " keyid=%s", keyid);
1613 }
1614
Sunil Ravi77d572f2023-01-17 23:58:31 +00001615 dpp_pkhash = ap_sta_wpa_get_dpp_pkhash(hapd, sta);
1616 if (dpp_pkhash) {
1617 const char *prefix = " dpp_pkhash=";
1618 size_t plen = os_strlen(prefix);
1619
1620 os_strlcpy(dpp_pkhash_buf, prefix,
1621 sizeof(dpp_pkhash_buf));
1622 wpa_snprintf_hex(&dpp_pkhash_buf[plen],
1623 sizeof(dpp_pkhash_buf) - plen,
1624 dpp_pkhash, SHA256_MAC_LEN);
1625 }
1626
1627 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s%s",
1628 buf, ip_addr, keyid_buf, dpp_pkhash_buf);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001630 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001631 hapd->msg_ctx_parent != hapd->msg_ctx)
1632 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001633 AP_STA_CONNECTED "%s%s%s%s",
1634 buf, ip_addr, keyid_buf,
1635 dpp_pkhash_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001636 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001637 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1638
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001639 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001640 hapd->msg_ctx_parent != hapd->msg_ctx)
1641 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1642 AP_STA_DISCONNECTED "%s", buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001643 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001644
Sunil Ravid8128a22023-11-06 23:53:58 +00001645 if (hapd->sta_authorized_cb)
1646 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1647 sta->addr, authorized, dev_addr,
1648 ip_ptr);
1649
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001650#ifdef CONFIG_FST
1651 if (hapd->iface->fst) {
1652 if (authorized)
1653 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1654 else
1655 fst_notify_peer_disconnected(hapd->iface->fst,
1656 sta->addr);
1657 }
1658#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001659}
1660
1661
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001662bool ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001663 int authorized)
1664{
1665 if (!ap_sta_set_authorized_flag(hapd, sta, authorized))
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001666 return false;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001667 ap_sta_set_authorized_event(hapd, sta, authorized);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001668 return true;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001669}
1670
1671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001672void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1673 const u8 *addr, u16 reason)
1674{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001675 if (sta)
1676 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1677 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1678 reason);
1679 else if (addr)
1680 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1681 hapd->conf->iface, __func__, MAC2STR(addr),
1682 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683
1684 if (sta == NULL && addr)
1685 sta = ap_get_sta(hapd, addr);
1686
1687 if (addr)
1688 hostapd_drv_sta_deauth(hapd, addr, reason);
1689
1690 if (sta == NULL)
1691 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001692
Dmitry Shmidt29333592017-01-09 12:27:11 -08001693 if (hapd->iface->current_mode &&
1694 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1695 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1696 * disassociate the STA instead. */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001697 ap_sta_disassociate_common(hapd, sta, reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001698 return;
1699 }
1700
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001701 if (ap_sta_ml_disconnect(hapd, sta, reason, AP_STA_DISCONNECT))
1702 return;
1703
1704 ap_sta_handle_disconnect(hapd, sta, reason);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001705}
1706
1707
1708void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1709{
1710 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1711 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1712 return;
1713 }
1714 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1715 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1716 ap_sta_deauth_cb_timeout(hapd, sta);
1717}
1718
1719
1720void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1721{
1722 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1723 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1724 return;
1725 }
1726 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1727 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1728 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001729}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001730
1731
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001732void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1733 struct sta_info *sta)
1734{
1735 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1736 wpa_printf(MSG_DEBUG,
1737 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1738 MACSTR,
1739 hapd->conf->iface, MAC2STR(sta->addr));
1740 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1741 wpa_printf(MSG_DEBUG,
1742 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1743 MACSTR,
1744 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt29333592017-01-09 12:27:11 -08001745 if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1746 {
1747 wpa_printf(MSG_DEBUG,
1748 "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1749 MACSTR,
1750 hapd->conf->iface, MAC2STR(sta->addr));
1751 if (sta->flags & WLAN_STA_WPS)
1752 hostapd_wps_eap_completed(hapd);
1753 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001754}
1755
1756
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001757void ap_sta_clear_assoc_timeout(struct hostapd_data *hapd,
1758 struct sta_info *sta)
1759{
1760 eloop_cancel_timeout(ap_sta_assoc_timeout, hapd, sta);
1761}
1762
1763
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001764int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1765{
1766 int res;
1767
1768 buf[0] = '\0';
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001769 res = os_snprintf(buf, buflen,
Sunil Ravia04bd252022-05-02 22:54:18 -07001770 "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001771 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1772 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1773 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1774 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1775 ""),
1776 (flags & WLAN_STA_SHORT_PREAMBLE ?
1777 "[SHORT_PREAMBLE]" : ""),
1778 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1779 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1780 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1781 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1782 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1783 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1784 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1785 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1786 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
Roshan Pius3a1667e2018-07-03 15:17:14 -07001787 (flags & WLAN_STA_HT ? "[HT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001788 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
Hai Shalomc3565922019-10-28 11:58:20 -07001789 (flags & WLAN_STA_HE ? "[HE]" : ""),
Sunil Ravia04bd252022-05-02 22:54:18 -07001790 (flags & WLAN_STA_EHT ? "[EHT]" : ""),
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001791 (flags & WLAN_STA_6GHZ ? "[6GHZ]" : ""),
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001792 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001793 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1794 "[WNM_SLEEP_MODE]" : ""));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001795 if (os_snprintf_error(buflen, res))
1796 res = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001797
1798 return res;
1799}
Dmitry Shmidt29333592017-01-09 12:27:11 -08001800
1801
1802static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1803{
1804 struct hostapd_data *hapd = eloop_ctx;
1805 struct sta_info *sta = timeout_ctx;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001806 u16 reason;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001807
1808 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1809 "IEEE 802.1X: Scheduled disconnection of " MACSTR
1810 " after EAP-Failure", MAC2STR(sta->addr));
1811
Roshan Pius3a1667e2018-07-03 15:17:14 -07001812 reason = sta->disconnect_reason_code;
1813 if (!reason)
1814 reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1815 ap_sta_disconnect(hapd, sta, sta->addr, reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001816 if (sta->flags & WLAN_STA_WPS)
1817 hostapd_wps_eap_completed(hapd);
1818}
1819
1820
1821void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
Sunil Ravi640215c2023-06-28 23:08:09 +00001822 struct sta_info *sta,
1823 unsigned timeout)
Dmitry Shmidt29333592017-01-09 12:27:11 -08001824{
1825 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1826 "IEEE 802.1X: Force disconnection of " MACSTR
Sunil Ravi640215c2023-06-28 23:08:09 +00001827 " after EAP-Failure in %u ms", MAC2STR(sta->addr), timeout);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001828
1829 /*
1830 * Add a small sleep to increase likelihood of previously requested
1831 * EAP-Failure TX getting out before this should the driver reorder
1832 * operations.
1833 */
1834 eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
Sunil Ravi640215c2023-06-28 23:08:09 +00001835 eloop_register_timeout(0, timeout * 1000,
1836 ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001837}
1838
1839
1840int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1841 struct sta_info *sta)
1842{
1843 return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1844 hapd, sta);
1845}
Hai Shalomb755a2a2020-04-23 21:49:02 -07001846
1847
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001848#ifdef CONFIG_IEEE80211BE
1849static void ap_sta_remove_link_sta(struct hostapd_data *hapd,
1850 struct sta_info *sta)
1851{
1852 struct hostapd_data *tmp_hapd;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001853
Sunil Ravi7f769292024-07-23 22:21:32 +00001854 for_each_mld_link(tmp_hapd, hapd) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001855 struct sta_info *tmp_sta;
1856
1857 if (hapd == tmp_hapd)
1858 continue;
1859
1860 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
1861 tmp_sta = tmp_sta->next) {
1862 if (tmp_sta == sta ||
1863 !ether_addr_equal(tmp_sta->addr, sta->addr))
1864 continue;
1865
1866 ap_free_sta(tmp_hapd, tmp_sta);
1867 break;
1868 }
1869 }
1870}
1871#endif /* CONFIG_IEEE80211BE */
1872
1873
Hai Shalomb755a2a2020-04-23 21:49:02 -07001874int ap_sta_re_add(struct hostapd_data *hapd, struct sta_info *sta)
1875{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001876 const u8 *mld_link_addr = NULL;
1877 bool mld_link_sta = false;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001878 u16 eml_cap = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001879
Hai Shalomb755a2a2020-04-23 21:49:02 -07001880 /*
1881 * If a station that is already associated to the AP, is trying to
1882 * authenticate again, remove the STA entry, in order to make sure the
1883 * STA PS state gets cleared and configuration gets updated. To handle
1884 * this, station's added_unassoc flag is cleared once the station has
1885 * completed association.
1886 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001887
1888#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001889 if (ap_sta_is_mld(hapd, sta)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001890 u8 mld_link_id = hapd->mld_link_id;
1891
1892 mld_link_sta = sta->mld_assoc_link_id != mld_link_id;
1893 mld_link_addr = sta->mld_info.links[mld_link_id].peer_addr;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001894 eml_cap = sta->mld_info.common_info.eml_capa;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001895
1896 /*
1897 * In case the AP is affiliated with an AP MLD, we need to
1898 * remove the station from all relevant links/APs.
1899 */
1900 ap_sta_remove_link_sta(hapd, sta);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001901 }
1902#endif /* CONFIG_IEEE80211BE */
1903
Hai Shalomb755a2a2020-04-23 21:49:02 -07001904 ap_sta_set_authorized(hapd, sta, 0);
1905 hostapd_drv_sta_remove(hapd, sta->addr);
1906 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH | WLAN_STA_AUTHORIZED);
1907
1908 if (hostapd_sta_add(hapd, sta->addr, 0, 0,
1909 sta->supported_rates,
1910 sta->supported_rates_len,
Sunil Ravia04bd252022-05-02 22:54:18 -07001911 0, NULL, NULL, NULL, 0, NULL, 0, NULL,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001912 sta->flags, 0, 0, 0, 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001913 mld_link_addr, mld_link_sta, eml_cap)) {
Hai Shalomb755a2a2020-04-23 21:49:02 -07001914 hostapd_logger(hapd, sta->addr,
1915 HOSTAPD_MODULE_IEEE80211,
1916 HOSTAPD_LEVEL_NOTICE,
1917 "Could not add STA to kernel driver");
1918 return -1;
1919 }
1920
1921 sta->added_unassoc = 1;
1922 return 0;
1923}
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001924
1925
1926#ifdef CONFIG_IEEE80211BE
1927void ap_sta_free_sta_profile(struct mld_info *info)
1928{
1929 int i;
1930
1931 if (!info)
1932 return;
1933
1934 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
1935 os_free(info->links[i].resp_sta_profile);
1936 info->links[i].resp_sta_profile = NULL;
1937 }
1938}
1939#endif /* CONFIG_IEEE80211BE */