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