blob: 8aa96d2c70247b443752065bcc7f4329576f7b1d [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);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700422 os_free(sta->t_c_url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800423 wpabuf_free(sta->hs20_deauth_req);
424 os_free(sta->hs20_session_info_url);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700425
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800426#ifdef CONFIG_SAE
427 sae_clear_data(sta->sae);
428 os_free(sta->sae);
429#endif /* CONFIG_SAE */
430
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800431 mbo_ap_sta_free(sta);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800432 os_free(sta->supp_op_classes);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800433
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800434#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);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700439#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 */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800444#endif /* CONFIG_FILS */
445
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700446#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
Hai Shalom021b0b52019-04-10 11:17:58 -0700451#ifdef CONFIG_DPP2
452 dpp_pfs_free(sta->dpp_pfs);
453 sta->dpp_pfs = NULL;
454#endif /* CONFIG_DPP2 */
455
Roshan Pius3a1667e2018-07-03 15:17:14 -0700456 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
Hai Shalom60840252021-02-19 19:02:11 -0800462#ifdef CONFIG_PASN
463 ap_free_sta_pasn(hapd, sta);
464#endif /* CONFIG_PASN */
465
Roshan Pius3a1667e2018-07-03 15:17:14 -0700466 os_free(sta->ifname_wds);
467
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000468#ifdef CONFIG_IEEE80211BE
469 ap_sta_free_sta_profile(&sta->mld_info);
470#endif /* CONFIG_IEEE80211BE */
471
Hai Shalomfdcde762020-04-02 11:19:20 -0700472#ifdef CONFIG_TESTING_OPTIONS
473 os_free(sta->sae_postponed_commit);
Sunil Ravia04bd252022-05-02 22:54:18 -0700474 forced_memzero(sta->last_tk, WPA_TK_MAX_LEN);
Hai Shalomfdcde762020-04-02 11:19:20 -0700475#endif /* CONFIG_TESTING_OPTIONS */
476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477 os_free(sta);
478}
479
480
481void 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
Sunil Ravi99c035e2024-07-12 01:42:03 +0000501#ifdef CONFIG_IEEE80211BE
502void 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700522/**
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 */
530void 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;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700535 int reason;
Sunil Ravi7f769292024-07-23 22:21:32 +0000536 int max_inactivity = hapd->conf->ap_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800538 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
539 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700540 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 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
Sunil Ravi7f769292024-07-23 22:21:32 +0000549 if (sta->max_idle_period)
550 max_inactivity = (sta->max_idle_period * 1024 + 999) / 1000;
551
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700552 if ((sta->flags & WLAN_STA_ASSOC) &&
553 (sta->timeout_next == STA_NULLFUNC ||
554 sta->timeout_next == STA_DISASSOC)) {
555 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700556 /*
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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
563 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800564 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
565 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800566 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800568 /*
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 */
Sunil Ravi7f769292024-07-23 22:21:32 +0000573 next_time = max_inactivity + fuzz;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800574 } 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
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800579 /* Avoid sending client probe on removed client */
580 sta->timeout_next = STA_DISASSOC;
581 goto skip_poll;
Sunil Ravi7f769292024-07-23 22:21:32 +0000582 } else if (inactive_sec < max_inactivity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700583 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800584 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
585 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700586 MAC2STR(sta->addr), inactive_sec);
587 sta->timeout_next = STA_NULLFUNC;
Sunil Ravi7f769292024-07-23 22:21:32 +0000588 next_time = max_inactivity + fuzz - inactive_sec;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700589 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800590 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
591 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700592 "inactive too long: %d sec, max allowed: %d",
593 MAC2STR(sta->addr), inactive_sec,
Sunil Ravi7f769292024-07-23 22:21:32 +0000594 max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800595
596 if (hapd->conf->skip_inactivity_poll)
597 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598 }
599 }
600
601 if ((sta->flags & WLAN_STA_ASSOC) &&
602 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603 !(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));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 /* data nullfunc frame poll did not produce TX errors; assume
608 * station ACKed it */
609 sta->timeout_next = STA_NULLFUNC;
Sunil Ravi7f769292024-07-23 22:21:32 +0000610 next_time = max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700611 }
612
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800613skip_poll:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700615 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
616 "for " MACSTR " (%lu seconds)",
617 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 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)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800625 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800627 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
628 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629 } else if (sta->timeout_next != STA_REMOVE) {
630 int deauth = sta->timeout_next == STA_DEAUTH;
631
Hai Shalom74f70d42019-02-11 14:42:39 -0800632 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
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800639 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
640 "Timeout, sending %s info to STA " MACSTR,
641 deauth ? "deauthentication" : "disassociation",
642 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643
644 if (deauth) {
645 hostapd_drv_sta_deauth(
646 hapd, sta->addr,
647 WLAN_REASON_PREV_AUTH_NOT_VALID);
648 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700649 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654 }
655 }
656
657 switch (sta->timeout_next) {
658 case STA_NULLFUNC:
659 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700660 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
664 hapd, sta);
665 break;
666 case STA_DISASSOC:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700667 case STA_DISASSOC_FROM_CLI:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800668 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669 sta->flags &= ~WLAN_STA_ASSOC;
Hai Shalomfdcde762020-04-02 11:19:20 -0700670 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700671 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);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800676 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700677 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
678 HOSTAPD_LEVEL_INFO, "disassociated due to "
679 "inactivity");
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700680 reason = (sta->timeout_next == STA_DISASSOC) ?
681 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
682 WLAN_REASON_PREV_AUTH_NOT_VALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700683 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700684 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
688 hapd, sta);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700689 mlme_disassociate_indication(hapd, sta, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 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 "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800695 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700696 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
708static 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700712
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800713 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
714 hapd->conf->iface, MAC2STR(sta->addr));
Hai Shalomfdcde762020-04-02 11:19:20 -0700715 if (!(sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC |
716 WLAN_STA_AUTHORIZED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700717 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 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700722 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700723 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700725 hostapd_drv_sta_deauth(hapd, sta->addr,
726 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727 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;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734 ap_free_sta(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700735}
736
737
Dmitry Shmidt54605472013-11-08 11:10:19 -0800738void 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,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800742 ap_handle_session_timer, hapd, sta) == 1) {
Dmitry Shmidt54605472013-11-08 11:10:19 -0800743 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700750void 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
762void 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
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800768static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
769{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700770#ifdef CONFIG_WNM_AP
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800771 struct hostapd_data *hapd = eloop_ctx;
772 struct sta_info *sta = timeout_ctx;
773
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800774 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
775 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800776 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);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700781#endif /* CONFIG_WNM_AP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800782}
783
784
785void 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
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000794static 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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
814{
815 struct sta_info *sta;
Hai Shalom81f62d82019-07-22 12:10:00 -0700816 int i;
Sunil Ravi7f769292024-07-23 22:21:32 +0000817 int max_inactivity = hapd->conf->ap_max_inactivity;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818
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;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800837 if (accounting_sta_get_id(hapd, sta) < 0) {
838 os_free(sta);
839 return NULL;
840 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841
Hai Shalom81f62d82019-07-22 12:10:00 -0700842 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
Sunil Ravi7f769292024-07-23 22:21:32 +0000851 if (sta->max_idle_period)
852 max_inactivity = (sta->max_idle_period * 1024 + 999) / 1000;
853
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800854 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),
Sunil Ravi7f769292024-07-23 22:21:32 +0000858 max_inactivity);
859 eloop_register_timeout(max_inactivity, 0,
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800860 ap_handle_timer, hapd, sta);
861 }
862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 /* initialize STA info data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 ap_sta_remove_in_other_bss(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800870 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
871 dl_list_init(&sta->ip6addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700872
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700873#ifdef CONFIG_TAXONOMY
874 sta_track_claim_taxonomy_info(hapd->iface, addr,
875 &sta->probe_ie_taxonomy);
876#endif /* CONFIG_TAXONOMY */
877
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000878 if (!(hapd->conf->mesh & MESH_ENABLED))
879 eloop_register_timeout(60, 0, ap_sta_assoc_timeout, hapd, sta);
880
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700881 return sta;
882}
883
884
885static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
886{
887 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
888
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800889 if (sta->ipaddr)
890 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
891 ap_sta_ip6addr_del(hapd, sta);
892
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800893 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
894 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700895 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
896 sta->flags & WLAN_STA_ASSOC) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800897 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
898 " from kernel driver",
899 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700900 return -1;
901 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800902 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903 return 0;
904}
905
906
907static 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
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800926 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700930 ap_sta_disconnect(bss, sta2, sta2->addr,
931 WLAN_REASON_PREV_AUTH_NOT_VALID);
932 }
933}
934
935
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936static 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
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800941 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
942 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800943 ap_sta_remove(hapd, sta);
944 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
945}
946
947
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000948static void ap_sta_disconnect_common(struct hostapd_data *hapd,
Sunil Ravi876a49b2025-02-03 19:18:32 +0000949 struct sta_info *sta, unsigned int timeout,
950 bool free_1x)
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);
Sunil Ravi876a49b2025-02-03 19:18:32 +0000964 if (free_1x)
965 ieee802_1x_free_station(hapd, sta);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000966#ifdef CONFIG_IEEE80211BE
967 if (!hapd->conf->mld_ap ||
Sunil Ravi7f769292024-07-23 22:21:32 +0000968 hapd->mld_link_id == sta->mld_assoc_link_id) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000969 wpa_auth_sta_deinit(sta->wpa_sm);
Sunil Ravi7f769292024-07-23 22:21:32 +0000970 clear_wpa_sm_for_each_partner_link(hapd, sta);
971 }
972#else /* CONFIG_IEEE80211BE */
Hai Shalom81f62d82019-07-22 12:10:00 -0700973 wpa_auth_sta_deinit(sta->wpa_sm);
Sunil Ravi2a14cf12023-11-21 00:54:38 +0000974#endif /* CONFIG_IEEE80211BE */
975
Hai Shalom81f62d82019-07-22 12:10:00 -0700976 sta->wpa_sm = NULL;
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000977}
978
979
Sunil Ravi79e6c4f2025-01-04 00:47:06 +0000980static 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
Sunil Ravib0ac25f2024-07-12 01:42:03 +0000992static 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
Sunil Ravi876a49b2025-02-03 19:18:32 +00001009 ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DISASSOC,
1010 true);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001011 ap_sta_disassociate_common(hapd, sta, reason);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001012}
1013
1014
1015static 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
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001020 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
1021 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022 ap_sta_remove(hapd, sta);
1023 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001024}
1025
1026
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001027static void ap_sta_deauthenticate_common(struct hostapd_data *hapd,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001028 struct sta_info *sta, u16 reason)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001029{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001030 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);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001036}
1037
1038
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001039static 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;
Sunil Ravi876a49b2025-02-03 19:18:32 +00001048 ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DEAUTH,
1049 true);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001050 ap_sta_deauthenticate_common(hapd, sta, reason);
1051}
1052
1053
1054static 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 sta->deauth_reason = reason;
Sunil Ravi876a49b2025-02-03 19:18:32 +00001066 ap_sta_disconnect_common(hapd, sta, AP_MAX_INACTIVITY_AFTER_DEAUTH,
1067 false);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001068 ap_sta_deauthenticate_common(hapd, sta, reason);
1069}
1070
1071
1072enum ap_sta_disconnect_op {
1073 AP_STA_DEAUTHENTICATE,
1074 AP_STA_DISASSOCIATE,
1075 AP_STA_DISCONNECT
1076};
1077
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001078static bool ap_sta_ml_disconnect(struct hostapd_data *hapd,
1079 struct sta_info *sta, u16 reason,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001080 enum ap_sta_disconnect_op op)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001081{
1082#ifdef CONFIG_IEEE80211BE
1083 struct hostapd_data *assoc_hapd, *tmp_hapd;
1084 struct sta_info *assoc_sta;
1085 unsigned int i, link_id;
1086 struct hapd_interfaces *interfaces;
1087
1088 if (!hostapd_is_mld_ap(hapd))
1089 return false;
1090
1091 /*
1092 * Get the station on which the association was performed, as it holds
1093 * the information about all the other links.
1094 */
1095 assoc_sta = hostapd_ml_get_assoc_sta(hapd, sta, &assoc_hapd);
1096 if (!assoc_sta)
1097 return false;
1098 interfaces = assoc_hapd->iface->interfaces;
1099
1100 for (link_id = 0; link_id < MAX_NUM_MLD_LINKS; link_id++) {
Sunil Ravi99c035e2024-07-12 01:42:03 +00001101 if (!assoc_sta->mld_info.links[link_id].valid)
1102 continue;
1103
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001104 for (i = 0; i < interfaces->count; i++) {
1105 struct sta_info *tmp_sta;
1106
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001107 tmp_hapd = interfaces->iface[i]->bss[0];
1108
Sunil Ravi99c035e2024-07-12 01:42:03 +00001109 if (!hostapd_is_ml_partner(tmp_hapd, assoc_hapd))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001110 continue;
1111
1112 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
1113 tmp_sta = tmp_sta->next) {
1114 /*
1115 * Handle the station on which the association
1116 * was done only after all other link station
1117 * are removed. Since there is a only a single
1118 * station per hapd with the same association
1119 * link simply break;
1120 */
1121 if (tmp_sta == assoc_sta)
1122 break;
1123
1124 if (tmp_sta->mld_assoc_link_id !=
1125 assoc_sta->mld_assoc_link_id ||
1126 tmp_sta->aid != assoc_sta->aid)
1127 continue;
1128
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001129 if (op == AP_STA_DISASSOCIATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001130 ap_sta_handle_disassociate(tmp_hapd,
1131 tmp_sta,
1132 reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001133 else if (op == AP_STA_DEAUTHENTICATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001134 ap_sta_handle_deauthenticate(tmp_hapd,
1135 tmp_sta,
1136 reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001137 else
1138 ap_sta_handle_disconnect(tmp_hapd,
1139 tmp_sta,
1140 reason);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001141 break;
1142 }
1143 }
1144 }
1145
1146 /* Disconnect the station on which the association was performed. */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001147 if (op == AP_STA_DISASSOCIATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001148 ap_sta_handle_disassociate(assoc_hapd, assoc_sta, reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001149 else if (op == AP_STA_DEAUTHENTICATE)
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001150 ap_sta_handle_deauthenticate(assoc_hapd, assoc_sta, reason);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001151 else
1152 ap_sta_handle_disconnect(assoc_hapd, assoc_sta, reason);
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001153
1154 return true;
1155#else /* CONFIG_IEEE80211BE */
1156 return false;
1157#endif /* CONFIG_IEEE80211BE */
1158}
1159
1160
1161void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
1162 u16 reason)
1163{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001164 if (ap_sta_ml_disconnect(hapd, sta, reason, AP_STA_DISASSOCIATE))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001165 return;
1166
1167 ap_sta_handle_disassociate(hapd, sta, reason);
1168}
1169
1170
1171void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
1172 u16 reason)
1173{
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001174 if (hapd->iface->current_mode &&
1175 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1176 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1177 * disassociate the STA instead. */
1178 ap_sta_disassociate(hapd, sta, reason);
1179 return;
1180 }
1181
1182 if (ap_sta_ml_disconnect(hapd, sta, reason, AP_STA_DEAUTHENTICATE))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001183 return;
1184
1185 ap_sta_handle_deauthenticate(hapd, sta, reason);
1186}
1187
1188
Dmitry Shmidt04949592012-07-19 12:16:46 -07001189#ifdef CONFIG_WPS
1190int ap_sta_wps_cancel(struct hostapd_data *hapd,
1191 struct sta_info *sta, void *ctx)
1192{
1193 if (sta && (sta->flags & WLAN_STA_WPS)) {
1194 ap_sta_deauthenticate(hapd, sta,
1195 WLAN_REASON_PREV_AUTH_NOT_VALID);
1196 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
1197 __func__, MAC2STR(sta->addr));
1198 return 1;
1199 }
1200
1201 return 0;
1202}
1203#endif /* CONFIG_WPS */
1204
1205
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001206static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
1207{
1208 struct hostapd_vlan *vlan;
1209 int vlan_id = MAX_VLAN_ID + 2;
1210
1211retry:
1212 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1213 if (vlan->vlan_id == vlan_id) {
1214 vlan_id++;
1215 goto retry;
1216 }
1217 }
1218 return vlan_id;
1219}
1220
1221
1222int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
1223 struct vlan_description *vlan_desc)
1224{
1225 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
1226 int old_vlan_id, vlan_id = 0, ret = 0;
1227
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001228 /* Check if there is something to do */
1229 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
1230 /* This sta is lacking its own vif */
1231 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
1232 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
1233 /* sta->vlan_id needs to be reset */
1234 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
1235 return 0; /* nothing to change */
1236 }
1237
1238 /* Now the real VLAN changed or the STA just needs its own vif */
1239 if (hapd->conf->ssid.per_sta_vif) {
1240 /* Assign a new vif, always */
1241 /* find a free vlan_id sufficiently big */
1242 vlan_id = ap_sta_get_free_vlan_id(hapd);
1243 /* Get wildcard VLAN */
1244 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1245 if (vlan->vlan_id == VLAN_ID_WILDCARD)
1246 break;
1247 }
1248 if (!vlan) {
1249 hostapd_logger(hapd, sta->addr,
1250 HOSTAPD_MODULE_IEEE80211,
1251 HOSTAPD_LEVEL_DEBUG,
1252 "per_sta_vif missing wildcard");
1253 vlan_id = 0;
1254 ret = -1;
1255 goto done;
1256 }
1257 } else if (vlan_desc && vlan_desc->notempty) {
1258 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1259 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
1260 break;
1261 if (vlan->vlan_id == VLAN_ID_WILDCARD)
1262 wildcard_vlan = vlan;
1263 }
1264 if (vlan) {
1265 vlan_id = vlan->vlan_id;
1266 } else if (wildcard_vlan) {
1267 vlan = wildcard_vlan;
1268 vlan_id = vlan_desc->untagged;
1269 if (vlan_desc->tagged[0]) {
1270 /* Tagged VLAN configuration */
1271 vlan_id = ap_sta_get_free_vlan_id(hapd);
1272 }
1273 } else {
1274 hostapd_logger(hapd, sta->addr,
1275 HOSTAPD_MODULE_IEEE80211,
1276 HOSTAPD_LEVEL_DEBUG,
1277 "missing vlan and wildcard for vlan=%d%s",
1278 vlan_desc->untagged,
1279 vlan_desc->tagged[0] ? "+" : "");
1280 vlan_id = 0;
1281 ret = -1;
1282 goto done;
1283 }
1284 }
1285
1286 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
1287 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
1288 if (vlan == NULL) {
1289 hostapd_logger(hapd, sta->addr,
1290 HOSTAPD_MODULE_IEEE80211,
1291 HOSTAPD_LEVEL_DEBUG,
1292 "could not add dynamic VLAN interface for vlan=%d%s",
1293 vlan_desc ? vlan_desc->untagged : -1,
1294 (vlan_desc && vlan_desc->tagged[0]) ?
1295 "+" : "");
1296 vlan_id = 0;
1297 ret = -1;
1298 goto done;
1299 }
1300
1301 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1302 HOSTAPD_LEVEL_DEBUG,
1303 "added new dynamic VLAN interface '%s'",
1304 vlan->ifname);
1305 } else if (vlan && vlan->dynamic_vlan > 0) {
1306 vlan->dynamic_vlan++;
1307 hostapd_logger(hapd, sta->addr,
1308 HOSTAPD_MODULE_IEEE80211,
1309 HOSTAPD_LEVEL_DEBUG,
1310 "updated existing dynamic VLAN interface '%s'",
1311 vlan->ifname);
1312 }
1313done:
1314 old_vlan_id = sta->vlan_id;
1315 sta->vlan_id = vlan_id;
1316 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
1317
1318 if (vlan_id != old_vlan_id && old_vlan_id)
1319 vlan_remove_dynamic(hapd, old_vlan_id);
1320
1321 return ret;
1322}
1323
1324
Dmitry Shmidt83474442015-04-15 13:47:09 -07001325int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326{
1327#ifndef CONFIG_NO_VLAN
1328 const char *iface;
1329 struct hostapd_vlan *vlan = NULL;
1330 int ret;
Dmitry Shmidt83474442015-04-15 13:47:09 -07001331 int old_vlanid = sta->vlan_id_bound;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001332 int mld_link_id = -1;
1333
1334#ifdef CONFIG_IEEE80211BE
1335 if (hapd->conf->mld_ap)
1336 mld_link_id = hapd->mld_link_id;
1337#endif /* CONFIG_IEEE80211BE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338
Hai Shalomfdcde762020-04-02 11:19:20 -07001339 if ((sta->flags & WLAN_STA_WDS) && sta->vlan_id == 0) {
1340 wpa_printf(MSG_DEBUG,
1341 "Do not override WDS VLAN assignment for STA "
1342 MACSTR, MAC2STR(sta->addr));
1343 return 0;
1344 }
1345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001346 iface = hapd->conf->iface;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001347 if (hapd->conf->ssid.vlan[0])
1348 iface = hapd->conf->ssid.vlan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001350 if (sta->vlan_id > 0) {
1351 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001352 if (vlan->vlan_id == sta->vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001353 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001354 }
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001355 if (vlan)
1356 iface = vlan->ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001357 }
1358
Dmitry Shmidt83474442015-04-15 13:47:09 -07001359 /*
1360 * Do not increment ref counters if the VLAN ID remains same, but do
1361 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1362 * have been called before.
1363 */
1364 if (sta->vlan_id == old_vlanid)
1365 goto skip_counting;
1366
Hai Shalomfdcde762020-04-02 11:19:20 -07001367 if (sta->vlan_id > 0 && !vlan &&
1368 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1370 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1371 "binding station to (vlan_id=%d)",
1372 sta->vlan_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001373 ret = -1;
1374 goto done;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001375 } else if (vlan && vlan->dynamic_vlan > 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001376 vlan->dynamic_vlan++;
1377 hostapd_logger(hapd, sta->addr,
1378 HOSTAPD_MODULE_IEEE80211,
1379 HOSTAPD_LEVEL_DEBUG,
1380 "updated existing dynamic VLAN interface '%s'",
1381 iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 }
1383
Dmitry Shmidt83474442015-04-15 13:47:09 -07001384 /* ref counters have been increased, so mark the station */
1385 sta->vlan_id_bound = sta->vlan_id;
1386
1387skip_counting:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1389 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1390 "'%s'", iface);
1391
1392 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1393 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1394
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001395 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id,
1396 mld_link_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001397 if (ret < 0) {
1398 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1399 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1400 "entry to vlan_id=%d", sta->vlan_id);
1401 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001402
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001403 /* During 1x reauth, if the vlan id changes, then remove the old id. */
Dmitry Shmidt83474442015-04-15 13:47:09 -07001404 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001405 vlan_remove_dynamic(hapd, old_vlanid);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001406done:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001407
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001408 return ret;
1409#else /* CONFIG_NO_VLAN */
1410 return 0;
1411#endif /* CONFIG_NO_VLAN */
1412}
1413
1414
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001415int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1416{
1417 u32 tu;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001418 struct os_reltime now, passed;
1419 os_get_reltime(&now);
1420 os_reltime_sub(&now, &sta->sa_query_start, &passed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1422 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1423 hostapd_logger(hapd, sta->addr,
1424 HOSTAPD_MODULE_IEEE80211,
1425 HOSTAPD_LEVEL_DEBUG,
1426 "association SA Query timed out");
1427 sta->sa_query_timed_out = 1;
1428 os_free(sta->sa_query_trans_id);
1429 sta->sa_query_trans_id = NULL;
1430 sta->sa_query_count = 0;
1431 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1432 return 1;
1433 }
1434
1435 return 0;
1436}
1437
1438
1439static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1440{
1441 struct hostapd_data *hapd = eloop_ctx;
1442 struct sta_info *sta = timeout_ctx;
1443 unsigned int timeout, sec, usec;
1444 u8 *trans_id, *nbuf;
1445
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001446 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1447 " (count=%d)",
1448 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1449
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001450 if (sta->sa_query_count > 0 &&
1451 ap_check_sa_query_timeout(hapd, sta))
1452 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07001453 if (sta->sa_query_count >= 1000)
1454 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001456 nbuf = os_realloc_array(sta->sa_query_trans_id,
1457 sta->sa_query_count + 1,
1458 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001459 if (nbuf == NULL)
1460 return;
1461 if (sta->sa_query_count == 0) {
1462 /* Starting a new SA Query procedure */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001463 os_get_reltime(&sta->sa_query_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 }
1465 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1466 sta->sa_query_trans_id = nbuf;
1467 sta->sa_query_count++;
1468
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001469 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1470 /*
1471 * We don't really care which ID is used here, so simply
1472 * hardcode this if the mostly theoretical os_get_random()
1473 * failure happens.
1474 */
1475 trans_id[0] = 0x12;
1476 trans_id[1] = 0x34;
1477 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001478
1479 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1480 sec = ((timeout / 1000) * 1024) / 1000;
1481 usec = (timeout % 1000) * 1024;
1482 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1483
1484 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1485 HOSTAPD_LEVEL_DEBUG,
1486 "association SA Query attempt %d", sta->sa_query_count);
1487
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001488 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001489}
1490
1491
1492void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1493{
1494 ap_sa_query_timer(hapd, sta);
1495}
1496
1497
1498void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1499{
1500 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1501 os_free(sta->sa_query_trans_id);
1502 sta->sa_query_trans_id = NULL;
1503 sta->sa_query_count = 0;
1504}
1505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506
Hai Shalom74f70d42019-02-11 14:42:39 -08001507const char * ap_sta_wpa_get_keyid(struct hostapd_data *hapd,
1508 struct sta_info *sta)
1509{
1510 struct hostapd_wpa_psk *psk;
1511 struct hostapd_ssid *ssid;
1512 const u8 *pmk;
1513 int pmk_len;
1514
1515 ssid = &hapd->conf->ssid;
1516
1517 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1518 if (!pmk || pmk_len != PMK_LEN)
1519 return NULL;
1520
1521 for (psk = ssid->wpa_psk; psk; psk = psk->next)
1522 if (os_memcmp(pmk, psk->psk, PMK_LEN) == 0)
1523 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08001524 if (!psk || !psk->keyid[0])
1525 return NULL;
1526
1527 return psk->keyid;
1528}
1529
1530
Sunil Ravi77d572f2023-01-17 23:58:31 +00001531const u8 * ap_sta_wpa_get_dpp_pkhash(struct hostapd_data *hapd,
1532 struct sta_info *sta)
1533{
1534 return wpa_auth_get_dpp_pkhash(sta->wpa_sm);
1535}
1536
1537
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001538bool ap_sta_set_authorized_flag(struct hostapd_data *hapd, struct sta_info *sta,
1539 int authorized)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001540{
1541 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001542 return false;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001543
Sunil Ravi640215c2023-06-28 23:08:09 +00001544 if (authorized) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001545 int mld_assoc_link_id = -1;
1546
1547#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001548 if (ap_sta_is_mld(hapd, sta)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001549 if (sta->mld_assoc_link_id == hapd->mld_link_id)
1550 mld_assoc_link_id = sta->mld_assoc_link_id;
1551 else
1552 mld_assoc_link_id = -2;
1553 }
1554#endif /* CONFIG_IEEE80211BE */
1555 if (mld_assoc_link_id != -2)
1556 hostapd_prune_associations(hapd, sta->addr,
1557 mld_assoc_link_id);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001558 sta->flags |= WLAN_STA_AUTHORIZED;
Sunil Ravi640215c2023-06-28 23:08:09 +00001559 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001560 sta->flags &= ~WLAN_STA_AUTHORIZED;
Sunil Ravi640215c2023-06-28 23:08:09 +00001561 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001562
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001563 return true;
1564}
1565
1566
1567void ap_sta_set_authorized_event(struct hostapd_data *hapd,
1568 struct sta_info *sta, int authorized)
1569{
1570 const u8 *dev_addr = NULL;
1571 char buf[100];
1572#ifdef CONFIG_P2P
1573 u8 addr[ETH_ALEN];
1574 u8 ip_addr_buf[4];
1575#endif /* CONFIG_P2P */
Sunil Ravi99c035e2024-07-12 01:42:03 +00001576 const u8 *ip_ptr = NULL;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001577
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001578#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001579 if (hapd->p2p_group == NULL) {
1580 if (sta->p2p_ie != NULL &&
1581 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1582 dev_addr = addr;
1583 } else
1584 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001585
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001586 if (dev_addr)
1587 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1588 MAC2STR(sta->addr), MAC2STR(dev_addr));
1589 else
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001590#endif /* CONFIG_P2P */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001591 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1592
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001593 if (authorized) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001594 const u8 *dpp_pkhash;
Hai Shalom74f70d42019-02-11 14:42:39 -08001595 const char *keyid;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001596 char dpp_pkhash_buf[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001597 char keyid_buf[100];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001598 char ip_addr[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001599
Sunil Ravi77d572f2023-01-17 23:58:31 +00001600 dpp_pkhash_buf[0] = '\0';
Hai Shalom74f70d42019-02-11 14:42:39 -08001601 keyid_buf[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001602 ip_addr[0] = '\0';
1603#ifdef CONFIG_P2P
1604 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1605 os_snprintf(ip_addr, sizeof(ip_addr),
1606 " ip_addr=%u.%u.%u.%u",
1607 ip_addr_buf[0], ip_addr_buf[1],
1608 ip_addr_buf[2], ip_addr_buf[3]);
Sunil Ravid8128a22023-11-06 23:53:58 +00001609 ip_ptr = ip_addr_buf;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001610 }
1611#endif /* CONFIG_P2P */
1612
Hai Shalom74f70d42019-02-11 14:42:39 -08001613 keyid = ap_sta_wpa_get_keyid(hapd, sta);
1614 if (keyid) {
1615 os_snprintf(keyid_buf, sizeof(keyid_buf),
1616 " keyid=%s", keyid);
1617 }
1618
Sunil Ravi77d572f2023-01-17 23:58:31 +00001619 dpp_pkhash = ap_sta_wpa_get_dpp_pkhash(hapd, sta);
1620 if (dpp_pkhash) {
1621 const char *prefix = " dpp_pkhash=";
1622 size_t plen = os_strlen(prefix);
1623
1624 os_strlcpy(dpp_pkhash_buf, prefix,
1625 sizeof(dpp_pkhash_buf));
1626 wpa_snprintf_hex(&dpp_pkhash_buf[plen],
1627 sizeof(dpp_pkhash_buf) - plen,
1628 dpp_pkhash, SHA256_MAC_LEN);
1629 }
1630
1631 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s%s",
1632 buf, ip_addr, keyid_buf, dpp_pkhash_buf);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001633
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001634 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001635 hapd->msg_ctx_parent != hapd->msg_ctx)
1636 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001637 AP_STA_CONNECTED "%s%s%s%s",
1638 buf, ip_addr, keyid_buf,
1639 dpp_pkhash_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001640 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001641 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1642
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001643 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001644 hapd->msg_ctx_parent != hapd->msg_ctx)
1645 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1646 AP_STA_DISCONNECTED "%s", buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001647 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001648
Sunil Ravid8128a22023-11-06 23:53:58 +00001649 if (hapd->sta_authorized_cb)
1650 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1651 sta->addr, authorized, dev_addr,
1652 ip_ptr);
1653
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001654#ifdef CONFIG_FST
1655 if (hapd->iface->fst) {
1656 if (authorized)
1657 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1658 else
1659 fst_notify_peer_disconnected(hapd->iface->fst,
1660 sta->addr);
1661 }
1662#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001663}
1664
1665
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001666bool ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001667 int authorized)
1668{
1669 if (!ap_sta_set_authorized_flag(hapd, sta, authorized))
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001670 return false;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001671 ap_sta_set_authorized_event(hapd, sta, authorized);
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001672 return true;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001673}
1674
1675
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001676void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1677 const u8 *addr, u16 reason)
1678{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001679 if (sta)
1680 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1681 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1682 reason);
1683 else if (addr)
1684 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1685 hapd->conf->iface, __func__, MAC2STR(addr),
1686 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687
1688 if (sta == NULL && addr)
1689 sta = ap_get_sta(hapd, addr);
1690
1691 if (addr)
1692 hostapd_drv_sta_deauth(hapd, addr, reason);
1693
1694 if (sta == NULL)
1695 return;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001696
Dmitry Shmidt29333592017-01-09 12:27:11 -08001697 if (hapd->iface->current_mode &&
1698 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1699 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1700 * disassociate the STA instead. */
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001701 ap_sta_disassociate_common(hapd, sta, reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001702 return;
1703 }
1704
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001705 if (ap_sta_ml_disconnect(hapd, sta, reason, AP_STA_DISCONNECT))
1706 return;
1707
1708 ap_sta_handle_disconnect(hapd, sta, reason);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001709}
1710
1711
1712void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1713{
1714 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1715 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1716 return;
1717 }
1718 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1719 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1720 ap_sta_deauth_cb_timeout(hapd, sta);
1721}
1722
1723
1724void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1725{
1726 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1727 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1728 return;
1729 }
1730 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1731 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1732 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001733}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001734
1735
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001736void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1737 struct sta_info *sta)
1738{
1739 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1740 wpa_printf(MSG_DEBUG,
1741 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1742 MACSTR,
1743 hapd->conf->iface, MAC2STR(sta->addr));
1744 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1745 wpa_printf(MSG_DEBUG,
1746 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1747 MACSTR,
1748 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt29333592017-01-09 12:27:11 -08001749 if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1750 {
1751 wpa_printf(MSG_DEBUG,
1752 "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1753 MACSTR,
1754 hapd->conf->iface, MAC2STR(sta->addr));
1755 if (sta->flags & WLAN_STA_WPS)
1756 hostapd_wps_eap_completed(hapd);
1757 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001758}
1759
1760
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001761void ap_sta_clear_assoc_timeout(struct hostapd_data *hapd,
1762 struct sta_info *sta)
1763{
1764 eloop_cancel_timeout(ap_sta_assoc_timeout, hapd, sta);
1765}
1766
1767
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001768int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1769{
1770 int res;
1771
1772 buf[0] = '\0';
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001773 res = os_snprintf(buf, buflen,
Sunil Ravi876a49b2025-02-03 19:18:32 +00001774 "%s%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 -08001775 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1776 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1777 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1778 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1779 ""),
1780 (flags & WLAN_STA_SHORT_PREAMBLE ?
1781 "[SHORT_PREAMBLE]" : ""),
1782 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1783 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1784 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1785 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1786 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1787 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1788 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1789 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1790 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
Roshan Pius3a1667e2018-07-03 15:17:14 -07001791 (flags & WLAN_STA_HT ? "[HT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001792 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
Hai Shalomc3565922019-10-28 11:58:20 -07001793 (flags & WLAN_STA_HE ? "[HE]" : ""),
Sunil Ravia04bd252022-05-02 22:54:18 -07001794 (flags & WLAN_STA_EHT ? "[EHT]" : ""),
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001795 (flags & WLAN_STA_6GHZ ? "[6GHZ]" : ""),
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001796 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
Sunil Ravi876a49b2025-02-03 19:18:32 +00001797 (flags & WLAN_STA_SPP_AMSDU ? "[SPP-A-MSDU]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001798 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1799 "[WNM_SLEEP_MODE]" : ""));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001800 if (os_snprintf_error(buflen, res))
1801 res = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001802
1803 return res;
1804}
Dmitry Shmidt29333592017-01-09 12:27:11 -08001805
1806
1807static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1808{
1809 struct hostapd_data *hapd = eloop_ctx;
1810 struct sta_info *sta = timeout_ctx;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001811 u16 reason;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001812
1813 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1814 "IEEE 802.1X: Scheduled disconnection of " MACSTR
1815 " after EAP-Failure", MAC2STR(sta->addr));
1816
Roshan Pius3a1667e2018-07-03 15:17:14 -07001817 reason = sta->disconnect_reason_code;
1818 if (!reason)
1819 reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1820 ap_sta_disconnect(hapd, sta, sta->addr, reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001821 if (sta->flags & WLAN_STA_WPS)
1822 hostapd_wps_eap_completed(hapd);
1823}
1824
1825
1826void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
Sunil Ravi640215c2023-06-28 23:08:09 +00001827 struct sta_info *sta,
1828 unsigned timeout)
Dmitry Shmidt29333592017-01-09 12:27:11 -08001829{
1830 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1831 "IEEE 802.1X: Force disconnection of " MACSTR
Sunil Ravi640215c2023-06-28 23:08:09 +00001832 " after EAP-Failure in %u ms", MAC2STR(sta->addr), timeout);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001833
1834 /*
1835 * Add a small sleep to increase likelihood of previously requested
1836 * EAP-Failure TX getting out before this should the driver reorder
1837 * operations.
1838 */
1839 eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
Sunil Ravi640215c2023-06-28 23:08:09 +00001840 eloop_register_timeout(0, timeout * 1000,
1841 ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001842}
1843
1844
1845int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1846 struct sta_info *sta)
1847{
1848 return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1849 hapd, sta);
1850}
Hai Shalomb755a2a2020-04-23 21:49:02 -07001851
1852
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001853#ifdef CONFIG_IEEE80211BE
1854static void ap_sta_remove_link_sta(struct hostapd_data *hapd,
1855 struct sta_info *sta)
1856{
1857 struct hostapd_data *tmp_hapd;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001858
Sunil Ravi7f769292024-07-23 22:21:32 +00001859 for_each_mld_link(tmp_hapd, hapd) {
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001860 struct sta_info *tmp_sta;
1861
1862 if (hapd == tmp_hapd)
1863 continue;
1864
1865 for (tmp_sta = tmp_hapd->sta_list; tmp_sta;
1866 tmp_sta = tmp_sta->next) {
1867 if (tmp_sta == sta ||
1868 !ether_addr_equal(tmp_sta->addr, sta->addr))
1869 continue;
1870
1871 ap_free_sta(tmp_hapd, tmp_sta);
1872 break;
1873 }
1874 }
1875}
1876#endif /* CONFIG_IEEE80211BE */
1877
1878
Hai Shalomb755a2a2020-04-23 21:49:02 -07001879int ap_sta_re_add(struct hostapd_data *hapd, struct sta_info *sta)
1880{
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001881 const u8 *mld_link_addr = NULL;
1882 bool mld_link_sta = false;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001883 u16 eml_cap = 0;
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001884
Hai Shalomb755a2a2020-04-23 21:49:02 -07001885 /*
1886 * If a station that is already associated to the AP, is trying to
1887 * authenticate again, remove the STA entry, in order to make sure the
1888 * STA PS state gets cleared and configuration gets updated. To handle
1889 * this, station's added_unassoc flag is cleared once the station has
1890 * completed association.
1891 */
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001892
1893#ifdef CONFIG_IEEE80211BE
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001894 if (ap_sta_is_mld(hapd, sta)) {
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001895 u8 mld_link_id = hapd->mld_link_id;
1896
1897 mld_link_sta = sta->mld_assoc_link_id != mld_link_id;
1898 mld_link_addr = sta->mld_info.links[mld_link_id].peer_addr;
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001899 eml_cap = sta->mld_info.common_info.eml_capa;
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001900
1901 /*
1902 * In case the AP is affiliated with an AP MLD, we need to
1903 * remove the station from all relevant links/APs.
1904 */
1905 ap_sta_remove_link_sta(hapd, sta);
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001906 }
1907#endif /* CONFIG_IEEE80211BE */
1908
Hai Shalomb755a2a2020-04-23 21:49:02 -07001909 ap_sta_set_authorized(hapd, sta, 0);
1910 hostapd_drv_sta_remove(hapd, sta->addr);
1911 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH | WLAN_STA_AUTHORIZED);
1912
1913 if (hostapd_sta_add(hapd, sta->addr, 0, 0,
1914 sta->supported_rates,
1915 sta->supported_rates_len,
Sunil Ravia04bd252022-05-02 22:54:18 -07001916 0, NULL, NULL, NULL, 0, NULL, 0, NULL,
Sunil Ravi2a14cf12023-11-21 00:54:38 +00001917 sta->flags, 0, 0, 0, 0,
Sunil Ravi79e6c4f2025-01-04 00:47:06 +00001918 mld_link_addr, mld_link_sta, eml_cap)) {
Hai Shalomb755a2a2020-04-23 21:49:02 -07001919 hostapd_logger(hapd, sta->addr,
1920 HOSTAPD_MODULE_IEEE80211,
1921 HOSTAPD_LEVEL_NOTICE,
1922 "Could not add STA to kernel driver");
1923 return -1;
1924 }
1925
1926 sta->added_unassoc = 1;
1927 return 0;
1928}
Sunil Ravib0ac25f2024-07-12 01:42:03 +00001929
1930
1931#ifdef CONFIG_IEEE80211BE
1932void ap_sta_free_sta_profile(struct mld_info *info)
1933{
1934 int i;
1935
1936 if (!info)
1937 return;
1938
1939 for (i = 0; i < MAX_NUM_MLD_LINKS; i++) {
1940 os_free(info->links[i].resp_sta_profile);
1941 info->links[i].resp_sta_profile = NULL;
1942 }
1943}
1944#endif /* CONFIG_IEEE80211BE */