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