blob: 2fb6edf09ff97522a0c008973e3f78cf1b089419 [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
95 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
96 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 &&
143 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
144 s = s->hnext;
145 if (s->hnext != NULL)
146 s->hnext = s->hnext->hnext;
147 else
148 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
149 " from hash table", MAC2STR(sta->addr));
150}
151
152
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
183 bin_clear_free(sta->pasn, sizeof(*sta->pasn));
184 sta->pasn = NULL;
185 }
186}
187
188#endif /* CONFIG_PASN */
189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
191{
192 int set_beacon = 0;
193
194 accounting_sta_stop(hapd, sta);
195
196 /* just in case */
197 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700198 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700199
Sunil Ravi640215c2023-06-28 23:08:09 +0000200 if ((sta->flags & WLAN_STA_WDS) ||
201 (sta->flags & WLAN_STA_MULTI_AP &&
202 !(hapd->conf->multi_ap & FRONTHAUL_BSS) &&
203 !(sta->flags & WLAN_STA_WPS)))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700204 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800206 if (sta->ipaddr)
207 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
208 ap_sta_ip6addr_del(hapd, sta);
209
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800210 if (!hapd->iface->driver_ap_teardown &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800211 !(sta->flags & WLAN_STA_PREAUTH)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700212 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800213 sta->added_unassoc = 0;
214 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215
216 ap_sta_hash_del(hapd, sta);
217 ap_sta_list_del(hapd, sta);
218
219 if (sta->aid > 0)
220 hapd->sta_aid[(sta->aid - 1) / 32] &=
221 ~BIT((sta->aid - 1) % 32);
222
223 hapd->num_sta--;
224 if (sta->nonerp_set) {
225 sta->nonerp_set = 0;
226 hapd->iface->num_sta_non_erp--;
227 if (hapd->iface->num_sta_non_erp == 0)
228 set_beacon++;
229 }
230
231 if (sta->no_short_slot_time_set) {
232 sta->no_short_slot_time_set = 0;
233 hapd->iface->num_sta_no_short_slot_time--;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700234 if (hapd->iface->current_mode &&
235 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700236 && hapd->iface->num_sta_no_short_slot_time == 0)
237 set_beacon++;
238 }
239
240 if (sta->no_short_preamble_set) {
241 sta->no_short_preamble_set = 0;
242 hapd->iface->num_sta_no_short_preamble--;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700243 if (hapd->iface->current_mode &&
244 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 && hapd->iface->num_sta_no_short_preamble == 0)
246 set_beacon++;
247 }
248
249 if (sta->no_ht_gf_set) {
250 sta->no_ht_gf_set = 0;
251 hapd->iface->num_sta_ht_no_gf--;
252 }
253
254 if (sta->no_ht_set) {
255 sta->no_ht_set = 0;
256 hapd->iface->num_sta_no_ht--;
257 }
258
259 if (sta->ht_20mhz_set) {
260 sta->ht_20mhz_set = 0;
261 hapd->iface->num_sta_ht_20mhz--;
262 }
263
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700264#ifdef CONFIG_TAXONOMY
265 wpabuf_free(sta->probe_ie_taxonomy);
266 sta->probe_ie_taxonomy = NULL;
267 wpabuf_free(sta->assoc_ie_taxonomy);
268 sta->assoc_ie_taxonomy = NULL;
269#endif /* CONFIG_TAXONOMY */
270
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700271 ht40_intolerant_remove(hapd->iface, sta);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700273#ifdef CONFIG_P2P
274 if (sta->no_p2p_set) {
275 sta->no_p2p_set = 0;
276 hapd->num_sta_no_p2p--;
277 if (hapd->num_sta_no_p2p == 0)
278 hostapd_p2p_non_p2p_sta_disconnected(hapd);
279 }
280#endif /* CONFIG_P2P */
281
Hai Shalomfdcde762020-04-02 11:19:20 -0700282#ifdef NEED_AP_MLME
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283 if (hostapd_ht_operation_update(hapd->iface) > 0)
284 set_beacon++;
Hai Shalomfdcde762020-04-02 11:19:20 -0700285#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800287#ifdef CONFIG_MESH
288 if (hapd->mesh_sta_free_cb)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800289 hapd->mesh_sta_free_cb(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800290#endif /* CONFIG_MESH */
291
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700292 if (set_beacon)
293 ieee802_11_set_beacons(hapd->iface);
294
Dmitry Shmidt04949592012-07-19 12:16:46 -0700295 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
296 __func__, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
298 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800299 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800300 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800301 sae_clear_retransmit_timer(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800303 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 wpa_auth_sta_deinit(sta->wpa_sm);
305 rsn_preauth_free_station(hapd, sta);
306#ifndef CONFIG_NO_RADIUS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700307 if (hapd->radius)
308 radius_client_flush_auth(hapd->radius, sta->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700309#endif /* CONFIG_NO_RADIUS */
310
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800311#ifndef CONFIG_NO_VLAN
312 /*
313 * sta->wpa_sm->group needs to be released before so that
314 * vlan_remove_dynamic() can check that no stations are left on the
315 * AP_VLAN netdev.
316 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800317 if (sta->vlan_id)
318 vlan_remove_dynamic(hapd, sta->vlan_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800319 if (sta->vlan_id_bound) {
320 /*
321 * Need to remove the STA entry before potentially removing the
322 * VLAN.
323 */
324 if (hapd->iface->driver_ap_teardown &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800325 !(sta->flags & WLAN_STA_PREAUTH)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800326 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800327 sta->added_unassoc = 0;
328 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800329 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
330 }
331#endif /* CONFIG_NO_VLAN */
332
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 os_free(sta->challenge);
334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335 os_free(sta->sa_query_trans_id);
336 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337
338#ifdef CONFIG_P2P
339 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
340#endif /* CONFIG_P2P */
341
Dmitry Shmidt04949592012-07-19 12:16:46 -0700342#ifdef CONFIG_INTERWORKING
343 if (sta->gas_dialog) {
344 int i;
345 for (i = 0; i < GAS_DIALOG_MAX; i++)
346 gas_serv_dialog_clear(&sta->gas_dialog[i]);
347 os_free(sta->gas_dialog);
348 }
349#endif /* CONFIG_INTERWORKING */
350
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 wpabuf_free(sta->wps_ie);
352 wpabuf_free(sta->p2p_ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800353 wpabuf_free(sta->hs20_ie);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700354 wpabuf_free(sta->roaming_consortium);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800355#ifdef CONFIG_FST
356 wpabuf_free(sta->mb_ies);
357#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358
359 os_free(sta->ht_capabilities);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800360 os_free(sta->vht_capabilities);
Hai Shalom74f70d42019-02-11 14:42:39 -0800361 os_free(sta->vht_operation);
Hai Shalom81f62d82019-07-22 12:10:00 -0700362 os_free(sta->he_capab);
Hai Shalom4fbc08f2020-05-18 12:37:00 -0700363 os_free(sta->he_6ghz_capab);
Sunil Ravia04bd252022-05-02 22:54:18 -0700364 os_free(sta->eht_capab);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800365 hostapd_free_psk_list(sta->psk);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700366 os_free(sta->identity);
367 os_free(sta->radius_cui);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800368 os_free(sta->remediation_url);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700369 os_free(sta->t_c_url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800370 wpabuf_free(sta->hs20_deauth_req);
371 os_free(sta->hs20_session_info_url);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800373#ifdef CONFIG_SAE
374 sae_clear_data(sta->sae);
375 os_free(sta->sae);
376#endif /* CONFIG_SAE */
377
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800378 mbo_ap_sta_free(sta);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800379 os_free(sta->supp_op_classes);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800380
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800381#ifdef CONFIG_FILS
382 os_free(sta->fils_pending_assoc_req);
383 wpabuf_free(sta->fils_hlp_resp);
384 wpabuf_free(sta->hlp_dhcp_discover);
385 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700386#ifdef CONFIG_FILS_SK_PFS
387 crypto_ecdh_deinit(sta->fils_ecdh);
388 wpabuf_clear_free(sta->fils_dh_ss);
389 wpabuf_free(sta->fils_g_sta);
390#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800391#endif /* CONFIG_FILS */
392
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700393#ifdef CONFIG_OWE
394 bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
395 crypto_ecdh_deinit(sta->owe_ecdh);
396#endif /* CONFIG_OWE */
397
Hai Shalom021b0b52019-04-10 11:17:58 -0700398#ifdef CONFIG_DPP2
399 dpp_pfs_free(sta->dpp_pfs);
400 sta->dpp_pfs = NULL;
401#endif /* CONFIG_DPP2 */
402
Roshan Pius3a1667e2018-07-03 15:17:14 -0700403 os_free(sta->ext_capability);
404
405#ifdef CONFIG_WNM_AP
406 eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
407#endif /* CONFIG_WNM_AP */
408
Hai Shalom60840252021-02-19 19:02:11 -0800409#ifdef CONFIG_PASN
410 ap_free_sta_pasn(hapd, sta);
411#endif /* CONFIG_PASN */
412
Roshan Pius3a1667e2018-07-03 15:17:14 -0700413 os_free(sta->ifname_wds);
414
Hai Shalomfdcde762020-04-02 11:19:20 -0700415#ifdef CONFIG_TESTING_OPTIONS
416 os_free(sta->sae_postponed_commit);
Sunil Ravia04bd252022-05-02 22:54:18 -0700417 forced_memzero(sta->last_tk, WPA_TK_MAX_LEN);
Hai Shalomfdcde762020-04-02 11:19:20 -0700418#endif /* CONFIG_TESTING_OPTIONS */
419
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700420 os_free(sta);
421}
422
423
424void hostapd_free_stas(struct hostapd_data *hapd)
425{
426 struct sta_info *sta, *prev;
427
428 sta = hapd->sta_list;
429
430 while (sta) {
431 prev = sta;
432 if (sta->flags & WLAN_STA_AUTH) {
433 mlme_deauthenticate_indication(
434 hapd, sta, WLAN_REASON_UNSPECIFIED);
435 }
436 sta = sta->next;
437 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
438 MAC2STR(prev->addr));
439 ap_free_sta(hapd, prev);
440 }
441}
442
443
444/**
445 * ap_handle_timer - Per STA timer handler
446 * @eloop_ctx: struct hostapd_data *
447 * @timeout_ctx: struct sta_info *
448 *
449 * This function is called to check station activity and to remove inactive
450 * stations.
451 */
452void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
453{
454 struct hostapd_data *hapd = eloop_ctx;
455 struct sta_info *sta = timeout_ctx;
456 unsigned long next_time = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700457 int reason;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700458
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800459 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
460 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700461 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700462 if (sta->timeout_next == STA_REMOVE) {
463 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
464 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
465 "local deauth request");
466 ap_free_sta(hapd, sta);
467 return;
468 }
469
470 if ((sta->flags & WLAN_STA_ASSOC) &&
471 (sta->timeout_next == STA_NULLFUNC ||
472 sta->timeout_next == STA_DISASSOC)) {
473 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700474 /*
475 * Add random value to timeout so that we don't end up bouncing
476 * all stations at the same time if we have lots of associated
477 * stations that are idle (but keep re-associating).
478 */
479 int fuzz = os_random() % 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700480 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
481 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800482 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
483 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800484 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800486 /*
487 * The driver may not support this functionality.
488 * Anyway, try again after the next inactivity timeout,
489 * but do not disconnect the station now.
490 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 next_time = hapd->conf->ap_max_inactivity + fuzz;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800492 } else if (inactive_sec == -ENOENT) {
493 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
494 "Station " MACSTR " has lost its driver entry",
495 MAC2STR(sta->addr));
496
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800497 /* Avoid sending client probe on removed client */
498 sta->timeout_next = STA_DISASSOC;
499 goto skip_poll;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800500 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700501 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800502 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
503 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 MAC2STR(sta->addr), inactive_sec);
505 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700506 next_time = hapd->conf->ap_max_inactivity + fuzz -
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 inactive_sec;
508 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800509 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
510 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700511 "inactive too long: %d sec, max allowed: %d",
512 MAC2STR(sta->addr), inactive_sec,
513 hapd->conf->ap_max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800514
515 if (hapd->conf->skip_inactivity_poll)
516 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700517 }
518 }
519
520 if ((sta->flags & WLAN_STA_ASSOC) &&
521 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800522 !(sta->flags & WLAN_STA_PENDING_POLL) &&
523 !hapd->conf->skip_inactivity_poll) {
524 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
525 " has ACKed data poll", MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700526 /* data nullfunc frame poll did not produce TX errors; assume
527 * station ACKed it */
528 sta->timeout_next = STA_NULLFUNC;
529 next_time = hapd->conf->ap_max_inactivity;
530 }
531
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800532skip_poll:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700533 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700534 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
535 "for " MACSTR " (%lu seconds)",
536 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700537 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
538 sta);
539 return;
540 }
541
542 if (sta->timeout_next == STA_NULLFUNC &&
543 (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800544 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800546 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
547 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700548 } else if (sta->timeout_next != STA_REMOVE) {
549 int deauth = sta->timeout_next == STA_DEAUTH;
550
Hai Shalom74f70d42019-02-11 14:42:39 -0800551 if (!deauth && !(sta->flags & WLAN_STA_ASSOC)) {
552 /* Cannot disassociate not-associated STA, so move
553 * directly to deauthentication. */
554 sta->timeout_next = STA_DEAUTH;
555 deauth = 1;
556 }
557
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800558 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
559 "Timeout, sending %s info to STA " MACSTR,
560 deauth ? "deauthentication" : "disassociation",
561 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562
563 if (deauth) {
564 hostapd_drv_sta_deauth(
565 hapd, sta->addr,
566 WLAN_REASON_PREV_AUTH_NOT_VALID);
567 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700568 reason = (sta->timeout_next == STA_DISASSOC) ?
569 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
570 WLAN_REASON_PREV_AUTH_NOT_VALID;
571
572 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700573 }
574 }
575
576 switch (sta->timeout_next) {
577 case STA_NULLFUNC:
578 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700579 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
580 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
581 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700582 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
583 hapd, sta);
584 break;
585 case STA_DISASSOC:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700586 case STA_DISASSOC_FROM_CLI:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800587 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700588 sta->flags &= ~WLAN_STA_ASSOC;
Hai Shalomfdcde762020-04-02 11:19:20 -0700589 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
591 if (!sta->acct_terminate_cause)
592 sta->acct_terminate_cause =
593 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
594 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800595 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700596 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
597 HOSTAPD_LEVEL_INFO, "disassociated due to "
598 "inactivity");
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700599 reason = (sta->timeout_next == STA_DISASSOC) ?
600 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
601 WLAN_REASON_PREV_AUTH_NOT_VALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
604 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
605 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
607 hapd, sta);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700608 mlme_disassociate_indication(hapd, sta, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700609 break;
610 case STA_DEAUTH:
611 case STA_REMOVE:
612 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
613 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800614 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700615 if (!sta->acct_terminate_cause)
616 sta->acct_terminate_cause =
617 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
618 mlme_deauthenticate_indication(
619 hapd, sta,
620 WLAN_REASON_PREV_AUTH_NOT_VALID);
621 ap_free_sta(hapd, sta);
622 break;
623 }
624}
625
626
627static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
628{
629 struct hostapd_data *hapd = eloop_ctx;
630 struct sta_info *sta = timeout_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700631
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800632 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
633 hapd->conf->iface, MAC2STR(sta->addr));
Hai Shalomfdcde762020-04-02 11:19:20 -0700634 if (!(sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC |
635 WLAN_STA_AUTHORIZED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700636 if (sta->flags & WLAN_STA_GAS) {
637 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
638 "entry " MACSTR, MAC2STR(sta->addr));
639 ap_free_sta(hapd, sta);
640 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700641 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700643
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700644 hostapd_drv_sta_deauth(hapd, sta->addr,
645 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 mlme_deauthenticate_indication(hapd, sta,
647 WLAN_REASON_PREV_AUTH_NOT_VALID);
648 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
649 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
650 "session timeout");
651 sta->acct_terminate_cause =
652 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700653 ap_free_sta(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700654}
655
656
Dmitry Shmidt54605472013-11-08 11:10:19 -0800657void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
658 u32 session_timeout)
659{
660 if (eloop_replenish_timeout(session_timeout, 0,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800661 ap_handle_session_timer, hapd, sta) == 1) {
Dmitry Shmidt54605472013-11-08 11:10:19 -0800662 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
663 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
664 "to %d seconds", session_timeout);
665 }
666}
667
668
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700669void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
670 u32 session_timeout)
671{
672 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
673 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
674 "seconds", session_timeout);
675 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
676 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
677 hapd, sta);
678}
679
680
681void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
682{
683 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
684}
685
686
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800687static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
688{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700689#ifdef CONFIG_WNM_AP
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800690 struct hostapd_data *hapd = eloop_ctx;
691 struct sta_info *sta = timeout_ctx;
692
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800693 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
694 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800695 if (sta->hs20_session_info_url == NULL)
696 return;
697
698 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
699 sta->hs20_disassoc_timer);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700700#endif /* CONFIG_WNM_AP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800701}
702
703
704void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
705 struct sta_info *sta, int warning_time)
706{
707 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
708 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
709 hapd, sta);
710}
711
712
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700713struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
714{
715 struct sta_info *sta;
Hai Shalom81f62d82019-07-22 12:10:00 -0700716 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700717
718 sta = ap_get_sta(hapd, addr);
719 if (sta)
720 return sta;
721
722 wpa_printf(MSG_DEBUG, " New STA");
723 if (hapd->num_sta >= hapd->conf->max_num_sta) {
724 /* FIX: might try to remove some old STAs first? */
725 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
726 hapd->num_sta, hapd->conf->max_num_sta);
727 return NULL;
728 }
729
730 sta = os_zalloc(sizeof(struct sta_info));
731 if (sta == NULL) {
732 wpa_printf(MSG_ERROR, "malloc failed");
733 return NULL;
734 }
735 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800736 if (accounting_sta_get_id(hapd, sta) < 0) {
737 os_free(sta);
738 return NULL;
739 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700740
Hai Shalom81f62d82019-07-22 12:10:00 -0700741 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
742 if (!hapd->iface->basic_rates)
743 break;
744 if (hapd->iface->basic_rates[i] < 0)
745 break;
746 sta->supported_rates[i] = hapd->iface->basic_rates[i] / 5;
747 }
748 sta->supported_rates_len = i;
749
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800750 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
751 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
752 "for " MACSTR " (%d seconds - ap_max_inactivity)",
753 __func__, MAC2STR(addr),
754 hapd->conf->ap_max_inactivity);
755 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
756 ap_handle_timer, hapd, sta);
757 }
758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759 /* initialize STA info data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700760 os_memcpy(sta->addr, addr, ETH_ALEN);
761 sta->next = hapd->sta_list;
762 hapd->sta_list = sta;
763 hapd->num_sta++;
764 ap_sta_hash_add(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700765 ap_sta_remove_in_other_bss(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800766 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
767 dl_list_init(&sta->ip6addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700769#ifdef CONFIG_TAXONOMY
770 sta_track_claim_taxonomy_info(hapd->iface, addr,
771 &sta->probe_ie_taxonomy);
772#endif /* CONFIG_TAXONOMY */
773
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774 return sta;
775}
776
777
778static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
779{
780 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
781
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800782 if (sta->ipaddr)
783 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
784 ap_sta_ip6addr_del(hapd, sta);
785
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800786 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
787 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700788 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
789 sta->flags & WLAN_STA_ASSOC) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800790 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
791 " from kernel driver",
792 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700793 return -1;
794 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800795 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700796 return 0;
797}
798
799
800static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
801 struct sta_info *sta)
802{
803 struct hostapd_iface *iface = hapd->iface;
804 size_t i;
805
806 for (i = 0; i < iface->num_bss; i++) {
807 struct hostapd_data *bss = iface->bss[i];
808 struct sta_info *sta2;
809 /* bss should always be set during operation, but it may be
810 * NULL during reconfiguration. Assume the STA is not
811 * associated to another BSS in that case to avoid NULL pointer
812 * dereferences. */
813 if (bss == hapd || bss == NULL)
814 continue;
815 sta2 = ap_get_sta(bss, sta->addr);
816 if (!sta2)
817 continue;
818
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800819 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
820 " association from another BSS %s",
821 hapd->conf->iface, MAC2STR(sta2->addr),
822 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823 ap_sta_disconnect(bss, sta2, sta2->addr,
824 WLAN_REASON_PREV_AUTH_NOT_VALID);
825 }
826}
827
828
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800829static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
830{
831 struct hostapd_data *hapd = eloop_ctx;
832 struct sta_info *sta = timeout_ctx;
833
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800834 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
835 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800836 ap_sta_remove(hapd, sta);
837 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
838}
839
840
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
842 u16 reason)
843{
844 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
845 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800846 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800847 if (hapd->iface->current_mode &&
848 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
849 /* Skip deauthentication in DMG/IEEE 802.11ad */
850 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
851 WLAN_STA_ASSOC_REQ_OK);
852 sta->timeout_next = STA_REMOVE;
853 } else {
854 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
855 sta->timeout_next = STA_DEAUTH;
856 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800857 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700858 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
860 "for " MACSTR " (%d seconds - "
861 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
862 __func__, MAC2STR(sta->addr),
863 AP_MAX_INACTIVITY_AFTER_DISASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
865 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
866 ap_handle_timer, hapd, sta);
867 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800868 ieee802_1x_free_station(hapd, sta);
Hai Shalom81f62d82019-07-22 12:10:00 -0700869 wpa_auth_sta_deinit(sta->wpa_sm);
870 sta->wpa_sm = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800872 sta->disassoc_reason = reason;
873 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
874 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
875 eloop_register_timeout(hapd->iface->drv_flags &
876 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
877 ap_sta_disassoc_cb_timeout, hapd, sta);
878}
879
880
881static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
882{
883 struct hostapd_data *hapd = eloop_ctx;
884 struct sta_info *sta = timeout_ctx;
885
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800886 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
887 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800888 ap_sta_remove(hapd, sta);
889 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700890}
891
892
893void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
894 u16 reason)
895{
Dmitry Shmidt29333592017-01-09 12:27:11 -0800896 if (hapd->iface->current_mode &&
897 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
898 /* Deauthentication is not used in DMG/IEEE 802.11ad;
899 * disassociate the STA instead. */
900 ap_sta_disassociate(hapd, sta, reason);
901 return;
902 }
903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
905 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800906 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
907 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800908 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700909 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700910 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700911 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
912 "for " MACSTR " (%d seconds - "
913 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
914 __func__, MAC2STR(sta->addr),
915 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700916 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
917 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
918 ap_handle_timer, hapd, sta);
919 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800920 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700921
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800922 sta->deauth_reason = reason;
923 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
924 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
925 eloop_register_timeout(hapd->iface->drv_flags &
926 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
927 ap_sta_deauth_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928}
929
930
Dmitry Shmidt04949592012-07-19 12:16:46 -0700931#ifdef CONFIG_WPS
932int ap_sta_wps_cancel(struct hostapd_data *hapd,
933 struct sta_info *sta, void *ctx)
934{
935 if (sta && (sta->flags & WLAN_STA_WPS)) {
936 ap_sta_deauthenticate(hapd, sta,
937 WLAN_REASON_PREV_AUTH_NOT_VALID);
938 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
939 __func__, MAC2STR(sta->addr));
940 return 1;
941 }
942
943 return 0;
944}
945#endif /* CONFIG_WPS */
946
947
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800948static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
949{
950 struct hostapd_vlan *vlan;
951 int vlan_id = MAX_VLAN_ID + 2;
952
953retry:
954 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
955 if (vlan->vlan_id == vlan_id) {
956 vlan_id++;
957 goto retry;
958 }
959 }
960 return vlan_id;
961}
962
963
964int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
965 struct vlan_description *vlan_desc)
966{
967 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
968 int old_vlan_id, vlan_id = 0, ret = 0;
969
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800970 /* Check if there is something to do */
971 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
972 /* This sta is lacking its own vif */
973 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
974 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
975 /* sta->vlan_id needs to be reset */
976 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
977 return 0; /* nothing to change */
978 }
979
980 /* Now the real VLAN changed or the STA just needs its own vif */
981 if (hapd->conf->ssid.per_sta_vif) {
982 /* Assign a new vif, always */
983 /* find a free vlan_id sufficiently big */
984 vlan_id = ap_sta_get_free_vlan_id(hapd);
985 /* Get wildcard VLAN */
986 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
987 if (vlan->vlan_id == VLAN_ID_WILDCARD)
988 break;
989 }
990 if (!vlan) {
991 hostapd_logger(hapd, sta->addr,
992 HOSTAPD_MODULE_IEEE80211,
993 HOSTAPD_LEVEL_DEBUG,
994 "per_sta_vif missing wildcard");
995 vlan_id = 0;
996 ret = -1;
997 goto done;
998 }
999 } else if (vlan_desc && vlan_desc->notempty) {
1000 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1001 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
1002 break;
1003 if (vlan->vlan_id == VLAN_ID_WILDCARD)
1004 wildcard_vlan = vlan;
1005 }
1006 if (vlan) {
1007 vlan_id = vlan->vlan_id;
1008 } else if (wildcard_vlan) {
1009 vlan = wildcard_vlan;
1010 vlan_id = vlan_desc->untagged;
1011 if (vlan_desc->tagged[0]) {
1012 /* Tagged VLAN configuration */
1013 vlan_id = ap_sta_get_free_vlan_id(hapd);
1014 }
1015 } else {
1016 hostapd_logger(hapd, sta->addr,
1017 HOSTAPD_MODULE_IEEE80211,
1018 HOSTAPD_LEVEL_DEBUG,
1019 "missing vlan and wildcard for vlan=%d%s",
1020 vlan_desc->untagged,
1021 vlan_desc->tagged[0] ? "+" : "");
1022 vlan_id = 0;
1023 ret = -1;
1024 goto done;
1025 }
1026 }
1027
1028 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
1029 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
1030 if (vlan == NULL) {
1031 hostapd_logger(hapd, sta->addr,
1032 HOSTAPD_MODULE_IEEE80211,
1033 HOSTAPD_LEVEL_DEBUG,
1034 "could not add dynamic VLAN interface for vlan=%d%s",
1035 vlan_desc ? vlan_desc->untagged : -1,
1036 (vlan_desc && vlan_desc->tagged[0]) ?
1037 "+" : "");
1038 vlan_id = 0;
1039 ret = -1;
1040 goto done;
1041 }
1042
1043 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1044 HOSTAPD_LEVEL_DEBUG,
1045 "added new dynamic VLAN interface '%s'",
1046 vlan->ifname);
1047 } else if (vlan && vlan->dynamic_vlan > 0) {
1048 vlan->dynamic_vlan++;
1049 hostapd_logger(hapd, sta->addr,
1050 HOSTAPD_MODULE_IEEE80211,
1051 HOSTAPD_LEVEL_DEBUG,
1052 "updated existing dynamic VLAN interface '%s'",
1053 vlan->ifname);
1054 }
1055done:
1056 old_vlan_id = sta->vlan_id;
1057 sta->vlan_id = vlan_id;
1058 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
1059
1060 if (vlan_id != old_vlan_id && old_vlan_id)
1061 vlan_remove_dynamic(hapd, old_vlan_id);
1062
1063 return ret;
1064}
1065
1066
Dmitry Shmidt83474442015-04-15 13:47:09 -07001067int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001068{
1069#ifndef CONFIG_NO_VLAN
1070 const char *iface;
1071 struct hostapd_vlan *vlan = NULL;
1072 int ret;
Dmitry Shmidt83474442015-04-15 13:47:09 -07001073 int old_vlanid = sta->vlan_id_bound;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001074
Hai Shalomfdcde762020-04-02 11:19:20 -07001075 if ((sta->flags & WLAN_STA_WDS) && sta->vlan_id == 0) {
1076 wpa_printf(MSG_DEBUG,
1077 "Do not override WDS VLAN assignment for STA "
1078 MACSTR, MAC2STR(sta->addr));
1079 return 0;
1080 }
1081
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001082 iface = hapd->conf->iface;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001083 if (hapd->conf->ssid.vlan[0])
1084 iface = hapd->conf->ssid.vlan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001085
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001086 if (sta->vlan_id > 0) {
1087 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001088 if (vlan->vlan_id == sta->vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001089 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090 }
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001091 if (vlan)
1092 iface = vlan->ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001093 }
1094
Dmitry Shmidt83474442015-04-15 13:47:09 -07001095 /*
1096 * Do not increment ref counters if the VLAN ID remains same, but do
1097 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1098 * have been called before.
1099 */
1100 if (sta->vlan_id == old_vlanid)
1101 goto skip_counting;
1102
Hai Shalomfdcde762020-04-02 11:19:20 -07001103 if (sta->vlan_id > 0 && !vlan &&
1104 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001105 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1106 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1107 "binding station to (vlan_id=%d)",
1108 sta->vlan_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001109 ret = -1;
1110 goto done;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001111 } else if (vlan && vlan->dynamic_vlan > 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001112 vlan->dynamic_vlan++;
1113 hostapd_logger(hapd, sta->addr,
1114 HOSTAPD_MODULE_IEEE80211,
1115 HOSTAPD_LEVEL_DEBUG,
1116 "updated existing dynamic VLAN interface '%s'",
1117 iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 }
1119
Dmitry Shmidt83474442015-04-15 13:47:09 -07001120 /* ref counters have been increased, so mark the station */
1121 sta->vlan_id_bound = sta->vlan_id;
1122
1123skip_counting:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1125 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1126 "'%s'", iface);
1127
1128 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1129 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1130
1131 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
1132 if (ret < 0) {
1133 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1134 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1135 "entry to vlan_id=%d", sta->vlan_id);
1136 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001137
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001138 /* During 1x reauth, if the vlan id changes, then remove the old id. */
Dmitry Shmidt83474442015-04-15 13:47:09 -07001139 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001140 vlan_remove_dynamic(hapd, old_vlanid);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001141done:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001142
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143 return ret;
1144#else /* CONFIG_NO_VLAN */
1145 return 0;
1146#endif /* CONFIG_NO_VLAN */
1147}
1148
1149
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001150int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1151{
1152 u32 tu;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001153 struct os_reltime now, passed;
1154 os_get_reltime(&now);
1155 os_reltime_sub(&now, &sta->sa_query_start, &passed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1157 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1158 hostapd_logger(hapd, sta->addr,
1159 HOSTAPD_MODULE_IEEE80211,
1160 HOSTAPD_LEVEL_DEBUG,
1161 "association SA Query timed out");
1162 sta->sa_query_timed_out = 1;
1163 os_free(sta->sa_query_trans_id);
1164 sta->sa_query_trans_id = NULL;
1165 sta->sa_query_count = 0;
1166 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1167 return 1;
1168 }
1169
1170 return 0;
1171}
1172
1173
1174static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1175{
1176 struct hostapd_data *hapd = eloop_ctx;
1177 struct sta_info *sta = timeout_ctx;
1178 unsigned int timeout, sec, usec;
1179 u8 *trans_id, *nbuf;
1180
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001181 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1182 " (count=%d)",
1183 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1184
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001185 if (sta->sa_query_count > 0 &&
1186 ap_check_sa_query_timeout(hapd, sta))
1187 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07001188 if (sta->sa_query_count >= 1000)
1189 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001190
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001191 nbuf = os_realloc_array(sta->sa_query_trans_id,
1192 sta->sa_query_count + 1,
1193 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001194 if (nbuf == NULL)
1195 return;
1196 if (sta->sa_query_count == 0) {
1197 /* Starting a new SA Query procedure */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001198 os_get_reltime(&sta->sa_query_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 }
1200 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1201 sta->sa_query_trans_id = nbuf;
1202 sta->sa_query_count++;
1203
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001204 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1205 /*
1206 * We don't really care which ID is used here, so simply
1207 * hardcode this if the mostly theoretical os_get_random()
1208 * failure happens.
1209 */
1210 trans_id[0] = 0x12;
1211 trans_id[1] = 0x34;
1212 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001213
1214 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1215 sec = ((timeout / 1000) * 1024) / 1000;
1216 usec = (timeout % 1000) * 1024;
1217 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1218
1219 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1220 HOSTAPD_LEVEL_DEBUG,
1221 "association SA Query attempt %d", sta->sa_query_count);
1222
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001223 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001224}
1225
1226
1227void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1228{
1229 ap_sa_query_timer(hapd, sta);
1230}
1231
1232
1233void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1234{
1235 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1236 os_free(sta->sa_query_trans_id);
1237 sta->sa_query_trans_id = NULL;
1238 sta->sa_query_count = 0;
1239}
1240
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001241
Hai Shalom74f70d42019-02-11 14:42:39 -08001242const char * ap_sta_wpa_get_keyid(struct hostapd_data *hapd,
1243 struct sta_info *sta)
1244{
1245 struct hostapd_wpa_psk *psk;
1246 struct hostapd_ssid *ssid;
1247 const u8 *pmk;
1248 int pmk_len;
1249
1250 ssid = &hapd->conf->ssid;
1251
1252 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1253 if (!pmk || pmk_len != PMK_LEN)
1254 return NULL;
1255
1256 for (psk = ssid->wpa_psk; psk; psk = psk->next)
1257 if (os_memcmp(pmk, psk->psk, PMK_LEN) == 0)
1258 break;
Hai Shalom74f70d42019-02-11 14:42:39 -08001259 if (!psk || !psk->keyid[0])
1260 return NULL;
1261
1262 return psk->keyid;
1263}
1264
1265
Sunil Ravi77d572f2023-01-17 23:58:31 +00001266const u8 * ap_sta_wpa_get_dpp_pkhash(struct hostapd_data *hapd,
1267 struct sta_info *sta)
1268{
1269 return wpa_auth_get_dpp_pkhash(sta->wpa_sm);
1270}
1271
1272
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001273void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1274 int authorized)
1275{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001276 const u8 *dev_addr = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001277 char buf[100];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001278#ifdef CONFIG_P2P
1279 u8 addr[ETH_ALEN];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001280 u8 ip_addr_buf[4];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001281#endif /* CONFIG_P2P */
1282
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001283 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1284 return;
1285
Sunil Ravi640215c2023-06-28 23:08:09 +00001286 if (authorized) {
1287 hostapd_prune_associations(hapd, sta->addr);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001288 sta->flags |= WLAN_STA_AUTHORIZED;
Sunil Ravi640215c2023-06-28 23:08:09 +00001289 } else {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001290 sta->flags &= ~WLAN_STA_AUTHORIZED;
Sunil Ravi640215c2023-06-28 23:08:09 +00001291 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001292
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001293#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001294 if (hapd->p2p_group == NULL) {
1295 if (sta->p2p_ie != NULL &&
1296 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1297 dev_addr = addr;
1298 } else
1299 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001300
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001301 if (dev_addr)
1302 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1303 MAC2STR(sta->addr), MAC2STR(dev_addr));
1304 else
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001305#endif /* CONFIG_P2P */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001306 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1307
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001308 if (hapd->sta_authorized_cb)
1309 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1310 sta->addr, authorized, dev_addr);
1311
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001312 if (authorized) {
Sunil Ravi77d572f2023-01-17 23:58:31 +00001313 const u8 *dpp_pkhash;
Hai Shalom74f70d42019-02-11 14:42:39 -08001314 const char *keyid;
Sunil Ravi77d572f2023-01-17 23:58:31 +00001315 char dpp_pkhash_buf[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001316 char keyid_buf[100];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001317 char ip_addr[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001318
Sunil Ravi77d572f2023-01-17 23:58:31 +00001319 dpp_pkhash_buf[0] = '\0';
Hai Shalom74f70d42019-02-11 14:42:39 -08001320 keyid_buf[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001321 ip_addr[0] = '\0';
1322#ifdef CONFIG_P2P
1323 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1324 os_snprintf(ip_addr, sizeof(ip_addr),
1325 " ip_addr=%u.%u.%u.%u",
1326 ip_addr_buf[0], ip_addr_buf[1],
1327 ip_addr_buf[2], ip_addr_buf[3]);
1328 }
1329#endif /* CONFIG_P2P */
1330
Hai Shalom74f70d42019-02-11 14:42:39 -08001331 keyid = ap_sta_wpa_get_keyid(hapd, sta);
1332 if (keyid) {
1333 os_snprintf(keyid_buf, sizeof(keyid_buf),
1334 " keyid=%s", keyid);
1335 }
1336
Sunil Ravi77d572f2023-01-17 23:58:31 +00001337 dpp_pkhash = ap_sta_wpa_get_dpp_pkhash(hapd, sta);
1338 if (dpp_pkhash) {
1339 const char *prefix = " dpp_pkhash=";
1340 size_t plen = os_strlen(prefix);
1341
1342 os_strlcpy(dpp_pkhash_buf, prefix,
1343 sizeof(dpp_pkhash_buf));
1344 wpa_snprintf_hex(&dpp_pkhash_buf[plen],
1345 sizeof(dpp_pkhash_buf) - plen,
1346 dpp_pkhash, SHA256_MAC_LEN);
1347 }
1348
1349 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s%s",
1350 buf, ip_addr, keyid_buf, dpp_pkhash_buf);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001351
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001352 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001353 hapd->msg_ctx_parent != hapd->msg_ctx)
1354 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
Sunil Ravi77d572f2023-01-17 23:58:31 +00001355 AP_STA_CONNECTED "%s%s%s%s",
1356 buf, ip_addr, keyid_buf,
1357 dpp_pkhash_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001358 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001359 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001361 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001362 hapd->msg_ctx_parent != hapd->msg_ctx)
1363 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1364 AP_STA_DISCONNECTED "%s", buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001365 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001366
1367#ifdef CONFIG_FST
1368 if (hapd->iface->fst) {
1369 if (authorized)
1370 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1371 else
1372 fst_notify_peer_disconnected(hapd->iface->fst,
1373 sta->addr);
1374 }
1375#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001376}
1377
1378
1379void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1380 const u8 *addr, u16 reason)
1381{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001382 if (sta)
1383 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1384 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1385 reason);
1386 else if (addr)
1387 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1388 hapd->conf->iface, __func__, MAC2STR(addr),
1389 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001390
1391 if (sta == NULL && addr)
1392 sta = ap_get_sta(hapd, addr);
1393
1394 if (addr)
1395 hostapd_drv_sta_deauth(hapd, addr, reason);
1396
1397 if (sta == NULL)
1398 return;
1399 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -07001400 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1401 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001402 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1403 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001404 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
Dmitry Shmidt04949592012-07-19 12:16:46 -07001405 "for " MACSTR " (%d seconds - "
1406 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001407 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001408 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001409 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001410 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1411 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001413
Dmitry Shmidt29333592017-01-09 12:27:11 -08001414 if (hapd->iface->current_mode &&
1415 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1416 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1417 * disassociate the STA instead. */
1418 sta->disassoc_reason = reason;
1419 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1420 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1421 eloop_register_timeout(hapd->iface->drv_flags &
1422 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ?
1423 2 : 0, 0, ap_sta_disassoc_cb_timeout,
1424 hapd, sta);
1425 return;
1426 }
1427
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001428 sta->deauth_reason = reason;
1429 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1430 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1431 eloop_register_timeout(hapd->iface->drv_flags &
1432 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1433 ap_sta_deauth_cb_timeout, hapd, sta);
1434}
1435
1436
1437void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1438{
1439 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1440 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1441 return;
1442 }
1443 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1444 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1445 ap_sta_deauth_cb_timeout(hapd, sta);
1446}
1447
1448
1449void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1450{
1451 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1452 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1453 return;
1454 }
1455 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1456 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1457 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001458}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001459
1460
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001461void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1462 struct sta_info *sta)
1463{
1464 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1465 wpa_printf(MSG_DEBUG,
1466 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1467 MACSTR,
1468 hapd->conf->iface, MAC2STR(sta->addr));
1469 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1470 wpa_printf(MSG_DEBUG,
1471 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1472 MACSTR,
1473 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt29333592017-01-09 12:27:11 -08001474 if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1475 {
1476 wpa_printf(MSG_DEBUG,
1477 "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1478 MACSTR,
1479 hapd->conf->iface, MAC2STR(sta->addr));
1480 if (sta->flags & WLAN_STA_WPS)
1481 hostapd_wps_eap_completed(hapd);
1482 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001483}
1484
1485
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001486int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1487{
1488 int res;
1489
1490 buf[0] = '\0';
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001491 res = os_snprintf(buf, buflen,
Sunil Ravia04bd252022-05-02 22:54:18 -07001492 "%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 -08001493 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1494 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1495 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1496 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1497 ""),
1498 (flags & WLAN_STA_SHORT_PREAMBLE ?
1499 "[SHORT_PREAMBLE]" : ""),
1500 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1501 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1502 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1503 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1504 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1505 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1506 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1507 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1508 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
Roshan Pius3a1667e2018-07-03 15:17:14 -07001509 (flags & WLAN_STA_HT ? "[HT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001510 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
Hai Shalomc3565922019-10-28 11:58:20 -07001511 (flags & WLAN_STA_HE ? "[HE]" : ""),
Sunil Ravia04bd252022-05-02 22:54:18 -07001512 (flags & WLAN_STA_EHT ? "[EHT]" : ""),
Hai Shalom4fbc08f2020-05-18 12:37:00 -07001513 (flags & WLAN_STA_6GHZ ? "[6GHZ]" : ""),
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001514 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001515 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1516 "[WNM_SLEEP_MODE]" : ""));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001517 if (os_snprintf_error(buflen, res))
1518 res = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001519
1520 return res;
1521}
Dmitry Shmidt29333592017-01-09 12:27:11 -08001522
1523
1524static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1525{
1526 struct hostapd_data *hapd = eloop_ctx;
1527 struct sta_info *sta = timeout_ctx;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001528 u16 reason;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001529
1530 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1531 "IEEE 802.1X: Scheduled disconnection of " MACSTR
1532 " after EAP-Failure", MAC2STR(sta->addr));
1533
Roshan Pius3a1667e2018-07-03 15:17:14 -07001534 reason = sta->disconnect_reason_code;
1535 if (!reason)
1536 reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1537 ap_sta_disconnect(hapd, sta, sta->addr, reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001538 if (sta->flags & WLAN_STA_WPS)
1539 hostapd_wps_eap_completed(hapd);
1540}
1541
1542
1543void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
Sunil Ravi640215c2023-06-28 23:08:09 +00001544 struct sta_info *sta,
1545 unsigned timeout)
Dmitry Shmidt29333592017-01-09 12:27:11 -08001546{
1547 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1548 "IEEE 802.1X: Force disconnection of " MACSTR
Sunil Ravi640215c2023-06-28 23:08:09 +00001549 " after EAP-Failure in %u ms", MAC2STR(sta->addr), timeout);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001550
1551 /*
1552 * Add a small sleep to increase likelihood of previously requested
1553 * EAP-Failure TX getting out before this should the driver reorder
1554 * operations.
1555 */
1556 eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
Sunil Ravi640215c2023-06-28 23:08:09 +00001557 eloop_register_timeout(0, timeout * 1000,
1558 ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001559}
1560
1561
1562int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1563 struct sta_info *sta)
1564{
1565 return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1566 hapd, sta);
1567}
Hai Shalomb755a2a2020-04-23 21:49:02 -07001568
1569
1570int ap_sta_re_add(struct hostapd_data *hapd, struct sta_info *sta)
1571{
1572 /*
1573 * If a station that is already associated to the AP, is trying to
1574 * authenticate again, remove the STA entry, in order to make sure the
1575 * STA PS state gets cleared and configuration gets updated. To handle
1576 * this, station's added_unassoc flag is cleared once the station has
1577 * completed association.
1578 */
1579 ap_sta_set_authorized(hapd, sta, 0);
1580 hostapd_drv_sta_remove(hapd, sta->addr);
1581 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH | WLAN_STA_AUTHORIZED);
1582
1583 if (hostapd_sta_add(hapd, sta->addr, 0, 0,
1584 sta->supported_rates,
1585 sta->supported_rates_len,
Sunil Ravia04bd252022-05-02 22:54:18 -07001586 0, NULL, NULL, NULL, 0, NULL, 0, NULL,
Hai Shalomb755a2a2020-04-23 21:49:02 -07001587 sta->flags, 0, 0, 0, 0)) {
1588 hostapd_logger(hapd, sta->addr,
1589 HOSTAPD_MODULE_IEEE80211,
1590 HOSTAPD_LEVEL_NOTICE,
1591 "Could not add STA to kernel driver");
1592 return -1;
1593 }
1594
1595 sta->added_unassoc = 1;
1596 return 0;
1597}