]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/sta_info.c
Restore scan_req if sta scan is rescheduled in the scan results event
[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"
18#include "drivers/driver.h"
8ccbe415 19#include "p2p/p2p.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"
6226e38d 34#include "sta_info.h"
6fc6879b 35
53f3d6f3
FF
36static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
37 struct sta_info *sta);
6fc6879b 38static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
4dc03726
JM
39static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
40static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 41#ifdef CONFIG_IEEE80211W
93b76319 42static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 43#endif /* CONFIG_IEEE80211W */
4dc03726 44static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
6fc6879b
JM
45
46int ap_for_each_sta(struct hostapd_data *hapd,
47 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
48 void *ctx),
49 void *ctx)
50{
51 struct sta_info *sta;
52
53 for (sta = hapd->sta_list; sta; sta = sta->next) {
54 if (cb(hapd, sta, ctx))
55 return 1;
56 }
57
58 return 0;
59}
60
61
62struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
63{
64 struct sta_info *s;
65
66 s = hapd->sta_hash[STA_HASH(sta)];
67 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
68 s = s->hnext;
69 return s;
70}
71
72
f2c56602
JM
73#ifdef CONFIG_P2P
74struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
75{
76 struct sta_info *sta;
77
78 for (sta = hapd->sta_list; sta; sta = sta->next) {
79 const u8 *p2p_dev_addr;
80
81 if (sta->p2p_ie == NULL)
82 continue;
83
84 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
85 if (p2p_dev_addr == NULL)
86 continue;
87
88 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
89 return sta;
90 }
91
92 return NULL;
93}
94#endif /* CONFIG_P2P */
95
96
6fc6879b
JM
97static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
98{
99 struct sta_info *tmp;
100
101 if (hapd->sta_list == sta) {
102 hapd->sta_list = sta->next;
103 return;
104 }
105
106 tmp = hapd->sta_list;
107 while (tmp != NULL && tmp->next != sta)
108 tmp = tmp->next;
109 if (tmp == NULL) {
110 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
111 "list.", MAC2STR(sta->addr));
112 } else
113 tmp->next = sta->next;
114}
115
116
117void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
118{
119 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
120 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
121}
122
123
124static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
125{
126 struct sta_info *s;
127
128 s = hapd->sta_hash[STA_HASH(sta->addr)];
129 if (s == NULL) return;
130 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
131 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
132 return;
133 }
134
135 while (s->hnext != NULL &&
136 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
137 s = s->hnext;
138 if (s->hnext != NULL)
139 s->hnext = s->hnext->hnext;
140 else
141 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
142 " from hash table", MAC2STR(sta->addr));
143}
144
145
146void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
147{
148 int set_beacon = 0;
149
150 accounting_sta_stop(hapd, sta);
151
6905dcb1
JB
152 /* just in case */
153 ap_sta_set_authorized(hapd, sta, 0);
154
39f42d11 155 if (sta->flags & WLAN_STA_WDS)
69dd2967 156 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
53f3d6f3
FF
157
158 if (!(sta->flags & WLAN_STA_PREAUTH))
51e2a27a 159 hostapd_drv_sta_remove(hapd, sta->addr);
6fc6879b
JM
160
161 ap_sta_hash_del(hapd, sta);
162 ap_sta_list_del(hapd, sta);
163
164 if (sta->aid > 0)
2991469c
JM
165 hapd->sta_aid[(sta->aid - 1) / 32] &=
166 ~BIT((sta->aid - 1) % 32);
6fc6879b
JM
167
168 hapd->num_sta--;
169 if (sta->nonerp_set) {
170 sta->nonerp_set = 0;
171 hapd->iface->num_sta_non_erp--;
172 if (hapd->iface->num_sta_non_erp == 0)
173 set_beacon++;
174 }
175
176 if (sta->no_short_slot_time_set) {
177 sta->no_short_slot_time_set = 0;
178 hapd->iface->num_sta_no_short_slot_time--;
179 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
180 && hapd->iface->num_sta_no_short_slot_time == 0)
181 set_beacon++;
182 }
183
184 if (sta->no_short_preamble_set) {
185 sta->no_short_preamble_set = 0;
186 hapd->iface->num_sta_no_short_preamble--;
187 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
188 && hapd->iface->num_sta_no_short_preamble == 0)
189 set_beacon++;
190 }
191
e8ff1e59
JM
192 if (sta->no_ht_gf_set) {
193 sta->no_ht_gf_set = 0;
194 hapd->iface->num_sta_ht_no_gf--;
195 }
196
197 if (sta->no_ht_set) {
198 sta->no_ht_set = 0;
de9289c8 199 hapd->iface->num_sta_no_ht--;
e8ff1e59
JM
200 }
201
202 if (sta->ht_20mhz_set) {
203 sta->ht_20mhz_set = 0;
204 hapd->iface->num_sta_ht_20mhz--;
205 }
de9289c8 206
aefb53bd
JM
207#ifdef CONFIG_P2P
208 if (sta->no_p2p_set) {
209 sta->no_p2p_set = 0;
210 hapd->num_sta_no_p2p--;
211 if (hapd->num_sta_no_p2p == 0)
212 hostapd_p2p_non_p2p_sta_disconnected(hapd);
213 }
214#endif /* CONFIG_P2P */
215
d45354be 216#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
de9289c8
JM
217 if (hostapd_ht_operation_update(hapd->iface) > 0)
218 set_beacon++;
d45354be 219#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
de9289c8 220
6fc6879b
JM
221 if (set_beacon)
222 ieee802_11_set_beacons(hapd->iface);
223
42ca9845
JM
224 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
225 __func__, MAC2STR(sta->addr));
6fc6879b
JM
226 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
227 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
4dc03726
JM
228 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
229 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
6fc6879b
JM
230
231 ieee802_1x_free_station(sta);
232 wpa_auth_sta_deinit(sta->wpa_sm);
233 rsn_preauth_free_station(hapd, sta);
74784010 234#ifndef CONFIG_NO_RADIUS
ded22b53
HS
235 if (hapd->radius)
236 radius_client_flush_auth(hapd->radius, sta->addr);
74784010 237#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
238
239 os_free(sta->last_assoc_req);
240 os_free(sta->challenge);
5d22a1d5
JM
241
242#ifdef CONFIG_IEEE80211W
93b76319
JM
243 os_free(sta->sa_query_trans_id);
244 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
5d22a1d5
JM
245#endif /* CONFIG_IEEE80211W */
246
8ccbe415
JM
247#ifdef CONFIG_P2P
248 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
249#endif /* CONFIG_P2P */
250
dca30c3f
JK
251#ifdef CONFIG_INTERWORKING
252 if (sta->gas_dialog) {
253 int i;
254 for (i = 0; i < GAS_DIALOG_MAX; i++)
255 gas_serv_dialog_clear(&sta->gas_dialog[i]);
256 os_free(sta->gas_dialog);
257 }
258#endif /* CONFIG_INTERWORKING */
259
eb76b7e3 260 wpabuf_free(sta->wps_ie);
b305c684 261 wpabuf_free(sta->p2p_ie);
f403dcd6 262 wpabuf_free(sta->hs20_ie);
eb76b7e3 263
df84268a 264 os_free(sta->ht_capabilities);
cc14091e 265 os_free(sta->vht_capabilities);
f2a14be7 266 hostapd_free_psk_list(sta->psk);
2092597f
MB
267 os_free(sta->identity);
268 os_free(sta->radius_cui);
df84268a 269
98efcc41 270#ifdef CONFIG_SAE
a46d72d7 271 sae_clear_data(sta->sae);
98efcc41
JM
272 os_free(sta->sae);
273#endif /* CONFIG_SAE */
274
6fc6879b
JM
275 os_free(sta);
276}
277
278
279void hostapd_free_stas(struct hostapd_data *hapd)
280{
281 struct sta_info *sta, *prev;
282
283 sta = hapd->sta_list;
284
285 while (sta) {
286 prev = sta;
287 if (sta->flags & WLAN_STA_AUTH) {
288 mlme_deauthenticate_indication(
289 hapd, sta, WLAN_REASON_UNSPECIFIED);
290 }
291 sta = sta->next;
292 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
293 MAC2STR(prev->addr));
294 ap_free_sta(hapd, prev);
295 }
296}
297
298
1c6e69cc
JM
299/**
300 * ap_handle_timer - Per STA timer handler
301 * @eloop_ctx: struct hostapd_data *
302 * @timeout_ctx: struct sta_info *
303 *
304 * This function is called to check station activity and to remove inactive
305 * stations.
306 */
6fc6879b
JM
307void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
308{
309 struct hostapd_data *hapd = eloop_ctx;
310 struct sta_info *sta = timeout_ctx;
311 unsigned long next_time = 0;
d5b559b6 312 int reason;
6fc6879b 313
42ca9845
JM
314 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
315 __func__, MAC2STR(sta->addr), sta->flags,
316 sta->timeout_next);
6fc6879b
JM
317 if (sta->timeout_next == STA_REMOVE) {
318 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
319 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
320 "local deauth request");
321 ap_free_sta(hapd, sta);
322 return;
323 }
324
325 if ((sta->flags & WLAN_STA_ASSOC) &&
326 (sta->timeout_next == STA_NULLFUNC ||
327 sta->timeout_next == STA_DISASSOC)) {
328 int inactive_sec;
ce28e279
BG
329 /*
330 * Add random value to timeout so that we don't end up bouncing
331 * all stations at the same time if we have lots of associated
332 * stations that are idle (but keep re-associating).
333 */
334 int fuzz = os_random() % 20;
51e2a27a 335 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
6fc6879b 336 if (inactive_sec == -1) {
3ec1e902
JM
337 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
338 "Check inactivity: Could not "
d5674791 339 "get station info from kernel driver for "
24d75245 340 MACSTR, MAC2STR(sta->addr));
d5674791
JM
341 /*
342 * The driver may not support this functionality.
343 * Anyway, try again after the next inactivity timeout,
344 * but do not disconnect the station now.
345 */
ce28e279 346 next_time = hapd->conf->ap_max_inactivity + fuzz;
6fc6879b
JM
347 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
348 sta->flags & WLAN_STA_ASSOC) {
349 /* station activity detected; reset timeout state */
3ec1e902
JM
350 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
351 "Station " MACSTR " has been active %is ago",
24d75245 352 MAC2STR(sta->addr), inactive_sec);
6fc6879b 353 sta->timeout_next = STA_NULLFUNC;
ce28e279 354 next_time = hapd->conf->ap_max_inactivity + fuzz -
6fc6879b 355 inactive_sec;
24d75245 356 } else {
3ec1e902
JM
357 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
358 "Station " MACSTR " has been "
24d75245
BG
359 "inactive too long: %d sec, max allowed: %d",
360 MAC2STR(sta->addr), inactive_sec,
361 hapd->conf->ap_max_inactivity);
ef01fa7b
YAP
362
363 if (hapd->conf->skip_inactivity_poll)
364 sta->timeout_next = STA_DISASSOC;
6fc6879b
JM
365 }
366 }
367
368 if ((sta->flags & WLAN_STA_ASSOC) &&
369 sta->timeout_next == STA_DISASSOC &&
ef01fa7b
YAP
370 !(sta->flags & WLAN_STA_PENDING_POLL) &&
371 !hapd->conf->skip_inactivity_poll) {
3ec1e902
JM
372 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
373 " has ACKed data poll", MAC2STR(sta->addr));
6fc6879b
JM
374 /* data nullfunc frame poll did not produce TX errors; assume
375 * station ACKed it */
376 sta->timeout_next = STA_NULLFUNC;
377 next_time = hapd->conf->ap_max_inactivity;
378 }
379
380 if (next_time) {
42ca9845
JM
381 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
382 "for " MACSTR " (%lu seconds)",
383 __func__, MAC2STR(sta->addr), next_time);
6fc6879b
JM
384 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
385 sta);
386 return;
387 }
388
389 if (sta->timeout_next == STA_NULLFUNC &&
390 (sta->flags & WLAN_STA_ASSOC)) {
bcf24348 391 wpa_printf(MSG_DEBUG, " Polling STA");
6fc6879b 392 sta->flags |= WLAN_STA_PENDING_POLL;
bcf24348
JB
393 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
394 sta->flags & WLAN_STA_WMM);
6fc6879b
JM
395 } else if (sta->timeout_next != STA_REMOVE) {
396 int deauth = sta->timeout_next == STA_DEAUTH;
397
afcc9ea1
BG
398 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
399 "Timeout, sending %s info to STA " MACSTR,
400 deauth ? "deauthentication" : "disassociation",
401 MAC2STR(sta->addr));
6fc6879b
JM
402
403 if (deauth) {
51e2a27a
JM
404 hostapd_drv_sta_deauth(
405 hapd, sta->addr,
406 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b 407 } else {
d5b559b6
KP
408 reason = (sta->timeout_next == STA_DISASSOC) ?
409 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
410 WLAN_REASON_PREV_AUTH_NOT_VALID;
411
412 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
6fc6879b
JM
413 }
414 }
415
416 switch (sta->timeout_next) {
417 case STA_NULLFUNC:
418 sta->timeout_next = STA_DISASSOC;
42ca9845
JM
419 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
420 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
421 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
6fc6879b
JM
422 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
423 hapd, sta);
424 break;
425 case STA_DISASSOC:
d5b559b6 426 case STA_DISASSOC_FROM_CLI:
ae055af4 427 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b
JM
428 sta->flags &= ~WLAN_STA_ASSOC;
429 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
430 if (!sta->acct_terminate_cause)
431 sta->acct_terminate_cause =
432 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
433 accounting_sta_stop(hapd, sta);
434 ieee802_1x_free_station(sta);
435 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
436 HOSTAPD_LEVEL_INFO, "disassociated due to "
437 "inactivity");
d5b559b6
KP
438 reason = (sta->timeout_next == STA_DISASSOC) ?
439 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
440 WLAN_REASON_PREV_AUTH_NOT_VALID;
6fc6879b 441 sta->timeout_next = STA_DEAUTH;
42ca9845
JM
442 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
443 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
444 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
6fc6879b
JM
445 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
446 hapd, sta);
d5b559b6 447 mlme_disassociate_indication(hapd, sta, reason);
6fc6879b
JM
448 break;
449 case STA_DEAUTH:
450 case STA_REMOVE:
451 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
452 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
afcc9ea1 453 "inactivity (timer DEAUTH/REMOVE)");
6fc6879b
JM
454 if (!sta->acct_terminate_cause)
455 sta->acct_terminate_cause =
456 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
457 mlme_deauthenticate_indication(
458 hapd, sta,
459 WLAN_REASON_PREV_AUTH_NOT_VALID);
460 ap_free_sta(hapd, sta);
461 break;
462 }
463}
464
465
466static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
467{
468 struct hostapd_data *hapd = eloop_ctx;
469 struct sta_info *sta = timeout_ctx;
470 u8 addr[ETH_ALEN];
471
dca30c3f
JK
472 if (!(sta->flags & WLAN_STA_AUTH)) {
473 if (sta->flags & WLAN_STA_GAS) {
474 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
475 "entry " MACSTR, MAC2STR(sta->addr));
476 ap_free_sta(hapd, sta);
477 }
6fc6879b 478 return;
dca30c3f 479 }
6fc6879b
JM
480
481 mlme_deauthenticate_indication(hapd, sta,
482 WLAN_REASON_PREV_AUTH_NOT_VALID);
483 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
484 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
485 "session timeout");
486 sta->acct_terminate_cause =
487 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
488 os_memcpy(addr, sta->addr, ETH_ALEN);
489 ap_free_sta(hapd, sta);
51e2a27a 490 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b
JM
491}
492
493
91f9e607
KP
494void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
495 u32 session_timeout)
496{
497 if (eloop_replenish_timeout(session_timeout, 0,
498 ap_handle_session_timer, hapd, sta)) {
499 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
500 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
501 "to %d seconds", session_timeout);
502 }
503}
504
505
6fc6879b
JM
506void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
507 u32 session_timeout)
508{
509 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
510 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
511 "seconds", session_timeout);
512 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
513 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
514 hapd, sta);
515}
516
517
518void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
519{
520 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
521}
522
523
524struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
525{
526 struct sta_info *sta;
527
528 sta = ap_get_sta(hapd, addr);
529 if (sta)
530 return sta;
531
532 wpa_printf(MSG_DEBUG, " New STA");
533 if (hapd->num_sta >= hapd->conf->max_num_sta) {
534 /* FIX: might try to remove some old STAs first? */
535 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
536 hapd->num_sta, hapd->conf->max_num_sta);
537 return NULL;
538 }
539
540 sta = os_zalloc(sizeof(struct sta_info));
541 if (sta == NULL) {
542 wpa_printf(MSG_ERROR, "malloc failed");
543 return NULL;
544 }
5843e1c9 545 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
8b248611 546 accounting_sta_get_id(hapd, sta);
6fc6879b
JM
547
548 /* initialize STA info data */
42ca9845
JM
549 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
550 "for " MACSTR " (%d seconds - ap_max_inactivity)",
551 __func__, MAC2STR(addr),
552 hapd->conf->ap_max_inactivity);
6fc6879b
JM
553 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
554 ap_handle_timer, hapd, sta);
555 os_memcpy(sta->addr, addr, ETH_ALEN);
556 sta->next = hapd->sta_list;
557 hapd->sta_list = sta;
558 hapd->num_sta++;
559 ap_sta_hash_add(hapd, sta);
560 sta->ssid = &hapd->conf->ssid;
53f3d6f3 561 ap_sta_remove_in_other_bss(hapd, sta);
6fc6879b
JM
562
563 return sta;
564}
565
566
567static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
568{
569 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
570
571 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
572 MAC2STR(sta->addr));
51e2a27a 573 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
6fc6879b
JM
574 sta->flags & WLAN_STA_ASSOC) {
575 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
576 " from kernel driver.", MAC2STR(sta->addr));
577 return -1;
578 }
579 return 0;
580}
581
582
53f3d6f3
FF
583static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
584 struct sta_info *sta)
6fc6879b
JM
585{
586 struct hostapd_iface *iface = hapd->iface;
587 size_t i;
588
589 for (i = 0; i < iface->num_bss; i++) {
590 struct hostapd_data *bss = iface->bss[i];
591 struct sta_info *sta2;
592 /* bss should always be set during operation, but it may be
593 * NULL during reconfiguration. Assume the STA is not
594 * associated to another BSS in that case to avoid NULL pointer
595 * dereferences. */
596 if (bss == hapd || bss == NULL)
597 continue;
598 sta2 = ap_get_sta(bss, sta->addr);
53f3d6f3
FF
599 if (!sta2)
600 continue;
6fc6879b 601
53f3d6f3
FF
602 ap_sta_disconnect(bss, sta2, sta2->addr,
603 WLAN_REASON_PREV_AUTH_NOT_VALID);
604 }
6fc6879b
JM
605}
606
607
4dc03726
JM
608static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
609{
610 struct hostapd_data *hapd = eloop_ctx;
611 struct sta_info *sta = timeout_ctx;
612
613 ap_sta_remove(hapd, sta);
614 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
615}
616
617
6fc6879b
JM
618void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
619 u16 reason)
620{
621 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
622 hapd->conf->iface, MAC2STR(sta->addr));
ba873c12 623 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
4dc03726 624 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b 625 sta->timeout_next = STA_DEAUTH;
42ca9845
JM
626 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
627 "for " MACSTR " (%d seconds - "
628 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
629 __func__, MAC2STR(sta->addr),
630 AP_MAX_INACTIVITY_AFTER_DISASSOC);
6fc6879b
JM
631 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
632 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
633 ap_handle_timer, hapd, sta);
634 accounting_sta_stop(hapd, sta);
635 ieee802_1x_free_station(sta);
636
4dc03726 637 sta->disassoc_reason = reason;
cc28ad8c 638 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
4dc03726
JM
639 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
640 eloop_register_timeout(hapd->iface->drv_flags &
641 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
642 ap_sta_disassoc_cb_timeout, hapd, sta);
643}
644
645
646static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
647{
648 struct hostapd_data *hapd = eloop_ctx;
649 struct sta_info *sta = timeout_ctx;
650
651 ap_sta_remove(hapd, sta);
652 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
6fc6879b
JM
653}
654
655
656void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
657 u16 reason)
658{
659 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
660 hapd->conf->iface, MAC2STR(sta->addr));
661 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
4dc03726 662 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b 663 sta->timeout_next = STA_REMOVE;
42ca9845
JM
664 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
665 "for " MACSTR " (%d seconds - "
666 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
667 __func__, MAC2STR(sta->addr),
668 AP_MAX_INACTIVITY_AFTER_DEAUTH);
6fc6879b
JM
669 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
670 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
671 ap_handle_timer, hapd, sta);
672 accounting_sta_stop(hapd, sta);
673 ieee802_1x_free_station(sta);
674
4dc03726 675 sta->deauth_reason = reason;
cc28ad8c 676 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
677 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
678 eloop_register_timeout(hapd->iface->drv_flags &
679 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
680 ap_sta_deauth_cb_timeout, hapd, sta);
6fc6879b
JM
681}
682
683
4c374cde
AS
684#ifdef CONFIG_WPS
685int ap_sta_wps_cancel(struct hostapd_data *hapd,
686 struct sta_info *sta, void *ctx)
687{
688 if (sta && (sta->flags & WLAN_STA_WPS)) {
689 ap_sta_deauthenticate(hapd, sta,
690 WLAN_REASON_PREV_AUTH_NOT_VALID);
691 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
692 __func__, MAC2STR(sta->addr));
693 return 1;
694 }
695
696 return 0;
697}
698#endif /* CONFIG_WPS */
699
700
6fc6879b
JM
701int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
702 int old_vlanid)
703{
30b32314 704#ifndef CONFIG_NO_VLAN
6fc6879b
JM
705 const char *iface;
706 struct hostapd_vlan *vlan = NULL;
4254100d 707 int ret;
6fc6879b
JM
708
709 /*
710 * Do not proceed furthur if the vlan id remains same. We do not want
711 * duplicate dynamic vlan entries.
712 */
713 if (sta->vlan_id == old_vlanid)
714 return 0;
715
716 /*
717 * During 1x reauth, if the vlan id changes, then remove the old id and
718 * proceed furthur to add the new one.
719 */
720 if (old_vlanid > 0)
721 vlan_remove_dynamic(hapd, old_vlanid);
722
723 iface = hapd->conf->iface;
724 if (sta->ssid->vlan[0])
725 iface = sta->ssid->vlan;
726
727 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
728 sta->vlan_id = 0;
729 else if (sta->vlan_id > 0) {
c2db79f2 730 struct hostapd_vlan *wildcard_vlan = NULL;
6fc6879b
JM
731 vlan = hapd->conf->vlan;
732 while (vlan) {
c2db79f2 733 if (vlan->vlan_id == sta->vlan_id)
6fc6879b 734 break;
c2db79f2
MB
735 if (vlan->vlan_id == VLAN_ID_WILDCARD)
736 wildcard_vlan = vlan;
6fc6879b
JM
737 vlan = vlan->next;
738 }
c2db79f2
MB
739 if (!vlan)
740 vlan = wildcard_vlan;
741 if (vlan)
742 iface = vlan->ifname;
6fc6879b
JM
743 }
744
745 if (sta->vlan_id > 0 && vlan == NULL) {
746 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
747 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
748 "binding station to (vlan_id=%d)",
749 sta->vlan_id);
750 return -1;
751 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
752 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
753 if (vlan == NULL) {
754 hostapd_logger(hapd, sta->addr,
755 HOSTAPD_MODULE_IEEE80211,
756 HOSTAPD_LEVEL_DEBUG, "could not add "
757 "dynamic VLAN interface for vlan_id=%d",
758 sta->vlan_id);
759 return -1;
760 }
761
762 iface = vlan->ifname;
763 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
764 hostapd_logger(hapd, sta->addr,
765 HOSTAPD_MODULE_IEEE80211,
766 HOSTAPD_LEVEL_DEBUG, "could not "
767 "configure encryption for dynamic VLAN "
768 "interface for vlan_id=%d",
769 sta->vlan_id);
770 }
771
772 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
773 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
774 "interface '%s'", iface);
775 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
776 if (sta->vlan_id > 0) {
777 vlan->dynamic_vlan++;
778 hostapd_logger(hapd, sta->addr,
779 HOSTAPD_MODULE_IEEE80211,
780 HOSTAPD_LEVEL_DEBUG, "updated existing "
781 "dynamic VLAN interface '%s'", iface);
782 }
783
784 /*
785 * Update encryption configuration for statically generated
786 * VLAN interface. This is only used for static WEP
787 * configuration for the case where hostapd did not yet know
788 * which keys are to be used when the interface was added.
789 */
790 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
791 hostapd_logger(hapd, sta->addr,
792 HOSTAPD_MODULE_IEEE80211,
793 HOSTAPD_LEVEL_DEBUG, "could not "
794 "configure encryption for VLAN "
795 "interface for vlan_id=%d",
796 sta->vlan_id);
797 }
798 }
799
800 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
801 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
802 "'%s'", iface);
803
804 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
805 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
806
51e2a27a 807 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
4254100d
JM
808 if (ret < 0) {
809 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
810 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
811 "entry to vlan_id=%d", sta->vlan_id);
812 }
813 return ret;
30b32314
JM
814#else /* CONFIG_NO_VLAN */
815 return 0;
816#endif /* CONFIG_NO_VLAN */
6fc6879b 817}
5d22a1d5
JM
818
819
820#ifdef CONFIG_IEEE80211W
821
45c94154 822int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 823{
45c94154
JM
824 u32 tu;
825 struct os_time now, passed;
826 os_get_time(&now);
827 os_time_sub(&now, &sta->sa_query_start, &passed);
828 tu = (passed.sec * 1000000 + passed.usec) / 1024;
829 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
830 hostapd_logger(hapd, sta->addr,
831 HOSTAPD_MODULE_IEEE80211,
5d22a1d5 832 HOSTAPD_LEVEL_DEBUG,
93b76319
JM
833 "association SA Query timed out");
834 sta->sa_query_timed_out = 1;
835 os_free(sta->sa_query_trans_id);
836 sta->sa_query_trans_id = NULL;
837 sta->sa_query_count = 0;
45c94154
JM
838 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
839 return 1;
5d22a1d5
JM
840 }
841
45c94154
JM
842 return 0;
843}
844
845
846static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
847{
848 struct hostapd_data *hapd = eloop_ctx;
849 struct sta_info *sta = timeout_ctx;
850 unsigned int timeout, sec, usec;
851 u8 *trans_id, *nbuf;
852
853 if (sta->sa_query_count > 0 &&
854 ap_check_sa_query_timeout(hapd, sta))
855 return;
856
067ffa26
JM
857 nbuf = os_realloc_array(sta->sa_query_trans_id,
858 sta->sa_query_count + 1,
859 WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5
JM
860 if (nbuf == NULL)
861 return;
45c94154
JM
862 if (sta->sa_query_count == 0) {
863 /* Starting a new SA Query procedure */
864 os_get_time(&sta->sa_query_start);
865 }
93b76319
JM
866 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
867 sta->sa_query_trans_id = nbuf;
868 sta->sa_query_count++;
5d22a1d5 869
93b76319 870 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5 871
45c94154
JM
872 timeout = hapd->conf->assoc_sa_query_retry_timeout;
873 sec = ((timeout / 1000) * 1024) / 1000;
874 usec = (timeout % 1000) * 1024;
875 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
876
5d22a1d5
JM
877 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
878 HOSTAPD_LEVEL_DEBUG,
93b76319 879 "association SA Query attempt %d", sta->sa_query_count);
5d22a1d5 880
93b76319 881 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
5d22a1d5
JM
882}
883
884
93b76319 885void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 886{
93b76319 887 ap_sa_query_timer(hapd, sta);
5d22a1d5
JM
888}
889
890
93b76319 891void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 892{
93b76319
JM
893 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
894 os_free(sta->sa_query_trans_id);
895 sta->sa_query_trans_id = NULL;
896 sta->sa_query_count = 0;
5d22a1d5
JM
897}
898
899#endif /* CONFIG_IEEE80211W */
45cefa0b
JM
900
901
6905dcb1
JB
902void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
903 int authorized)
904{
10cc6c88 905 const u8 *dev_addr = NULL;
7793c959 906 char buf[100];
c2d76aa6
MH
907#ifdef CONFIG_P2P
908 u8 addr[ETH_ALEN];
909#endif /* CONFIG_P2P */
910
6905dcb1
JB
911 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
912 return;
913
ae055af4 914#ifdef CONFIG_P2P
c2d76aa6
MH
915 if (hapd->p2p_group == NULL) {
916 if (sta->p2p_ie != NULL &&
917 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
918 dev_addr = addr;
919 } else
920 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
ae055af4 921#endif /* CONFIG_P2P */
10cc6c88 922
7793c959
JM
923 if (dev_addr)
924 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
925 MAC2STR(sta->addr), MAC2STR(dev_addr));
926 else
927 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
928
10cc6c88 929 if (authorized) {
7793c959
JM
930 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s", buf);
931
8a5e75f6 932 if (hapd->msg_ctx_parent &&
7793c959 933 hapd->msg_ctx_parent != hapd->msg_ctx)
c4bf83a7
JM
934 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
935 AP_STA_CONNECTED "%s", buf);
ae055af4 936
6905dcb1 937 sta->flags |= WLAN_STA_AUTHORIZED;
ae055af4 938 } else {
7793c959
JM
939 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
940
8a5e75f6 941 if (hapd->msg_ctx_parent &&
7793c959 942 hapd->msg_ctx_parent != hapd->msg_ctx)
c4bf83a7
JM
943 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
944 AP_STA_DISCONNECTED "%s", buf);
7793c959 945
6905dcb1 946 sta->flags &= ~WLAN_STA_AUTHORIZED;
ae055af4 947 }
0661eed2
JB
948
949 if (hapd->sta_authorized_cb)
950 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
fbdcfd57 951 sta->addr, authorized, dev_addr);
6905dcb1
JB
952}
953
954
45cefa0b
JM
955void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
956 const u8 *addr, u16 reason)
957{
958
959 if (sta == NULL && addr)
960 sta = ap_get_sta(hapd, addr);
961
962 if (addr)
51e2a27a 963 hostapd_drv_sta_deauth(hapd, addr, reason);
45cefa0b
JM
964
965 if (sta == NULL)
966 return;
6905dcb1 967 ap_sta_set_authorized(hapd, sta, 0);
ceb997f3
JM
968 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
969 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
6905dcb1 970 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
42ca9845
JM
971 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
972 "for " MACSTR " (%d seconds - "
973 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
974 __func__, MAC2STR(sta->addr),
975 AP_MAX_INACTIVITY_AFTER_DEAUTH);
45cefa0b 976 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4dc03726
JM
977 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
978 ap_handle_timer, hapd, sta);
45cefa0b 979 sta->timeout_next = STA_REMOVE;
4dc03726
JM
980
981 sta->deauth_reason = reason;
cc28ad8c 982 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
983 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
984 eloop_register_timeout(hapd->iface->drv_flags &
985 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
986 ap_sta_deauth_cb_timeout, hapd, sta);
987}
988
989
990void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
991{
cc28ad8c
JM
992 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
993 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
994 return;
995 }
996 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
997 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
998 ap_sta_deauth_cb_timeout(hapd, sta);
999}
1000
1001
1002void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1003{
cc28ad8c
JM
1004 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1005 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1006 return;
1007 }
1008 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1009 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1010 ap_sta_disassoc_cb_timeout(hapd, sta);
45cefa0b 1011}