]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/sta_info.c
hostapd: Add Min/Max Transmit Power Capability into STA command
[thirdparty/hostap.git] / src / ap / sta_info.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / Station table
09368515 3 * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
6226e38d 9#include "utils/includes.h"
6fc6879b 10
6226e38d
JM
11#include "utils/common.h"
12#include "utils/eloop.h"
81f4f619 13#include "common/ieee802_11_defs.h"
6caaae1e 14#include "common/wpa_ctrl.h"
a46d72d7 15#include "common/sae.h"
bdee6fce
JM
16#include "radius/radius.h"
17#include "radius/radius_client.h"
8ccbe415 18#include "p2p/p2p.h"
6959145b 19#include "fst/fst.h"
09368515 20#include "crypto/crypto.h"
6fc6879b 21#include "hostapd.h"
6fc6879b
JM
22#include "accounting.h"
23#include "ieee802_1x.h"
24#include "ieee802_11.h"
f2a14be7 25#include "ieee802_11_auth.h"
6226e38d
JM
26#include "wpa_auth.h"
27#include "preauth_auth.h"
28#include "ap_config.h"
6fc6879b 29#include "beacon.h"
6226e38d 30#include "ap_mlme.h"
6fc6879b 31#include "vlan_init.h"
aefb53bd 32#include "p2p_hostapd.h"
cee7d66b 33#include "ap_drv_ops.h"
dca30c3f 34#include "gas_serv.h"
97596f8e 35#include "wnm_ap.h"
ca911d61 36#include "mbo_ap.h"
bd00c431 37#include "ndisc_snoop.h"
6226e38d 38#include "sta_info.h"
1889af2e 39#include "vlan.h"
0f5eb69f 40#include "wps_hostapd.h"
6fc6879b 41
53f3d6f3
FF
42static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
43 struct sta_info *sta);
6fc6879b 44static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
97596f8e 45static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
4dc03726
JM
46static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
47static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 48#ifdef CONFIG_IEEE80211W
93b76319 49static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 50#endif /* CONFIG_IEEE80211W */
4dc03726 51static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
0f5eb69f 52static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx);
6fc6879b
JM
53
54int ap_for_each_sta(struct hostapd_data *hapd,
55 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
56 void *ctx),
57 void *ctx)
58{
59 struct sta_info *sta;
60
61 for (sta = hapd->sta_list; sta; sta = sta->next) {
62 if (cb(hapd, sta, ctx))
63 return 1;
64 }
65
66 return 0;
67}
68
69
70struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
71{
72 struct sta_info *s;
73
74 s = hapd->sta_hash[STA_HASH(sta)];
75 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
76 s = s->hnext;
77 return s;
78}
79
80
f2c56602
JM
81#ifdef CONFIG_P2P
82struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
83{
84 struct sta_info *sta;
85
86 for (sta = hapd->sta_list; sta; sta = sta->next) {
87 const u8 *p2p_dev_addr;
88
89 if (sta->p2p_ie == NULL)
90 continue;
91
92 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
93 if (p2p_dev_addr == NULL)
94 continue;
95
96 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
97 return sta;
98 }
99
100 return NULL;
101}
102#endif /* CONFIG_P2P */
103
104
6fc6879b
JM
105static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
106{
107 struct sta_info *tmp;
108
109 if (hapd->sta_list == sta) {
110 hapd->sta_list = sta->next;
111 return;
112 }
113
114 tmp = hapd->sta_list;
115 while (tmp != NULL && tmp->next != sta)
116 tmp = tmp->next;
117 if (tmp == NULL) {
118 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
119 "list.", MAC2STR(sta->addr));
120 } else
121 tmp->next = sta->next;
122}
123
124
125void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
126{
127 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
128 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
129}
130
131
132static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
133{
134 struct sta_info *s;
135
136 s = hapd->sta_hash[STA_HASH(sta->addr)];
137 if (s == NULL) return;
138 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
139 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
140 return;
141 }
142
143 while (s->hnext != NULL &&
144 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
145 s = s->hnext;
146 if (s->hnext != NULL)
147 s->hnext = s->hnext->hnext;
148 else
149 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
150 " from hash table", MAC2STR(sta->addr));
151}
152
153
bd00c431
KP
154void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
155{
156 sta_ip6addr_del(hapd, sta);
157}
158
159
6fc6879b
JM
160void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
161{
162 int set_beacon = 0;
163
164 accounting_sta_stop(hapd, sta);
165
6905dcb1
JB
166 /* just in case */
167 ap_sta_set_authorized(hapd, sta, 0);
168
39f42d11 169 if (sta->flags & WLAN_STA_WDS)
69dd2967 170 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
53f3d6f3 171
7d597d46 172 if (sta->ipaddr)
ed4ddb6d 173 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
bd00c431 174 ap_sta_ip6addr_del(hapd, sta);
7d597d46 175
354c903f 176 if (!hapd->iface->driver_ap_teardown &&
bb598c3b 177 !(sta->flags & WLAN_STA_PREAUTH)) {
51e2a27a 178 hostapd_drv_sta_remove(hapd, sta->addr);
bb598c3b
AB
179 sta->added_unassoc = 0;
180 }
6fc6879b
JM
181
182 ap_sta_hash_del(hapd, sta);
183 ap_sta_list_del(hapd, sta);
184
185 if (sta->aid > 0)
2991469c
JM
186 hapd->sta_aid[(sta->aid - 1) / 32] &=
187 ~BIT((sta->aid - 1) % 32);
6fc6879b
JM
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--;
28d12641
AKP
200 if (hapd->iface->current_mode &&
201 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
6fc6879b
JM
202 && 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--;
28d12641
AKP
209 if (hapd->iface->current_mode &&
210 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
6fc6879b
JM
211 && hapd->iface->num_sta_no_short_preamble == 0)
212 set_beacon++;
213 }
214
e8ff1e59
JM
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;
de9289c8 222 hapd->iface->num_sta_no_ht--;
e8ff1e59
JM
223 }
224
225 if (sta->ht_20mhz_set) {
226 sta->ht_20mhz_set = 0;
227 hapd->iface->num_sta_ht_20mhz--;
228 }
de9289c8 229
04059ab8
DG
230#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
9c47f6a2
PX
237#ifdef CONFIG_IEEE80211N
238 ht40_intolerant_remove(hapd->iface, sta);
239#endif /* CONFIG_IEEE80211N */
240
aefb53bd
JM
241#ifdef CONFIG_P2P
242 if (sta->no_p2p_set) {
243 sta->no_p2p_set = 0;
244 hapd->num_sta_no_p2p--;
245 if (hapd->num_sta_no_p2p == 0)
246 hostapd_p2p_non_p2p_sta_disconnected(hapd);
247 }
248#endif /* CONFIG_P2P */
249
d45354be 250#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
de9289c8
JM
251 if (hostapd_ht_operation_update(hapd->iface) > 0)
252 set_beacon++;
d45354be 253#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
de9289c8 254
c596f3f0
CYY
255#ifdef CONFIG_MESH
256 if (hapd->mesh_sta_free_cb)
9684c756 257 hapd->mesh_sta_free_cb(hapd, sta);
c596f3f0
CYY
258#endif /* CONFIG_MESH */
259
6fc6879b
JM
260 if (set_beacon)
261 ieee802_11_set_beacons(hapd->iface);
262
42ca9845
JM
263 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
264 __func__, MAC2STR(sta->addr));
6fc6879b
JM
265 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
266 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
97596f8e 267 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
9e8fde21 268 ap_sta_clear_disconnect_timeouts(hapd, sta);
f3b8ad4d 269 sae_clear_retransmit_timer(hapd, sta);
6fc6879b 270
d7c3347f 271 ieee802_1x_free_station(hapd, sta);
6fc6879b
JM
272 wpa_auth_sta_deinit(sta->wpa_sm);
273 rsn_preauth_free_station(hapd, sta);
74784010 274#ifndef CONFIG_NO_RADIUS
ded22b53
HS
275 if (hapd->radius)
276 radius_client_flush_auth(hapd->radius, sta->addr);
74784010 277#endif /* CONFIG_NO_RADIUS */
6fc6879b 278
7cebc8e2
MB
279#ifndef CONFIG_NO_VLAN
280 /*
281 * sta->wpa_sm->group needs to be released before so that
282 * vlan_remove_dynamic() can check that no stations are left on the
283 * AP_VLAN netdev.
284 */
1889af2e
MB
285 if (sta->vlan_id)
286 vlan_remove_dynamic(hapd, sta->vlan_id);
7cebc8e2
MB
287 if (sta->vlan_id_bound) {
288 /*
289 * Need to remove the STA entry before potentially removing the
290 * VLAN.
291 */
292 if (hapd->iface->driver_ap_teardown &&
bb598c3b 293 !(sta->flags & WLAN_STA_PREAUTH)) {
7cebc8e2 294 hostapd_drv_sta_remove(hapd, sta->addr);
bb598c3b
AB
295 sta->added_unassoc = 0;
296 }
7cebc8e2
MB
297 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
298 }
299#endif /* CONFIG_NO_VLAN */
300
6fc6879b 301 os_free(sta->challenge);
5d22a1d5
JM
302
303#ifdef CONFIG_IEEE80211W
93b76319
JM
304 os_free(sta->sa_query_trans_id);
305 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
5d22a1d5
JM
306#endif /* CONFIG_IEEE80211W */
307
8ccbe415
JM
308#ifdef CONFIG_P2P
309 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
310#endif /* CONFIG_P2P */
311
dca30c3f
JK
312#ifdef CONFIG_INTERWORKING
313 if (sta->gas_dialog) {
314 int i;
315 for (i = 0; i < GAS_DIALOG_MAX; i++)
316 gas_serv_dialog_clear(&sta->gas_dialog[i]);
317 os_free(sta->gas_dialog);
318 }
319#endif /* CONFIG_INTERWORKING */
320
eb76b7e3 321 wpabuf_free(sta->wps_ie);
b305c684 322 wpabuf_free(sta->p2p_ie);
f403dcd6 323 wpabuf_free(sta->hs20_ie);
ae667c08
AN
324#ifdef CONFIG_FST
325 wpabuf_free(sta->mb_ies);
326#endif /* CONFIG_FST */
eb76b7e3 327
df84268a 328 os_free(sta->ht_capabilities);
cc14091e 329 os_free(sta->vht_capabilities);
f2a14be7 330 hostapd_free_psk_list(sta->psk);
2092597f
MB
331 os_free(sta->identity);
332 os_free(sta->radius_cui);
6ca0853d 333 os_free(sta->remediation_url);
8e1146d9 334 wpabuf_free(sta->hs20_deauth_req);
97596f8e 335 os_free(sta->hs20_session_info_url);
df84268a 336
98efcc41 337#ifdef CONFIG_SAE
a46d72d7 338 sae_clear_data(sta->sae);
98efcc41
JM
339 os_free(sta->sae);
340#endif /* CONFIG_SAE */
341
ca911d61 342 mbo_ap_sta_free(sta);
adf0478e 343 os_free(sta->supp_op_classes);
ca911d61 344
91d91abf
JM
345#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);
1764559e
JM
350#ifdef CONFIG_FILS_SK_PFS
351 crypto_ecdh_deinit(sta->fils_ecdh);
352 wpabuf_clear_free(sta->fils_dh_ss);
80ddf5d9 353 wpabuf_free(sta->fils_g_sta);
1764559e 354#endif /* CONFIG_FILS_SK_PFS */
91d91abf
JM
355#endif /* CONFIG_FILS */
356
09368515 357#ifdef CONFIG_OWE
7a12edd1 358 bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
09368515
JM
359 crypto_ecdh_deinit(sta->owe_ecdh);
360#endif /* CONFIG_OWE */
361
6fc6879b
JM
362 os_free(sta);
363}
364
365
366void hostapd_free_stas(struct hostapd_data *hapd)
367{
368 struct sta_info *sta, *prev;
369
370 sta = hapd->sta_list;
371
372 while (sta) {
373 prev = sta;
374 if (sta->flags & WLAN_STA_AUTH) {
375 mlme_deauthenticate_indication(
376 hapd, sta, WLAN_REASON_UNSPECIFIED);
377 }
378 sta = sta->next;
379 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
380 MAC2STR(prev->addr));
381 ap_free_sta(hapd, prev);
382 }
383}
384
385
1c6e69cc
JM
386/**
387 * ap_handle_timer - Per STA timer handler
388 * @eloop_ctx: struct hostapd_data *
389 * @timeout_ctx: struct sta_info *
390 *
391 * This function is called to check station activity and to remove inactive
392 * stations.
393 */
6fc6879b
JM
394void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
395{
396 struct hostapd_data *hapd = eloop_ctx;
397 struct sta_info *sta = timeout_ctx;
398 unsigned long next_time = 0;
d5b559b6 399 int reason;
6fc6879b 400
03269d55
JM
401 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
402 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
42ca9845 403 sta->timeout_next);
6fc6879b
JM
404 if (sta->timeout_next == STA_REMOVE) {
405 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
406 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
407 "local deauth request");
408 ap_free_sta(hapd, sta);
409 return;
410 }
411
412 if ((sta->flags & WLAN_STA_ASSOC) &&
413 (sta->timeout_next == STA_NULLFUNC ||
414 sta->timeout_next == STA_DISASSOC)) {
415 int inactive_sec;
ce28e279
BG
416 /*
417 * Add random value to timeout so that we don't end up bouncing
418 * all stations at the same time if we have lots of associated
419 * stations that are idle (but keep re-associating).
420 */
421 int fuzz = os_random() % 20;
51e2a27a 422 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
6fc6879b 423 if (inactive_sec == -1) {
3ec1e902
JM
424 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
425 "Check inactivity: Could not "
d5674791 426 "get station info from kernel driver for "
24d75245 427 MACSTR, MAC2STR(sta->addr));
d5674791
JM
428 /*
429 * The driver may not support this functionality.
430 * Anyway, try again after the next inactivity timeout,
431 * but do not disconnect the station now.
432 */
ce28e279 433 next_time = hapd->conf->ap_max_inactivity + fuzz;
b9749bac
MH
434 } else if (inactive_sec == -ENOENT) {
435 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
436 "Station " MACSTR " has lost its driver entry",
437 MAC2STR(sta->addr));
438
47e5fbde
PO
439 /* Avoid sending client probe on removed client */
440 sta->timeout_next = STA_DISASSOC;
441 goto skip_poll;
a114c723 442 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
6fc6879b 443 /* station activity detected; reset timeout state */
3ec1e902
JM
444 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
445 "Station " MACSTR " has been active %is ago",
24d75245 446 MAC2STR(sta->addr), inactive_sec);
6fc6879b 447 sta->timeout_next = STA_NULLFUNC;
ce28e279 448 next_time = hapd->conf->ap_max_inactivity + fuzz -
6fc6879b 449 inactive_sec;
24d75245 450 } else {
3ec1e902
JM
451 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
452 "Station " MACSTR " has been "
24d75245
BG
453 "inactive too long: %d sec, max allowed: %d",
454 MAC2STR(sta->addr), inactive_sec,
455 hapd->conf->ap_max_inactivity);
ef01fa7b
YAP
456
457 if (hapd->conf->skip_inactivity_poll)
458 sta->timeout_next = STA_DISASSOC;
6fc6879b
JM
459 }
460 }
461
462 if ((sta->flags & WLAN_STA_ASSOC) &&
463 sta->timeout_next == STA_DISASSOC &&
ef01fa7b
YAP
464 !(sta->flags & WLAN_STA_PENDING_POLL) &&
465 !hapd->conf->skip_inactivity_poll) {
3ec1e902
JM
466 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
467 " has ACKed data poll", MAC2STR(sta->addr));
6fc6879b
JM
468 /* data nullfunc frame poll did not produce TX errors; assume
469 * station ACKed it */
470 sta->timeout_next = STA_NULLFUNC;
471 next_time = hapd->conf->ap_max_inactivity;
472 }
473
47e5fbde 474skip_poll:
6fc6879b 475 if (next_time) {
42ca9845
JM
476 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
477 "for " MACSTR " (%lu seconds)",
478 __func__, MAC2STR(sta->addr), next_time);
6fc6879b
JM
479 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
480 sta);
481 return;
482 }
483
484 if (sta->timeout_next == STA_NULLFUNC &&
485 (sta->flags & WLAN_STA_ASSOC)) {
bcf24348 486 wpa_printf(MSG_DEBUG, " Polling STA");
6fc6879b 487 sta->flags |= WLAN_STA_PENDING_POLL;
bcf24348
JB
488 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
489 sta->flags & WLAN_STA_WMM);
6fc6879b
JM
490 } else if (sta->timeout_next != STA_REMOVE) {
491 int deauth = sta->timeout_next == STA_DEAUTH;
492
afcc9ea1
BG
493 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
494 "Timeout, sending %s info to STA " MACSTR,
495 deauth ? "deauthentication" : "disassociation",
496 MAC2STR(sta->addr));
6fc6879b
JM
497
498 if (deauth) {
51e2a27a
JM
499 hostapd_drv_sta_deauth(
500 hapd, sta->addr,
501 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b 502 } else {
d5b559b6
KP
503 reason = (sta->timeout_next == STA_DISASSOC) ?
504 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
505 WLAN_REASON_PREV_AUTH_NOT_VALID;
506
507 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
6fc6879b
JM
508 }
509 }
510
511 switch (sta->timeout_next) {
512 case STA_NULLFUNC:
513 sta->timeout_next = STA_DISASSOC;
42ca9845
JM
514 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
515 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
516 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
6fc6879b
JM
517 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
518 hapd, sta);
519 break;
520 case STA_DISASSOC:
d5b559b6 521 case STA_DISASSOC_FROM_CLI:
ae055af4 522 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b
JM
523 sta->flags &= ~WLAN_STA_ASSOC;
524 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
525 if (!sta->acct_terminate_cause)
526 sta->acct_terminate_cause =
527 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
528 accounting_sta_stop(hapd, sta);
d7c3347f 529 ieee802_1x_free_station(hapd, sta);
6fc6879b
JM
530 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
531 HOSTAPD_LEVEL_INFO, "disassociated due to "
532 "inactivity");
d5b559b6
KP
533 reason = (sta->timeout_next == STA_DISASSOC) ?
534 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
535 WLAN_REASON_PREV_AUTH_NOT_VALID;
6fc6879b 536 sta->timeout_next = STA_DEAUTH;
42ca9845
JM
537 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
538 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
539 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
6fc6879b
JM
540 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
541 hapd, sta);
d5b559b6 542 mlme_disassociate_indication(hapd, sta, reason);
6fc6879b
JM
543 break;
544 case STA_DEAUTH:
545 case STA_REMOVE:
546 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
547 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
afcc9ea1 548 "inactivity (timer DEAUTH/REMOVE)");
6fc6879b
JM
549 if (!sta->acct_terminate_cause)
550 sta->acct_terminate_cause =
551 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
552 mlme_deauthenticate_indication(
553 hapd, sta,
554 WLAN_REASON_PREV_AUTH_NOT_VALID);
555 ap_free_sta(hapd, sta);
556 break;
557 }
558}
559
560
561static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
562{
563 struct hostapd_data *hapd = eloop_ctx;
564 struct sta_info *sta = timeout_ctx;
6fc6879b 565
03269d55
JM
566 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
567 hapd->conf->iface, MAC2STR(sta->addr));
dca30c3f
JK
568 if (!(sta->flags & WLAN_STA_AUTH)) {
569 if (sta->flags & WLAN_STA_GAS) {
570 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
571 "entry " MACSTR, MAC2STR(sta->addr));
572 ap_free_sta(hapd, sta);
573 }
6fc6879b 574 return;
dca30c3f 575 }
6fc6879b 576
0ac38766
JM
577 hostapd_drv_sta_deauth(hapd, sta->addr,
578 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b
JM
579 mlme_deauthenticate_indication(hapd, sta,
580 WLAN_REASON_PREV_AUTH_NOT_VALID);
581 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
582 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
583 "session timeout");
584 sta->acct_terminate_cause =
585 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
6fc6879b 586 ap_free_sta(hapd, sta);
6fc6879b
JM
587}
588
589
91f9e607
KP
590void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
591 u32 session_timeout)
592{
593 if (eloop_replenish_timeout(session_timeout, 0,
a09ffd5f 594 ap_handle_session_timer, hapd, sta) == 1) {
91f9e607
KP
595 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
596 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
597 "to %d seconds", session_timeout);
598 }
599}
600
601
6fc6879b
JM
602void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
603 u32 session_timeout)
604{
605 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
606 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
607 "seconds", session_timeout);
608 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
609 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
610 hapd, sta);
611}
612
613
614void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
615{
616 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
617}
618
619
97596f8e
JM
620static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
621{
b5bf84ba 622#ifdef CONFIG_WNM_AP
97596f8e
JM
623 struct hostapd_data *hapd = eloop_ctx;
624 struct sta_info *sta = timeout_ctx;
625
03269d55
JM
626 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
627 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
97596f8e
JM
628 if (sta->hs20_session_info_url == NULL)
629 return;
630
631 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
632 sta->hs20_disassoc_timer);
b5bf84ba 633#endif /* CONFIG_WNM_AP */
97596f8e
JM
634}
635
636
637void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
638 struct sta_info *sta, int warning_time)
639{
640 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
641 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
642 hapd, sta);
643}
644
645
6fc6879b
JM
646struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
647{
648 struct sta_info *sta;
649
650 sta = ap_get_sta(hapd, addr);
651 if (sta)
652 return sta;
653
654 wpa_printf(MSG_DEBUG, " New STA");
655 if (hapd->num_sta >= hapd->conf->max_num_sta) {
656 /* FIX: might try to remove some old STAs first? */
657 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
658 hapd->num_sta, hapd->conf->max_num_sta);
659 return NULL;
660 }
661
662 sta = os_zalloc(sizeof(struct sta_info));
663 if (sta == NULL) {
664 wpa_printf(MSG_ERROR, "malloc failed");
665 return NULL;
666 }
5843e1c9 667 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
d72a0053
NL
668 if (accounting_sta_get_id(hapd, sta) < 0) {
669 os_free(sta);
670 return NULL;
671 }
6fc6879b 672
336167c8
MSS
673 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
674 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
675 "for " MACSTR " (%d seconds - ap_max_inactivity)",
676 __func__, MAC2STR(addr),
677 hapd->conf->ap_max_inactivity);
678 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
679 ap_handle_timer, hapd, sta);
680 }
681
6fc6879b 682 /* initialize STA info data */
6fc6879b
JM
683 os_memcpy(sta->addr, addr, ETH_ALEN);
684 sta->next = hapd->sta_list;
685 hapd->sta_list = sta;
686 hapd->num_sta++;
687 ap_sta_hash_add(hapd, sta);
53f3d6f3 688 ap_sta_remove_in_other_bss(hapd, sta);
38cb0a2d 689 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
bd00c431 690 dl_list_init(&sta->ip6addr);
6fc6879b 691
44281940
DG
692#ifdef CONFIG_TAXONOMY
693 sta_track_claim_taxonomy_info(hapd->iface, addr,
694 &sta->probe_ie_taxonomy);
695#endif /* CONFIG_TAXONOMY */
696
6fc6879b
JM
697 return sta;
698}
699
700
701static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
702{
703 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
704
7d597d46 705 if (sta->ipaddr)
ed4ddb6d 706 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
bd00c431 707 ap_sta_ip6addr_del(hapd, sta);
7d597d46 708
03269d55
JM
709 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
710 hapd->conf->iface, MAC2STR(sta->addr));
51e2a27a 711 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
6fc6879b 712 sta->flags & WLAN_STA_ASSOC) {
03269d55
JM
713 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
714 " from kernel driver",
715 hapd->conf->iface, MAC2STR(sta->addr));
6fc6879b
JM
716 return -1;
717 }
bb598c3b 718 sta->added_unassoc = 0;
6fc6879b
JM
719 return 0;
720}
721
722
53f3d6f3
FF
723static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
724 struct sta_info *sta)
6fc6879b
JM
725{
726 struct hostapd_iface *iface = hapd->iface;
727 size_t i;
728
729 for (i = 0; i < iface->num_bss; i++) {
730 struct hostapd_data *bss = iface->bss[i];
731 struct sta_info *sta2;
732 /* bss should always be set during operation, but it may be
733 * NULL during reconfiguration. Assume the STA is not
734 * associated to another BSS in that case to avoid NULL pointer
735 * dereferences. */
736 if (bss == hapd || bss == NULL)
737 continue;
738 sta2 = ap_get_sta(bss, sta->addr);
53f3d6f3
FF
739 if (!sta2)
740 continue;
6fc6879b 741
03269d55
JM
742 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
743 " association from another BSS %s",
744 hapd->conf->iface, MAC2STR(sta2->addr),
745 bss->conf->iface);
53f3d6f3
FF
746 ap_sta_disconnect(bss, sta2, sta2->addr,
747 WLAN_REASON_PREV_AUTH_NOT_VALID);
748 }
6fc6879b
JM
749}
750
751
4dc03726
JM
752static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
753{
754 struct hostapd_data *hapd = eloop_ctx;
755 struct sta_info *sta = timeout_ctx;
756
03269d55
JM
757 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
758 hapd->conf->iface, MAC2STR(sta->addr));
4dc03726
JM
759 ap_sta_remove(hapd, sta);
760 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
761}
762
763
6fc6879b
JM
764void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
765 u16 reason)
766{
767 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
768 hapd->conf->iface, MAC2STR(sta->addr));
38cb0a2d 769 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
05e5e615
DL
770 if (hapd->iface->current_mode &&
771 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
772 /* Skip deauthentication in DMG/IEEE 802.11ad */
773 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
774 WLAN_STA_ASSOC_REQ_OK);
775 sta->timeout_next = STA_REMOVE;
776 } else {
777 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
778 sta->timeout_next = STA_DEAUTH;
779 }
4dc03726 780 ap_sta_set_authorized(hapd, sta, 0);
42ca9845
JM
781 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
782 "for " MACSTR " (%d seconds - "
783 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
784 __func__, MAC2STR(sta->addr),
785 AP_MAX_INACTIVITY_AFTER_DISASSOC);
6fc6879b
JM
786 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
787 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
788 ap_handle_timer, hapd, sta);
789 accounting_sta_stop(hapd, sta);
d7c3347f 790 ieee802_1x_free_station(hapd, sta);
6fc6879b 791
4dc03726 792 sta->disassoc_reason = reason;
cc28ad8c 793 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
4dc03726
JM
794 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
795 eloop_register_timeout(hapd->iface->drv_flags &
796 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
797 ap_sta_disassoc_cb_timeout, hapd, sta);
798}
799
800
801static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
802{
803 struct hostapd_data *hapd = eloop_ctx;
804 struct sta_info *sta = timeout_ctx;
805
03269d55
JM
806 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
807 hapd->conf->iface, MAC2STR(sta->addr));
4dc03726
JM
808 ap_sta_remove(hapd, sta);
809 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
6fc6879b
JM
810}
811
812
813void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
814 u16 reason)
815{
05e5e615
DL
816 if (hapd->iface->current_mode &&
817 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
818 /* Deauthentication is not used in DMG/IEEE 802.11ad;
819 * disassociate the STA instead. */
820 ap_sta_disassociate(hapd, sta, reason);
821 return;
822 }
823
6fc6879b
JM
824 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
825 hapd->conf->iface, MAC2STR(sta->addr));
38cb0a2d 826 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
631739b3 827 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
4dc03726 828 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b 829 sta->timeout_next = STA_REMOVE;
42ca9845
JM
830 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
831 "for " MACSTR " (%d seconds - "
832 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
833 __func__, MAC2STR(sta->addr),
834 AP_MAX_INACTIVITY_AFTER_DEAUTH);
6fc6879b
JM
835 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
836 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
837 ap_handle_timer, hapd, sta);
838 accounting_sta_stop(hapd, sta);
d7c3347f 839 ieee802_1x_free_station(hapd, sta);
6fc6879b 840
4dc03726 841 sta->deauth_reason = reason;
cc28ad8c 842 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
843 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
844 eloop_register_timeout(hapd->iface->drv_flags &
845 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
846 ap_sta_deauth_cb_timeout, hapd, sta);
6fc6879b
JM
847}
848
849
4c374cde
AS
850#ifdef CONFIG_WPS
851int ap_sta_wps_cancel(struct hostapd_data *hapd,
852 struct sta_info *sta, void *ctx)
853{
854 if (sta && (sta->flags & WLAN_STA_WPS)) {
855 ap_sta_deauthenticate(hapd, sta,
856 WLAN_REASON_PREV_AUTH_NOT_VALID);
857 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
858 __func__, MAC2STR(sta->addr));
859 return 1;
860 }
861
862 return 0;
863}
864#endif /* CONFIG_WPS */
865
866
8e44c192
MB
867static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
868{
869 struct hostapd_vlan *vlan;
870 int vlan_id = MAX_VLAN_ID + 2;
871
872retry:
873 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
874 if (vlan->vlan_id == vlan_id) {
875 vlan_id++;
876 goto retry;
877 }
878 }
879 return vlan_id;
880}
881
882
1889af2e
MB
883int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
884 struct vlan_description *vlan_desc)
885{
886 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
887 int old_vlan_id, vlan_id = 0, ret = 0;
888
8e44c192 889 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
1889af2e 890 vlan_desc = NULL;
1889af2e 891
8e44c192 892 /* Check if there is something to do */
8be640b7
MB
893 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
894 /* This sta is lacking its own vif */
895 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
896 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
897 /* sta->vlan_id needs to be reset */
898 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
8e44c192 899 return 0; /* nothing to change */
8be640b7 900 }
8e44c192
MB
901
902 /* Now the real VLAN changed or the STA just needs its own vif */
8be640b7
MB
903 if (hapd->conf->ssid.per_sta_vif) {
904 /* Assign a new vif, always */
905 /* find a free vlan_id sufficiently big */
906 vlan_id = ap_sta_get_free_vlan_id(hapd);
907 /* Get wildcard VLAN */
908 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
909 if (vlan->vlan_id == VLAN_ID_WILDCARD)
910 break;
911 }
912 if (!vlan) {
913 hostapd_logger(hapd, sta->addr,
914 HOSTAPD_MODULE_IEEE80211,
915 HOSTAPD_LEVEL_DEBUG,
916 "per_sta_vif missing wildcard");
917 vlan_id = 0;
918 ret = -1;
919 goto done;
920 }
921 } else if (vlan_desc && vlan_desc->notempty) {
1889af2e
MB
922 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
923 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
924 break;
925 if (vlan->vlan_id == VLAN_ID_WILDCARD)
926 wildcard_vlan = vlan;
927 }
928 if (vlan) {
929 vlan_id = vlan->vlan_id;
930 } else if (wildcard_vlan) {
931 vlan = wildcard_vlan;
932 vlan_id = vlan_desc->untagged;
8e44c192
MB
933 if (vlan_desc->tagged[0]) {
934 /* Tagged VLAN configuration */
935 vlan_id = ap_sta_get_free_vlan_id(hapd);
936 }
1889af2e
MB
937 } else {
938 hostapd_logger(hapd, sta->addr,
939 HOSTAPD_MODULE_IEEE80211,
940 HOSTAPD_LEVEL_DEBUG,
8e44c192
MB
941 "missing vlan and wildcard for vlan=%d%s",
942 vlan_desc->untagged,
943 vlan_desc->tagged[0] ? "+" : "");
1889af2e
MB
944 vlan_id = 0;
945 ret = -1;
946 goto done;
947 }
948 }
949
950 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
951 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
952 if (vlan == NULL) {
953 hostapd_logger(hapd, sta->addr,
954 HOSTAPD_MODULE_IEEE80211,
955 HOSTAPD_LEVEL_DEBUG,
8e44c192 956 "could not add dynamic VLAN interface for vlan=%d%s",
75cc211d
JM
957 vlan_desc ? vlan_desc->untagged : -1,
958 (vlan_desc && vlan_desc->tagged[0]) ?
959 "+" : "");
1889af2e
MB
960 vlan_id = 0;
961 ret = -1;
962 goto done;
963 }
964
965 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
966 HOSTAPD_LEVEL_DEBUG,
967 "added new dynamic VLAN interface '%s'",
968 vlan->ifname);
969 } else if (vlan && vlan->dynamic_vlan > 0) {
970 vlan->dynamic_vlan++;
971 hostapd_logger(hapd, sta->addr,
972 HOSTAPD_MODULE_IEEE80211,
973 HOSTAPD_LEVEL_DEBUG,
974 "updated existing dynamic VLAN interface '%s'",
975 vlan->ifname);
976 }
977done:
978 old_vlan_id = sta->vlan_id;
979 sta->vlan_id = vlan_id;
980 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
981
982 if (vlan_id != old_vlan_id && old_vlan_id)
983 vlan_remove_dynamic(hapd, old_vlan_id);
984
985 return ret;
986}
987
988
c8e6beab 989int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
6fc6879b 990{
30b32314 991#ifndef CONFIG_NO_VLAN
6fc6879b
JM
992 const char *iface;
993 struct hostapd_vlan *vlan = NULL;
4254100d 994 int ret;
c8e6beab 995 int old_vlanid = sta->vlan_id_bound;
6fc6879b 996
6fc6879b 997 iface = hapd->conf->iface;
f41ded6f
JM
998 if (hapd->conf->ssid.vlan[0])
999 iface = hapd->conf->ssid.vlan;
6fc6879b 1000
1889af2e
MB
1001 if (sta->vlan_id > 0) {
1002 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
c2db79f2 1003 if (vlan->vlan_id == sta->vlan_id)
6fc6879b 1004 break;
6fc6879b 1005 }
c2db79f2
MB
1006 if (vlan)
1007 iface = vlan->ifname;
6fc6879b
JM
1008 }
1009
c8e6beab
MB
1010 /*
1011 * Do not increment ref counters if the VLAN ID remains same, but do
1012 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1013 * have been called before.
1014 */
1015 if (sta->vlan_id == old_vlanid)
1016 goto skip_counting;
1017
6fc6879b
JM
1018 if (sta->vlan_id > 0 && vlan == NULL) {
1019 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1020 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1021 "binding station to (vlan_id=%d)",
1022 sta->vlan_id);
2dd4f3ae
JM
1023 ret = -1;
1024 goto done;
1889af2e 1025 } else if (vlan && vlan->dynamic_vlan > 0) {
41d62107
MB
1026 vlan->dynamic_vlan++;
1027 hostapd_logger(hapd, sta->addr,
1028 HOSTAPD_MODULE_IEEE80211,
1029 HOSTAPD_LEVEL_DEBUG,
1030 "updated existing dynamic VLAN interface '%s'",
1031 iface);
6fc6879b
JM
1032 }
1033
c8e6beab
MB
1034 /* ref counters have been increased, so mark the station */
1035 sta->vlan_id_bound = sta->vlan_id;
1036
1037skip_counting:
6fc6879b
JM
1038 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1039 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1040 "'%s'", iface);
1041
1042 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1043 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1044
51e2a27a 1045 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
4254100d
JM
1046 if (ret < 0) {
1047 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1048 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1049 "entry to vlan_id=%d", sta->vlan_id);
1050 }
2dd4f3ae 1051
2dd4f3ae 1052 /* During 1x reauth, if the vlan id changes, then remove the old id. */
c8e6beab 1053 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
2dd4f3ae 1054 vlan_remove_dynamic(hapd, old_vlanid);
c8e6beab 1055done:
2dd4f3ae 1056
4254100d 1057 return ret;
30b32314
JM
1058#else /* CONFIG_NO_VLAN */
1059 return 0;
1060#endif /* CONFIG_NO_VLAN */
6fc6879b 1061}
5d22a1d5
JM
1062
1063
1064#ifdef CONFIG_IEEE80211W
1065
45c94154 1066int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 1067{
45c94154 1068 u32 tu;
10e694a6
JB
1069 struct os_reltime now, passed;
1070 os_get_reltime(&now);
1071 os_reltime_sub(&now, &sta->sa_query_start, &passed);
45c94154
JM
1072 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1073 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1074 hostapd_logger(hapd, sta->addr,
1075 HOSTAPD_MODULE_IEEE80211,
5d22a1d5 1076 HOSTAPD_LEVEL_DEBUG,
93b76319
JM
1077 "association SA Query timed out");
1078 sta->sa_query_timed_out = 1;
1079 os_free(sta->sa_query_trans_id);
1080 sta->sa_query_trans_id = NULL;
1081 sta->sa_query_count = 0;
45c94154
JM
1082 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1083 return 1;
5d22a1d5
JM
1084 }
1085
45c94154
JM
1086 return 0;
1087}
1088
1089
1090static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1091{
1092 struct hostapd_data *hapd = eloop_ctx;
1093 struct sta_info *sta = timeout_ctx;
1094 unsigned int timeout, sec, usec;
1095 u8 *trans_id, *nbuf;
1096
03269d55
JM
1097 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1098 " (count=%d)",
1099 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1100
45c94154
JM
1101 if (sta->sa_query_count > 0 &&
1102 ap_check_sa_query_timeout(hapd, sta))
1103 return;
1104
067ffa26
JM
1105 nbuf = os_realloc_array(sta->sa_query_trans_id,
1106 sta->sa_query_count + 1,
1107 WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5
JM
1108 if (nbuf == NULL)
1109 return;
45c94154
JM
1110 if (sta->sa_query_count == 0) {
1111 /* Starting a new SA Query procedure */
10e694a6 1112 os_get_reltime(&sta->sa_query_start);
45c94154 1113 }
93b76319
JM
1114 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1115 sta->sa_query_trans_id = nbuf;
1116 sta->sa_query_count++;
5d22a1d5 1117
24661bba
JM
1118 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1119 /*
1120 * We don't really care which ID is used here, so simply
1121 * hardcode this if the mostly theoretical os_get_random()
1122 * failure happens.
1123 */
1124 trans_id[0] = 0x12;
1125 trans_id[1] = 0x34;
1126 }
5d22a1d5 1127
45c94154
JM
1128 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1129 sec = ((timeout / 1000) * 1024) / 1000;
1130 usec = (timeout % 1000) * 1024;
1131 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1132
5d22a1d5
JM
1133 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1134 HOSTAPD_LEVEL_DEBUG,
93b76319 1135 "association SA Query attempt %d", sta->sa_query_count);
5d22a1d5 1136
93b76319 1137 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
5d22a1d5
JM
1138}
1139
1140
93b76319 1141void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 1142{
93b76319 1143 ap_sa_query_timer(hapd, sta);
5d22a1d5
JM
1144}
1145
1146
93b76319 1147void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 1148{
93b76319
JM
1149 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1150 os_free(sta->sa_query_trans_id);
1151 sta->sa_query_trans_id = NULL;
1152 sta->sa_query_count = 0;
5d22a1d5
JM
1153}
1154
1155#endif /* CONFIG_IEEE80211W */
45cefa0b
JM
1156
1157
6905dcb1
JB
1158void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1159 int authorized)
1160{
10cc6c88 1161 const u8 *dev_addr = NULL;
7793c959 1162 char buf[100];
c2d76aa6
MH
1163#ifdef CONFIG_P2P
1164 u8 addr[ETH_ALEN];
25ef8529 1165 u8 ip_addr_buf[4];
c2d76aa6
MH
1166#endif /* CONFIG_P2P */
1167
6905dcb1
JB
1168 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1169 return;
1170
61fc9048
SD
1171 if (authorized)
1172 sta->flags |= WLAN_STA_AUTHORIZED;
1173 else
1174 sta->flags &= ~WLAN_STA_AUTHORIZED;
1175
ae055af4 1176#ifdef CONFIG_P2P
c2d76aa6
MH
1177 if (hapd->p2p_group == NULL) {
1178 if (sta->p2p_ie != NULL &&
1179 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1180 dev_addr = addr;
1181 } else
1182 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
10cc6c88 1183
7793c959
JM
1184 if (dev_addr)
1185 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1186 MAC2STR(sta->addr), MAC2STR(dev_addr));
1187 else
375f4a3b 1188#endif /* CONFIG_P2P */
7793c959
JM
1189 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1190
61fc9048
SD
1191 if (hapd->sta_authorized_cb)
1192 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1193 sta->addr, authorized, dev_addr);
1194
10cc6c88 1195 if (authorized) {
25ef8529
JM
1196 char ip_addr[100];
1197 ip_addr[0] = '\0';
1198#ifdef CONFIG_P2P
1199 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1200 os_snprintf(ip_addr, sizeof(ip_addr),
1201 " ip_addr=%u.%u.%u.%u",
1202 ip_addr_buf[0], ip_addr_buf[1],
1203 ip_addr_buf[2], ip_addr_buf[3]);
1204 }
1205#endif /* CONFIG_P2P */
1206
1207 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1208 buf, ip_addr);
7793c959 1209
8a5e75f6 1210 if (hapd->msg_ctx_parent &&
7793c959 1211 hapd->msg_ctx_parent != hapd->msg_ctx)
c4bf83a7 1212 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
25ef8529
JM
1213 AP_STA_CONNECTED "%s%s",
1214 buf, ip_addr);
ae055af4 1215 } else {
7793c959
JM
1216 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1217
8a5e75f6 1218 if (hapd->msg_ctx_parent &&
7793c959 1219 hapd->msg_ctx_parent != hapd->msg_ctx)
c4bf83a7
JM
1220 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1221 AP_STA_DISCONNECTED "%s", buf);
ae055af4 1222 }
6959145b
AN
1223
1224#ifdef CONFIG_FST
1225 if (hapd->iface->fst) {
1226 if (authorized)
1227 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1228 else
1229 fst_notify_peer_disconnected(hapd->iface->fst,
1230 sta->addr);
1231 }
1232#endif /* CONFIG_FST */
6905dcb1
JB
1233}
1234
1235
45cefa0b
JM
1236void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1237 const u8 *addr, u16 reason)
1238{
03269d55
JM
1239 if (sta)
1240 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1241 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1242 reason);
1243 else if (addr)
1244 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1245 hapd->conf->iface, __func__, MAC2STR(addr),
1246 reason);
45cefa0b
JM
1247
1248 if (sta == NULL && addr)
1249 sta = ap_get_sta(hapd, addr);
1250
1251 if (addr)
51e2a27a 1252 hostapd_drv_sta_deauth(hapd, addr, reason);
45cefa0b
JM
1253
1254 if (sta == NULL)
1255 return;
6905dcb1 1256 ap_sta_set_authorized(hapd, sta, 0);
ceb997f3
JM
1257 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1258 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
6905dcb1 1259 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
03269d55 1260 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
42ca9845
JM
1261 "for " MACSTR " (%d seconds - "
1262 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
03269d55 1263 hapd->conf->iface, __func__, MAC2STR(sta->addr),
42ca9845 1264 AP_MAX_INACTIVITY_AFTER_DEAUTH);
45cefa0b 1265 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4dc03726
JM
1266 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1267 ap_handle_timer, hapd, sta);
45cefa0b 1268 sta->timeout_next = STA_REMOVE;
4dc03726 1269
05e5e615
DL
1270 if (hapd->iface->current_mode &&
1271 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1272 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1273 * disassociate the STA instead. */
1274 sta->disassoc_reason = reason;
1275 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1276 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1277 eloop_register_timeout(hapd->iface->drv_flags &
1278 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ?
1279 2 : 0, 0, ap_sta_disassoc_cb_timeout,
1280 hapd, sta);
1281 return;
1282 }
1283
4dc03726 1284 sta->deauth_reason = reason;
cc28ad8c 1285 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
1286 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1287 eloop_register_timeout(hapd->iface->drv_flags &
1288 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1289 ap_sta_deauth_cb_timeout, hapd, sta);
1290}
1291
1292
1293void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1294{
cc28ad8c
JM
1295 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1296 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1297 return;
1298 }
1299 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
1300 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1301 ap_sta_deauth_cb_timeout(hapd, sta);
1302}
1303
1304
1305void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1306{
cc28ad8c
JM
1307 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1308 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1309 return;
1310 }
1311 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1312 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1313 ap_sta_disassoc_cb_timeout(hapd, sta);
45cefa0b 1314}
b76f4c27
JM
1315
1316
9e8fde21
JM
1317void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1318 struct sta_info *sta)
1319{
1320 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1321 wpa_printf(MSG_DEBUG,
1322 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1323 MACSTR,
1324 hapd->conf->iface, MAC2STR(sta->addr));
1325 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1326 wpa_printf(MSG_DEBUG,
1327 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1328 MACSTR,
1329 hapd->conf->iface, MAC2STR(sta->addr));
0f5eb69f
JM
1330 if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1331 {
1332 wpa_printf(MSG_DEBUG,
1333 "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1334 MACSTR,
1335 hapd->conf->iface, MAC2STR(sta->addr));
1336 if (sta->flags & WLAN_STA_WPS)
1337 hostapd_wps_eap_completed(hapd);
1338 }
9e8fde21
JM
1339}
1340
1341
b76f4c27
JM
1342int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1343{
1344 int res;
1345
1346 buf[0] = '\0';
e7d0e97b 1347 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
b76f4c27
JM
1348 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1349 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1350 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1351 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1352 ""),
1353 (flags & WLAN_STA_SHORT_PREAMBLE ?
1354 "[SHORT_PREAMBLE]" : ""),
1355 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1356 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1357 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1358 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1359 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1360 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1361 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1362 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1363 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1364 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
e7d0e97b 1365 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
b76f4c27
JM
1366 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1367 "[WNM_SLEEP_MODE]" : ""));
aaadd727
JM
1368 if (os_snprintf_error(buflen, res))
1369 res = -1;
b76f4c27
JM
1370
1371 return res;
1372}
0f5eb69f
JM
1373
1374
1375static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1376{
1377 struct hostapd_data *hapd = eloop_ctx;
1378 struct sta_info *sta = timeout_ctx;
1379
1380 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1381 "IEEE 802.1X: Scheduled disconnection of " MACSTR
1382 " after EAP-Failure", MAC2STR(sta->addr));
1383
1384 ap_sta_disconnect(hapd, sta, sta->addr,
1385 WLAN_REASON_IEEE_802_1X_AUTH_FAILED);
1386 if (sta->flags & WLAN_STA_WPS)
1387 hostapd_wps_eap_completed(hapd);
1388}
1389
1390
1391void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1392 struct sta_info *sta)
1393{
1394 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1395 "IEEE 802.1X: Force disconnection of " MACSTR
1396 " after EAP-Failure in 10 ms", MAC2STR(sta->addr));
1397
1398 /*
1399 * Add a small sleep to increase likelihood of previously requested
1400 * EAP-Failure TX getting out before this should the driver reorder
1401 * operations.
1402 */
1403 eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
1404 eloop_register_timeout(0, 10000, ap_sta_delayed_1x_auth_fail_cb,
1405 hapd, sta);
1406}
1407
1408
1409int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1410 struct sta_info *sta)
1411{
1412 return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1413 hapd, sta);
1414}