blob: 93f1f0c201e14d31f0c69de5c133ff8d09359b17 [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
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700159void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
160{
161 int set_beacon = 0;
162
163 accounting_sta_stop(hapd, sta);
164
165 /* just in case */
166 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700167 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168
Hai Shalom74f70d42019-02-11 14:42:39 -0800169 if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP))
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700170 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800172 if (sta->ipaddr)
173 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
174 ap_sta_ip6addr_del(hapd, sta);
175
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800176 if (!hapd->iface->driver_ap_teardown &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800177 !(sta->flags & WLAN_STA_PREAUTH)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700178 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800179 sta->added_unassoc = 0;
180 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700181
182 ap_sta_hash_del(hapd, sta);
183 ap_sta_list_del(hapd, sta);
184
185 if (sta->aid > 0)
186 hapd->sta_aid[(sta->aid - 1) / 32] &=
187 ~BIT((sta->aid - 1) % 32);
188
189 hapd->num_sta--;
190 if (sta->nonerp_set) {
191 sta->nonerp_set = 0;
192 hapd->iface->num_sta_non_erp--;
193 if (hapd->iface->num_sta_non_erp == 0)
194 set_beacon++;
195 }
196
197 if (sta->no_short_slot_time_set) {
198 sta->no_short_slot_time_set = 0;
199 hapd->iface->num_sta_no_short_slot_time--;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700200 if (hapd->iface->current_mode &&
201 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700202 && hapd->iface->num_sta_no_short_slot_time == 0)
203 set_beacon++;
204 }
205
206 if (sta->no_short_preamble_set) {
207 sta->no_short_preamble_set = 0;
208 hapd->iface->num_sta_no_short_preamble--;
Roshan Pius3a1667e2018-07-03 15:17:14 -0700209 if (hapd->iface->current_mode &&
210 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211 && hapd->iface->num_sta_no_short_preamble == 0)
212 set_beacon++;
213 }
214
215 if (sta->no_ht_gf_set) {
216 sta->no_ht_gf_set = 0;
217 hapd->iface->num_sta_ht_no_gf--;
218 }
219
220 if (sta->no_ht_set) {
221 sta->no_ht_set = 0;
222 hapd->iface->num_sta_no_ht--;
223 }
224
225 if (sta->ht_20mhz_set) {
226 sta->ht_20mhz_set = 0;
227 hapd->iface->num_sta_ht_20mhz--;
228 }
229
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700230#ifdef CONFIG_TAXONOMY
231 wpabuf_free(sta->probe_ie_taxonomy);
232 sta->probe_ie_taxonomy = NULL;
233 wpabuf_free(sta->assoc_ie_taxonomy);
234 sta->assoc_ie_taxonomy = NULL;
235#endif /* CONFIG_TAXONOMY */
236
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700237 ht40_intolerant_remove(hapd->iface, sta);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700238
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700239#ifdef CONFIG_P2P
240 if (sta->no_p2p_set) {
241 sta->no_p2p_set = 0;
242 hapd->num_sta_no_p2p--;
243 if (hapd->num_sta_no_p2p == 0)
244 hostapd_p2p_non_p2p_sta_disconnected(hapd);
245 }
246#endif /* CONFIG_P2P */
247
Hai Shalomfdcde762020-04-02 11:19:20 -0700248#ifdef NEED_AP_MLME
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 if (hostapd_ht_operation_update(hapd->iface) > 0)
250 set_beacon++;
Hai Shalomfdcde762020-04-02 11:19:20 -0700251#endif /* NEED_AP_MLME */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700252
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800253#ifdef CONFIG_MESH
254 if (hapd->mesh_sta_free_cb)
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800255 hapd->mesh_sta_free_cb(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800256#endif /* CONFIG_MESH */
257
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700258 if (set_beacon)
259 ieee802_11_set_beacons(hapd->iface);
260
Dmitry Shmidt04949592012-07-19 12:16:46 -0700261 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
262 __func__, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
264 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800265 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800266 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800267 sae_clear_retransmit_timer(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800269 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 wpa_auth_sta_deinit(sta->wpa_sm);
271 rsn_preauth_free_station(hapd, sta);
272#ifndef CONFIG_NO_RADIUS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700273 if (hapd->radius)
274 radius_client_flush_auth(hapd->radius, sta->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700275#endif /* CONFIG_NO_RADIUS */
276
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800277#ifndef CONFIG_NO_VLAN
278 /*
279 * sta->wpa_sm->group needs to be released before so that
280 * vlan_remove_dynamic() can check that no stations are left on the
281 * AP_VLAN netdev.
282 */
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800283 if (sta->vlan_id)
284 vlan_remove_dynamic(hapd, sta->vlan_id);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800285 if (sta->vlan_id_bound) {
286 /*
287 * Need to remove the STA entry before potentially removing the
288 * VLAN.
289 */
290 if (hapd->iface->driver_ap_teardown &&
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800291 !(sta->flags & WLAN_STA_PREAUTH)) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800292 hostapd_drv_sta_remove(hapd, sta->addr);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800293 sta->added_unassoc = 0;
294 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800295 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
296 }
297#endif /* CONFIG_NO_VLAN */
298
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700299 os_free(sta->challenge);
300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 os_free(sta->sa_query_trans_id);
302 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303
304#ifdef CONFIG_P2P
305 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
306#endif /* CONFIG_P2P */
307
Dmitry Shmidt04949592012-07-19 12:16:46 -0700308#ifdef CONFIG_INTERWORKING
309 if (sta->gas_dialog) {
310 int i;
311 for (i = 0; i < GAS_DIALOG_MAX; i++)
312 gas_serv_dialog_clear(&sta->gas_dialog[i]);
313 os_free(sta->gas_dialog);
314 }
315#endif /* CONFIG_INTERWORKING */
316
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700317 wpabuf_free(sta->wps_ie);
318 wpabuf_free(sta->p2p_ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800319 wpabuf_free(sta->hs20_ie);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700320 wpabuf_free(sta->roaming_consortium);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800321#ifdef CONFIG_FST
322 wpabuf_free(sta->mb_ies);
323#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700324
325 os_free(sta->ht_capabilities);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800326 os_free(sta->vht_capabilities);
Hai Shalom74f70d42019-02-11 14:42:39 -0800327 os_free(sta->vht_operation);
Hai Shalom81f62d82019-07-22 12:10:00 -0700328 os_free(sta->he_capab);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800329 hostapd_free_psk_list(sta->psk);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700330 os_free(sta->identity);
331 os_free(sta->radius_cui);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800332 os_free(sta->remediation_url);
Roshan Pius3a1667e2018-07-03 15:17:14 -0700333 os_free(sta->t_c_url);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800334 wpabuf_free(sta->hs20_deauth_req);
335 os_free(sta->hs20_session_info_url);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700336
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800337#ifdef CONFIG_SAE
338 sae_clear_data(sta->sae);
339 os_free(sta->sae);
340#endif /* CONFIG_SAE */
341
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800342 mbo_ap_sta_free(sta);
Dmitry Shmidt9c175262016-03-03 10:20:07 -0800343 os_free(sta->supp_op_classes);
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800344
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800345#ifdef CONFIG_FILS
346 os_free(sta->fils_pending_assoc_req);
347 wpabuf_free(sta->fils_hlp_resp);
348 wpabuf_free(sta->hlp_dhcp_discover);
349 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700350#ifdef CONFIG_FILS_SK_PFS
351 crypto_ecdh_deinit(sta->fils_ecdh);
352 wpabuf_clear_free(sta->fils_dh_ss);
353 wpabuf_free(sta->fils_g_sta);
354#endif /* CONFIG_FILS_SK_PFS */
Dmitry Shmidtebd93af2017-02-21 13:40:44 -0800355#endif /* CONFIG_FILS */
356
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700357#ifdef CONFIG_OWE
358 bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
359 crypto_ecdh_deinit(sta->owe_ecdh);
360#endif /* CONFIG_OWE */
361
Hai Shalom021b0b52019-04-10 11:17:58 -0700362#ifdef CONFIG_DPP2
363 dpp_pfs_free(sta->dpp_pfs);
364 sta->dpp_pfs = NULL;
365#endif /* CONFIG_DPP2 */
366
Roshan Pius3a1667e2018-07-03 15:17:14 -0700367 os_free(sta->ext_capability);
368
369#ifdef CONFIG_WNM_AP
370 eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
371#endif /* CONFIG_WNM_AP */
372
373 os_free(sta->ifname_wds);
374
Hai Shalomfdcde762020-04-02 11:19:20 -0700375#ifdef CONFIG_TESTING_OPTIONS
376 os_free(sta->sae_postponed_commit);
377#endif /* CONFIG_TESTING_OPTIONS */
378
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700379 os_free(sta);
380}
381
382
383void hostapd_free_stas(struct hostapd_data *hapd)
384{
385 struct sta_info *sta, *prev;
386
387 sta = hapd->sta_list;
388
389 while (sta) {
390 prev = sta;
391 if (sta->flags & WLAN_STA_AUTH) {
392 mlme_deauthenticate_indication(
393 hapd, sta, WLAN_REASON_UNSPECIFIED);
394 }
395 sta = sta->next;
396 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
397 MAC2STR(prev->addr));
398 ap_free_sta(hapd, prev);
399 }
400}
401
402
403/**
404 * ap_handle_timer - Per STA timer handler
405 * @eloop_ctx: struct hostapd_data *
406 * @timeout_ctx: struct sta_info *
407 *
408 * This function is called to check station activity and to remove inactive
409 * stations.
410 */
411void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
412{
413 struct hostapd_data *hapd = eloop_ctx;
414 struct sta_info *sta = timeout_ctx;
415 unsigned long next_time = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700416 int reason;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800418 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
419 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700420 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700421 if (sta->timeout_next == STA_REMOVE) {
422 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
423 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
424 "local deauth request");
425 ap_free_sta(hapd, sta);
426 return;
427 }
428
429 if ((sta->flags & WLAN_STA_ASSOC) &&
430 (sta->timeout_next == STA_NULLFUNC ||
431 sta->timeout_next == STA_DISASSOC)) {
432 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700433 /*
434 * Add random value to timeout so that we don't end up bouncing
435 * all stations at the same time if we have lots of associated
436 * stations that are idle (but keep re-associating).
437 */
438 int fuzz = os_random() % 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
440 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800441 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
442 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800443 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700444 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800445 /*
446 * The driver may not support this functionality.
447 * Anyway, try again after the next inactivity timeout,
448 * but do not disconnect the station now.
449 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700450 next_time = hapd->conf->ap_max_inactivity + fuzz;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800451 } else if (inactive_sec == -ENOENT) {
452 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
453 "Station " MACSTR " has lost its driver entry",
454 MAC2STR(sta->addr));
455
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800456 /* Avoid sending client probe on removed client */
457 sta->timeout_next = STA_DISASSOC;
458 goto skip_poll;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800459 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700460 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800461 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
462 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700463 MAC2STR(sta->addr), inactive_sec);
464 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700465 next_time = hapd->conf->ap_max_inactivity + fuzz -
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700466 inactive_sec;
467 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800468 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
469 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700470 "inactive too long: %d sec, max allowed: %d",
471 MAC2STR(sta->addr), inactive_sec,
472 hapd->conf->ap_max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800473
474 if (hapd->conf->skip_inactivity_poll)
475 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700476 }
477 }
478
479 if ((sta->flags & WLAN_STA_ASSOC) &&
480 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800481 !(sta->flags & WLAN_STA_PENDING_POLL) &&
482 !hapd->conf->skip_inactivity_poll) {
483 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
484 " has ACKed data poll", MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 /* data nullfunc frame poll did not produce TX errors; assume
486 * station ACKed it */
487 sta->timeout_next = STA_NULLFUNC;
488 next_time = hapd->conf->ap_max_inactivity;
489 }
490
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800491skip_poll:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700492 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
494 "for " MACSTR " (%lu seconds)",
495 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
497 sta);
498 return;
499 }
500
501 if (sta->timeout_next == STA_NULLFUNC &&
502 (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800503 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800505 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
506 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700507 } else if (sta->timeout_next != STA_REMOVE) {
508 int deauth = sta->timeout_next == STA_DEAUTH;
509
Hai Shalom74f70d42019-02-11 14:42:39 -0800510 if (!deauth && !(sta->flags & WLAN_STA_ASSOC)) {
511 /* Cannot disassociate not-associated STA, so move
512 * directly to deauthentication. */
513 sta->timeout_next = STA_DEAUTH;
514 deauth = 1;
515 }
516
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800517 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
518 "Timeout, sending %s info to STA " MACSTR,
519 deauth ? "deauthentication" : "disassociation",
520 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521
522 if (deauth) {
523 hostapd_drv_sta_deauth(
524 hapd, sta->addr,
525 WLAN_REASON_PREV_AUTH_NOT_VALID);
526 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700527 reason = (sta->timeout_next == STA_DISASSOC) ?
528 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
529 WLAN_REASON_PREV_AUTH_NOT_VALID;
530
531 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700532 }
533 }
534
535 switch (sta->timeout_next) {
536 case STA_NULLFUNC:
537 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700538 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
539 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
540 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700541 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
542 hapd, sta);
543 break;
544 case STA_DISASSOC:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700545 case STA_DISASSOC_FROM_CLI:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800546 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547 sta->flags &= ~WLAN_STA_ASSOC;
Hai Shalomfdcde762020-04-02 11:19:20 -0700548 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700549 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
550 if (!sta->acct_terminate_cause)
551 sta->acct_terminate_cause =
552 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
553 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800554 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700555 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
556 HOSTAPD_LEVEL_INFO, "disassociated due to "
557 "inactivity");
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700558 reason = (sta->timeout_next == STA_DISASSOC) ?
559 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
560 WLAN_REASON_PREV_AUTH_NOT_VALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700561 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700562 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
563 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
564 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700565 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
566 hapd, sta);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700567 mlme_disassociate_indication(hapd, sta, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700568 break;
569 case STA_DEAUTH:
570 case STA_REMOVE:
571 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
572 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 if (!sta->acct_terminate_cause)
575 sta->acct_terminate_cause =
576 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
577 mlme_deauthenticate_indication(
578 hapd, sta,
579 WLAN_REASON_PREV_AUTH_NOT_VALID);
580 ap_free_sta(hapd, sta);
581 break;
582 }
583}
584
585
586static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
587{
588 struct hostapd_data *hapd = eloop_ctx;
589 struct sta_info *sta = timeout_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700590
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800591 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
592 hapd->conf->iface, MAC2STR(sta->addr));
Hai Shalomfdcde762020-04-02 11:19:20 -0700593 if (!(sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC |
594 WLAN_STA_AUTHORIZED))) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700595 if (sta->flags & WLAN_STA_GAS) {
596 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
597 "entry " MACSTR, MAC2STR(sta->addr));
598 ap_free_sta(hapd, sta);
599 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700600 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700601 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700603 hostapd_drv_sta_deauth(hapd, sta->addr,
604 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700605 mlme_deauthenticate_indication(hapd, sta,
606 WLAN_REASON_PREV_AUTH_NOT_VALID);
607 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
608 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
609 "session timeout");
610 sta->acct_terminate_cause =
611 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 ap_free_sta(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700613}
614
615
Dmitry Shmidt54605472013-11-08 11:10:19 -0800616void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
617 u32 session_timeout)
618{
619 if (eloop_replenish_timeout(session_timeout, 0,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800620 ap_handle_session_timer, hapd, sta) == 1) {
Dmitry Shmidt54605472013-11-08 11:10:19 -0800621 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
622 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
623 "to %d seconds", session_timeout);
624 }
625}
626
627
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700628void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
629 u32 session_timeout)
630{
631 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
632 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
633 "seconds", session_timeout);
634 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
635 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
636 hapd, sta);
637}
638
639
640void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
641{
642 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
643}
644
645
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800646static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
647{
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700648#ifdef CONFIG_WNM_AP
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800649 struct hostapd_data *hapd = eloop_ctx;
650 struct sta_info *sta = timeout_ctx;
651
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800652 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
653 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800654 if (sta->hs20_session_info_url == NULL)
655 return;
656
657 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
658 sta->hs20_disassoc_timer);
Dmitry Shmidtd2986c22017-10-23 14:22:09 -0700659#endif /* CONFIG_WNM_AP */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800660}
661
662
663void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
664 struct sta_info *sta, int warning_time)
665{
666 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
667 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
668 hapd, sta);
669}
670
671
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700672struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
673{
674 struct sta_info *sta;
Hai Shalom81f62d82019-07-22 12:10:00 -0700675 int i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700676
677 sta = ap_get_sta(hapd, addr);
678 if (sta)
679 return sta;
680
681 wpa_printf(MSG_DEBUG, " New STA");
682 if (hapd->num_sta >= hapd->conf->max_num_sta) {
683 /* FIX: might try to remove some old STAs first? */
684 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
685 hapd->num_sta, hapd->conf->max_num_sta);
686 return NULL;
687 }
688
689 sta = os_zalloc(sizeof(struct sta_info));
690 if (sta == NULL) {
691 wpa_printf(MSG_ERROR, "malloc failed");
692 return NULL;
693 }
694 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
Dmitry Shmidtb97e4282016-02-08 10:16:07 -0800695 if (accounting_sta_get_id(hapd, sta) < 0) {
696 os_free(sta);
697 return NULL;
698 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700699
Hai Shalom81f62d82019-07-22 12:10:00 -0700700 for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
701 if (!hapd->iface->basic_rates)
702 break;
703 if (hapd->iface->basic_rates[i] < 0)
704 break;
705 sta->supported_rates[i] = hapd->iface->basic_rates[i] / 5;
706 }
707 sta->supported_rates_len = i;
708
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800709 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
710 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
711 "for " MACSTR " (%d seconds - ap_max_inactivity)",
712 __func__, MAC2STR(addr),
713 hapd->conf->ap_max_inactivity);
714 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
715 ap_handle_timer, hapd, sta);
716 }
717
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700718 /* initialize STA info data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700719 os_memcpy(sta->addr, addr, ETH_ALEN);
720 sta->next = hapd->sta_list;
721 hapd->sta_list = sta;
722 hapd->num_sta++;
723 ap_sta_hash_add(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700724 ap_sta_remove_in_other_bss(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800725 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
726 dl_list_init(&sta->ip6addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700727
Dmitry Shmidtaca489e2016-09-28 15:44:14 -0700728#ifdef CONFIG_TAXONOMY
729 sta_track_claim_taxonomy_info(hapd->iface, addr,
730 &sta->probe_ie_taxonomy);
731#endif /* CONFIG_TAXONOMY */
732
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700733 return sta;
734}
735
736
737static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
738{
739 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
740
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800741 if (sta->ipaddr)
742 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
743 ap_sta_ip6addr_del(hapd, sta);
744
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800745 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
746 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700747 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
748 sta->flags & WLAN_STA_ASSOC) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800749 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
750 " from kernel driver",
751 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 return -1;
753 }
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800754 sta->added_unassoc = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700755 return 0;
756}
757
758
759static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
760 struct sta_info *sta)
761{
762 struct hostapd_iface *iface = hapd->iface;
763 size_t i;
764
765 for (i = 0; i < iface->num_bss; i++) {
766 struct hostapd_data *bss = iface->bss[i];
767 struct sta_info *sta2;
768 /* bss should always be set during operation, but it may be
769 * NULL during reconfiguration. Assume the STA is not
770 * associated to another BSS in that case to avoid NULL pointer
771 * dereferences. */
772 if (bss == hapd || bss == NULL)
773 continue;
774 sta2 = ap_get_sta(bss, sta->addr);
775 if (!sta2)
776 continue;
777
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800778 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
779 " association from another BSS %s",
780 hapd->conf->iface, MAC2STR(sta2->addr),
781 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 ap_sta_disconnect(bss, sta2, sta2->addr,
783 WLAN_REASON_PREV_AUTH_NOT_VALID);
784 }
785}
786
787
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
789{
790 struct hostapd_data *hapd = eloop_ctx;
791 struct sta_info *sta = timeout_ctx;
792
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800793 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
794 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800795 ap_sta_remove(hapd, sta);
796 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
797}
798
799
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
801 u16 reason)
802{
803 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
804 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800805 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt29333592017-01-09 12:27:11 -0800806 if (hapd->iface->current_mode &&
807 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
808 /* Skip deauthentication in DMG/IEEE 802.11ad */
809 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
810 WLAN_STA_ASSOC_REQ_OK);
811 sta->timeout_next = STA_REMOVE;
812 } else {
813 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
814 sta->timeout_next = STA_DEAUTH;
815 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800816 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700817 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700818 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
819 "for " MACSTR " (%d seconds - "
820 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
821 __func__, MAC2STR(sta->addr),
822 AP_MAX_INACTIVITY_AFTER_DISASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700823 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
824 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
825 ap_handle_timer, hapd, sta);
826 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800827 ieee802_1x_free_station(hapd, sta);
Hai Shalom81f62d82019-07-22 12:10:00 -0700828 wpa_auth_sta_deinit(sta->wpa_sm);
829 sta->wpa_sm = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800831 sta->disassoc_reason = reason;
832 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
833 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
834 eloop_register_timeout(hapd->iface->drv_flags &
835 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
836 ap_sta_disassoc_cb_timeout, hapd, sta);
837}
838
839
840static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
841{
842 struct hostapd_data *hapd = eloop_ctx;
843 struct sta_info *sta = timeout_ctx;
844
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800845 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
846 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800847 ap_sta_remove(hapd, sta);
848 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700849}
850
851
852void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
853 u16 reason)
854{
Dmitry Shmidt29333592017-01-09 12:27:11 -0800855 if (hapd->iface->current_mode &&
856 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
857 /* Deauthentication is not used in DMG/IEEE 802.11ad;
858 * disassociate the STA instead. */
859 ap_sta_disassociate(hapd, sta, reason);
860 return;
861 }
862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
864 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800865 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
866 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800867 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -0700868 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700870 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
871 "for " MACSTR " (%d seconds - "
872 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
873 __func__, MAC2STR(sta->addr),
874 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700875 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
876 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
877 ap_handle_timer, hapd, sta);
878 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800879 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700880
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800881 sta->deauth_reason = reason;
882 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
883 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
884 eloop_register_timeout(hapd->iface->drv_flags &
885 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
886 ap_sta_deauth_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700887}
888
889
Dmitry Shmidt04949592012-07-19 12:16:46 -0700890#ifdef CONFIG_WPS
891int ap_sta_wps_cancel(struct hostapd_data *hapd,
892 struct sta_info *sta, void *ctx)
893{
894 if (sta && (sta->flags & WLAN_STA_WPS)) {
895 ap_sta_deauthenticate(hapd, sta,
896 WLAN_REASON_PREV_AUTH_NOT_VALID);
897 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
898 __func__, MAC2STR(sta->addr));
899 return 1;
900 }
901
902 return 0;
903}
904#endif /* CONFIG_WPS */
905
906
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800907static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
908{
909 struct hostapd_vlan *vlan;
910 int vlan_id = MAX_VLAN_ID + 2;
911
912retry:
913 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
914 if (vlan->vlan_id == vlan_id) {
915 vlan_id++;
916 goto retry;
917 }
918 }
919 return vlan_id;
920}
921
922
923int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
924 struct vlan_description *vlan_desc)
925{
926 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
927 int old_vlan_id, vlan_id = 0, ret = 0;
928
Dmitry Shmidt57c2d392016-02-23 13:40:19 -0800929 /* Check if there is something to do */
930 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
931 /* This sta is lacking its own vif */
932 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
933 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
934 /* sta->vlan_id needs to be reset */
935 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
936 return 0; /* nothing to change */
937 }
938
939 /* Now the real VLAN changed or the STA just needs its own vif */
940 if (hapd->conf->ssid.per_sta_vif) {
941 /* Assign a new vif, always */
942 /* find a free vlan_id sufficiently big */
943 vlan_id = ap_sta_get_free_vlan_id(hapd);
944 /* Get wildcard VLAN */
945 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
946 if (vlan->vlan_id == VLAN_ID_WILDCARD)
947 break;
948 }
949 if (!vlan) {
950 hostapd_logger(hapd, sta->addr,
951 HOSTAPD_MODULE_IEEE80211,
952 HOSTAPD_LEVEL_DEBUG,
953 "per_sta_vif missing wildcard");
954 vlan_id = 0;
955 ret = -1;
956 goto done;
957 }
958 } else if (vlan_desc && vlan_desc->notempty) {
959 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
960 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
961 break;
962 if (vlan->vlan_id == VLAN_ID_WILDCARD)
963 wildcard_vlan = vlan;
964 }
965 if (vlan) {
966 vlan_id = vlan->vlan_id;
967 } else if (wildcard_vlan) {
968 vlan = wildcard_vlan;
969 vlan_id = vlan_desc->untagged;
970 if (vlan_desc->tagged[0]) {
971 /* Tagged VLAN configuration */
972 vlan_id = ap_sta_get_free_vlan_id(hapd);
973 }
974 } else {
975 hostapd_logger(hapd, sta->addr,
976 HOSTAPD_MODULE_IEEE80211,
977 HOSTAPD_LEVEL_DEBUG,
978 "missing vlan and wildcard for vlan=%d%s",
979 vlan_desc->untagged,
980 vlan_desc->tagged[0] ? "+" : "");
981 vlan_id = 0;
982 ret = -1;
983 goto done;
984 }
985 }
986
987 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
988 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
989 if (vlan == NULL) {
990 hostapd_logger(hapd, sta->addr,
991 HOSTAPD_MODULE_IEEE80211,
992 HOSTAPD_LEVEL_DEBUG,
993 "could not add dynamic VLAN interface for vlan=%d%s",
994 vlan_desc ? vlan_desc->untagged : -1,
995 (vlan_desc && vlan_desc->tagged[0]) ?
996 "+" : "");
997 vlan_id = 0;
998 ret = -1;
999 goto done;
1000 }
1001
1002 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1003 HOSTAPD_LEVEL_DEBUG,
1004 "added new dynamic VLAN interface '%s'",
1005 vlan->ifname);
1006 } else if (vlan && vlan->dynamic_vlan > 0) {
1007 vlan->dynamic_vlan++;
1008 hostapd_logger(hapd, sta->addr,
1009 HOSTAPD_MODULE_IEEE80211,
1010 HOSTAPD_LEVEL_DEBUG,
1011 "updated existing dynamic VLAN interface '%s'",
1012 vlan->ifname);
1013 }
1014done:
1015 old_vlan_id = sta->vlan_id;
1016 sta->vlan_id = vlan_id;
1017 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
1018
1019 if (vlan_id != old_vlan_id && old_vlan_id)
1020 vlan_remove_dynamic(hapd, old_vlan_id);
1021
1022 return ret;
1023}
1024
1025
Dmitry Shmidt83474442015-04-15 13:47:09 -07001026int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001027{
1028#ifndef CONFIG_NO_VLAN
1029 const char *iface;
1030 struct hostapd_vlan *vlan = NULL;
1031 int ret;
Dmitry Shmidt83474442015-04-15 13:47:09 -07001032 int old_vlanid = sta->vlan_id_bound;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033
Hai Shalomfdcde762020-04-02 11:19:20 -07001034 if ((sta->flags & WLAN_STA_WDS) && sta->vlan_id == 0) {
1035 wpa_printf(MSG_DEBUG,
1036 "Do not override WDS VLAN assignment for STA "
1037 MACSTR, MAC2STR(sta->addr));
1038 return 0;
1039 }
1040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001041 iface = hapd->conf->iface;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -07001042 if (hapd->conf->ssid.vlan[0])
1043 iface = hapd->conf->ssid.vlan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001044
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001045 if (sta->vlan_id > 0) {
1046 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001047 if (vlan->vlan_id == sta->vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001048 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001049 }
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -07001050 if (vlan)
1051 iface = vlan->ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001052 }
1053
Dmitry Shmidt83474442015-04-15 13:47:09 -07001054 /*
1055 * Do not increment ref counters if the VLAN ID remains same, but do
1056 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1057 * have been called before.
1058 */
1059 if (sta->vlan_id == old_vlanid)
1060 goto skip_counting;
1061
Hai Shalomfdcde762020-04-02 11:19:20 -07001062 if (sta->vlan_id > 0 && !vlan &&
1063 !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001064 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1065 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1066 "binding station to (vlan_id=%d)",
1067 sta->vlan_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001068 ret = -1;
1069 goto done;
Dmitry Shmidt57c2d392016-02-23 13:40:19 -08001070 } else if (vlan && vlan->dynamic_vlan > 0) {
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001071 vlan->dynamic_vlan++;
1072 hostapd_logger(hapd, sta->addr,
1073 HOSTAPD_MODULE_IEEE80211,
1074 HOSTAPD_LEVEL_DEBUG,
1075 "updated existing dynamic VLAN interface '%s'",
1076 iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001077 }
1078
Dmitry Shmidt83474442015-04-15 13:47:09 -07001079 /* ref counters have been increased, so mark the station */
1080 sta->vlan_id_bound = sta->vlan_id;
1081
1082skip_counting:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001083 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1084 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1085 "'%s'", iface);
1086
1087 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1088 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1089
1090 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
1091 if (ret < 0) {
1092 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1093 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1094 "entry to vlan_id=%d", sta->vlan_id);
1095 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001096
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001097 /* During 1x reauth, if the vlan id changes, then remove the old id. */
Dmitry Shmidt83474442015-04-15 13:47:09 -07001098 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001099 vlan_remove_dynamic(hapd, old_vlanid);
Dmitry Shmidt83474442015-04-15 13:47:09 -07001100done:
Dmitry Shmidt216983b2015-02-06 10:50:36 -08001101
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102 return ret;
1103#else /* CONFIG_NO_VLAN */
1104 return 0;
1105#endif /* CONFIG_NO_VLAN */
1106}
1107
1108
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1110{
1111 u32 tu;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001112 struct os_reltime now, passed;
1113 os_get_reltime(&now);
1114 os_reltime_sub(&now, &sta->sa_query_start, &passed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001115 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1116 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1117 hostapd_logger(hapd, sta->addr,
1118 HOSTAPD_MODULE_IEEE80211,
1119 HOSTAPD_LEVEL_DEBUG,
1120 "association SA Query timed out");
1121 sta->sa_query_timed_out = 1;
1122 os_free(sta->sa_query_trans_id);
1123 sta->sa_query_trans_id = NULL;
1124 sta->sa_query_count = 0;
1125 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1126 return 1;
1127 }
1128
1129 return 0;
1130}
1131
1132
1133static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1134{
1135 struct hostapd_data *hapd = eloop_ctx;
1136 struct sta_info *sta = timeout_ctx;
1137 unsigned int timeout, sec, usec;
1138 u8 *trans_id, *nbuf;
1139
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001140 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1141 " (count=%d)",
1142 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1143
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 if (sta->sa_query_count > 0 &&
1145 ap_check_sa_query_timeout(hapd, sta))
1146 return;
Hai Shalomfdcde762020-04-02 11:19:20 -07001147 if (sta->sa_query_count >= 1000)
1148 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001150 nbuf = os_realloc_array(sta->sa_query_trans_id,
1151 sta->sa_query_count + 1,
1152 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 if (nbuf == NULL)
1154 return;
1155 if (sta->sa_query_count == 0) {
1156 /* Starting a new SA Query procedure */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001157 os_get_reltime(&sta->sa_query_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001158 }
1159 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1160 sta->sa_query_trans_id = nbuf;
1161 sta->sa_query_count++;
1162
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001163 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1164 /*
1165 * We don't really care which ID is used here, so simply
1166 * hardcode this if the mostly theoretical os_get_random()
1167 * failure happens.
1168 */
1169 trans_id[0] = 0x12;
1170 trans_id[1] = 0x34;
1171 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001172
1173 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1174 sec = ((timeout / 1000) * 1024) / 1000;
1175 usec = (timeout % 1000) * 1024;
1176 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1177
1178 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1179 HOSTAPD_LEVEL_DEBUG,
1180 "association SA Query attempt %d", sta->sa_query_count);
1181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001183}
1184
1185
1186void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1187{
1188 ap_sa_query_timer(hapd, sta);
1189}
1190
1191
1192void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1193{
1194 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1195 os_free(sta->sa_query_trans_id);
1196 sta->sa_query_trans_id = NULL;
1197 sta->sa_query_count = 0;
1198}
1199
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001200
Hai Shalom74f70d42019-02-11 14:42:39 -08001201const char * ap_sta_wpa_get_keyid(struct hostapd_data *hapd,
1202 struct sta_info *sta)
1203{
1204 struct hostapd_wpa_psk *psk;
1205 struct hostapd_ssid *ssid;
1206 const u8 *pmk;
1207 int pmk_len;
1208
1209 ssid = &hapd->conf->ssid;
1210
1211 pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1212 if (!pmk || pmk_len != PMK_LEN)
1213 return NULL;
1214
1215 for (psk = ssid->wpa_psk; psk; psk = psk->next)
1216 if (os_memcmp(pmk, psk->psk, PMK_LEN) == 0)
1217 break;
1218 if (!psk)
1219 return NULL;
1220 if (!psk || !psk->keyid[0])
1221 return NULL;
1222
1223 return psk->keyid;
1224}
1225
1226
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001227void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1228 int authorized)
1229{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001230 const u8 *dev_addr = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001231 char buf[100];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001232#ifdef CONFIG_P2P
1233 u8 addr[ETH_ALEN];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001234 u8 ip_addr_buf[4];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001235#endif /* CONFIG_P2P */
1236
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001237 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1238 return;
1239
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001240 if (authorized)
1241 sta->flags |= WLAN_STA_AUTHORIZED;
1242 else
1243 sta->flags &= ~WLAN_STA_AUTHORIZED;
1244
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001245#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001246 if (hapd->p2p_group == NULL) {
1247 if (sta->p2p_ie != NULL &&
1248 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1249 dev_addr = addr;
1250 } else
1251 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001252
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001253 if (dev_addr)
1254 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1255 MAC2STR(sta->addr), MAC2STR(dev_addr));
1256 else
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001257#endif /* CONFIG_P2P */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001258 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1259
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001260 if (hapd->sta_authorized_cb)
1261 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1262 sta->addr, authorized, dev_addr);
1263
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001264 if (authorized) {
Hai Shalom74f70d42019-02-11 14:42:39 -08001265 const char *keyid;
1266 char keyid_buf[100];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001267 char ip_addr[100];
Hai Shalom74f70d42019-02-11 14:42:39 -08001268
1269 keyid_buf[0] = '\0';
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001270 ip_addr[0] = '\0';
1271#ifdef CONFIG_P2P
1272 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1273 os_snprintf(ip_addr, sizeof(ip_addr),
1274 " ip_addr=%u.%u.%u.%u",
1275 ip_addr_buf[0], ip_addr_buf[1],
1276 ip_addr_buf[2], ip_addr_buf[3]);
1277 }
1278#endif /* CONFIG_P2P */
1279
Hai Shalom74f70d42019-02-11 14:42:39 -08001280 keyid = ap_sta_wpa_get_keyid(hapd, sta);
1281 if (keyid) {
1282 os_snprintf(keyid_buf, sizeof(keyid_buf),
1283 " keyid=%s", keyid);
1284 }
1285
1286 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s",
1287 buf, ip_addr, keyid_buf);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001288
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001289 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001290 hapd->msg_ctx_parent != hapd->msg_ctx)
1291 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
Hai Shalom74f70d42019-02-11 14:42:39 -08001292 AP_STA_CONNECTED "%s%s%s",
1293 buf, ip_addr, keyid_buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001294 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001295 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1296
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001297 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001298 hapd->msg_ctx_parent != hapd->msg_ctx)
1299 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1300 AP_STA_DISCONNECTED "%s", buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001301 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001302
1303#ifdef CONFIG_FST
1304 if (hapd->iface->fst) {
1305 if (authorized)
1306 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1307 else
1308 fst_notify_peer_disconnected(hapd->iface->fst,
1309 sta->addr);
1310 }
1311#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001312}
1313
1314
1315void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1316 const u8 *addr, u16 reason)
1317{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001318 if (sta)
1319 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1320 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1321 reason);
1322 else if (addr)
1323 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1324 hapd->conf->iface, __func__, MAC2STR(addr),
1325 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001326
1327 if (sta == NULL && addr)
1328 sta = ap_get_sta(hapd, addr);
1329
1330 if (addr)
1331 hostapd_drv_sta_deauth(hapd, addr, reason);
1332
1333 if (sta == NULL)
1334 return;
1335 ap_sta_set_authorized(hapd, sta, 0);
Hai Shalomfdcde762020-04-02 11:19:20 -07001336 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1337 hostapd_set_sta_flags(hapd, sta);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001338 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1339 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001340 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
Dmitry Shmidt04949592012-07-19 12:16:46 -07001341 "for " MACSTR " (%d seconds - "
1342 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001343 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001344 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001346 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1347 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001348 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349
Dmitry Shmidt29333592017-01-09 12:27:11 -08001350 if (hapd->iface->current_mode &&
1351 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1352 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1353 * disassociate the STA instead. */
1354 sta->disassoc_reason = reason;
1355 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1356 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1357 eloop_register_timeout(hapd->iface->drv_flags &
1358 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ?
1359 2 : 0, 0, ap_sta_disassoc_cb_timeout,
1360 hapd, sta);
1361 return;
1362 }
1363
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001364 sta->deauth_reason = reason;
1365 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1366 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1367 eloop_register_timeout(hapd->iface->drv_flags &
1368 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1369 ap_sta_deauth_cb_timeout, hapd, sta);
1370}
1371
1372
1373void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1374{
1375 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1376 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1377 return;
1378 }
1379 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1380 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1381 ap_sta_deauth_cb_timeout(hapd, sta);
1382}
1383
1384
1385void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1386{
1387 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1388 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1389 return;
1390 }
1391 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1392 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1393 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001394}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001395
1396
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001397void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1398 struct sta_info *sta)
1399{
1400 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1401 wpa_printf(MSG_DEBUG,
1402 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1403 MACSTR,
1404 hapd->conf->iface, MAC2STR(sta->addr));
1405 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1406 wpa_printf(MSG_DEBUG,
1407 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1408 MACSTR,
1409 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt29333592017-01-09 12:27:11 -08001410 if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1411 {
1412 wpa_printf(MSG_DEBUG,
1413 "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1414 MACSTR,
1415 hapd->conf->iface, MAC2STR(sta->addr));
1416 if (sta->flags & WLAN_STA_WPS)
1417 hostapd_wps_eap_completed(hapd);
1418 }
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001419}
1420
1421
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001422int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1423{
1424 int res;
1425
1426 buf[0] = '\0';
Hai Shalomc3565922019-10-28 11:58:20 -07001427 res = os_snprintf(buf, buflen, "%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 -08001428 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1429 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1430 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1431 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1432 ""),
1433 (flags & WLAN_STA_SHORT_PREAMBLE ?
1434 "[SHORT_PREAMBLE]" : ""),
1435 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1436 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1437 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1438 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1439 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1440 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1441 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1442 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1443 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
Roshan Pius3a1667e2018-07-03 15:17:14 -07001444 (flags & WLAN_STA_HT ? "[HT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001445 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
Hai Shalomc3565922019-10-28 11:58:20 -07001446 (flags & WLAN_STA_HE ? "[HE]" : ""),
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001447 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001448 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1449 "[WNM_SLEEP_MODE]" : ""));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001450 if (os_snprintf_error(buflen, res))
1451 res = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001452
1453 return res;
1454}
Dmitry Shmidt29333592017-01-09 12:27:11 -08001455
1456
1457static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1458{
1459 struct hostapd_data *hapd = eloop_ctx;
1460 struct sta_info *sta = timeout_ctx;
Roshan Pius3a1667e2018-07-03 15:17:14 -07001461 u16 reason;
Dmitry Shmidt29333592017-01-09 12:27:11 -08001462
1463 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1464 "IEEE 802.1X: Scheduled disconnection of " MACSTR
1465 " after EAP-Failure", MAC2STR(sta->addr));
1466
Roshan Pius3a1667e2018-07-03 15:17:14 -07001467 reason = sta->disconnect_reason_code;
1468 if (!reason)
1469 reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1470 ap_sta_disconnect(hapd, sta, sta->addr, reason);
Dmitry Shmidt29333592017-01-09 12:27:11 -08001471 if (sta->flags & WLAN_STA_WPS)
1472 hostapd_wps_eap_completed(hapd);
1473}
1474
1475
1476void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1477 struct sta_info *sta)
1478{
1479 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1480 "IEEE 802.1X: Force disconnection of " MACSTR
1481 " after EAP-Failure in 10 ms", MAC2STR(sta->addr));
1482
1483 /*
1484 * Add a small sleep to increase likelihood of previously requested
1485 * EAP-Failure TX getting out before this should the driver reorder
1486 * operations.
1487 */
1488 eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
1489 eloop_register_timeout(0, 10000, ap_sta_delayed_1x_auth_fail_cb,
1490 hapd, sta);
1491}
1492
1493
1494int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1495 struct sta_info *sta)
1496{
1497 return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1498 hapd, sta);
1499}
Hai Shalomb755a2a2020-04-23 21:49:02 -07001500
1501
1502int ap_sta_re_add(struct hostapd_data *hapd, struct sta_info *sta)
1503{
1504 /*
1505 * If a station that is already associated to the AP, is trying to
1506 * authenticate again, remove the STA entry, in order to make sure the
1507 * STA PS state gets cleared and configuration gets updated. To handle
1508 * this, station's added_unassoc flag is cleared once the station has
1509 * completed association.
1510 */
1511 ap_sta_set_authorized(hapd, sta, 0);
1512 hostapd_drv_sta_remove(hapd, sta->addr);
1513 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH | WLAN_STA_AUTHORIZED);
1514
1515 if (hostapd_sta_add(hapd, sta->addr, 0, 0,
1516 sta->supported_rates,
1517 sta->supported_rates_len,
1518 0, NULL, NULL, NULL, 0,
1519 sta->flags, 0, 0, 0, 0)) {
1520 hostapd_logger(hapd, sta->addr,
1521 HOSTAPD_MODULE_IEEE80211,
1522 HOSTAPD_LEVEL_NOTICE,
1523 "Could not add STA to kernel driver");
1524 return -1;
1525 }
1526
1527 sta->added_unassoc = 1;
1528 return 0;
1529}