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