2 * hostapd / Station table
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/sae.h"
16 #include "radius/radius.h"
17 #include "radius/radius_client.h"
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
23 #include "ieee802_11_auth.h"
25 #include "preauth_auth.h"
26 #include "ap_config.h"
29 #include "vlan_init.h"
30 #include "p2p_hostapd.h"
31 #include "ap_drv_ops.h"
36 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
37 struct sta_info *sta);
38 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
39 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
40 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
41 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
42 #ifdef CONFIG_IEEE80211W
43 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
44 #endif /* CONFIG_IEEE80211W */
45 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
47 int ap_for_each_sta(struct hostapd_data *hapd,
48 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
54 for (sta = hapd->sta_list; sta; sta = sta->next) {
55 if (cb(hapd, sta, ctx))
63 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
67 s = hapd->sta_hash[STA_HASH(sta)];
68 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
75 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
79 for (sta = hapd->sta_list; sta; sta = sta->next) {
80 const u8 *p2p_dev_addr;
82 if (sta->p2p_ie == NULL)
85 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
86 if (p2p_dev_addr == NULL)
89 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
95 #endif /* CONFIG_P2P */
98 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
100 struct sta_info *tmp;
102 if (hapd->sta_list == sta) {
103 hapd->sta_list = sta->next;
107 tmp = hapd->sta_list;
108 while (tmp != NULL && tmp->next != sta)
111 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
112 "list.", MAC2STR(sta->addr));
114 tmp->next = sta->next;
118 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
120 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
121 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
125 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
129 s = hapd->sta_hash[STA_HASH(sta->addr)];
130 if (s == NULL) return;
131 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
132 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
136 while (s->hnext != NULL &&
137 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
139 if (s->hnext != NULL)
140 s->hnext = s->hnext->hnext;
142 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
143 " from hash table", MAC2STR(sta->addr));
147 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
151 accounting_sta_stop(hapd, sta);
154 ap_sta_set_authorized(hapd, sta, 0);
156 if (sta->flags & WLAN_STA_WDS)
157 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
159 if (!(sta->flags & WLAN_STA_PREAUTH))
160 hostapd_drv_sta_remove(hapd, sta->addr);
162 ap_sta_hash_del(hapd, sta);
163 ap_sta_list_del(hapd, sta);
166 hapd->sta_aid[(sta->aid - 1) / 32] &=
167 ~BIT((sta->aid - 1) % 32);
170 if (sta->nonerp_set) {
172 hapd->iface->num_sta_non_erp--;
173 if (hapd->iface->num_sta_non_erp == 0)
177 if (sta->no_short_slot_time_set) {
178 sta->no_short_slot_time_set = 0;
179 hapd->iface->num_sta_no_short_slot_time--;
180 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
181 && hapd->iface->num_sta_no_short_slot_time == 0)
185 if (sta->no_short_preamble_set) {
186 sta->no_short_preamble_set = 0;
187 hapd->iface->num_sta_no_short_preamble--;
188 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
189 && hapd->iface->num_sta_no_short_preamble == 0)
193 if (sta->no_ht_gf_set) {
194 sta->no_ht_gf_set = 0;
195 hapd->iface->num_sta_ht_no_gf--;
198 if (sta->no_ht_set) {
200 hapd->iface->num_sta_no_ht--;
203 if (sta->ht_20mhz_set) {
204 sta->ht_20mhz_set = 0;
205 hapd->iface->num_sta_ht_20mhz--;
209 if (sta->no_p2p_set) {
211 hapd->num_sta_no_p2p--;
212 if (hapd->num_sta_no_p2p == 0)
213 hostapd_p2p_non_p2p_sta_disconnected(hapd);
215 #endif /* CONFIG_P2P */
217 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
218 if (hostapd_ht_operation_update(hapd->iface) > 0)
220 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
223 ieee802_11_set_beacons(hapd->iface);
225 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
226 __func__, MAC2STR(sta->addr));
227 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
228 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
229 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
230 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
231 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
233 ieee802_1x_free_station(sta);
234 wpa_auth_sta_deinit(sta->wpa_sm);
235 rsn_preauth_free_station(hapd, sta);
236 #ifndef CONFIG_NO_RADIUS
238 radius_client_flush_auth(hapd->radius, sta->addr);
239 #endif /* CONFIG_NO_RADIUS */
241 os_free(sta->last_assoc_req);
242 os_free(sta->challenge);
244 #ifdef CONFIG_IEEE80211W
245 os_free(sta->sa_query_trans_id);
246 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
247 #endif /* CONFIG_IEEE80211W */
250 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
251 #endif /* CONFIG_P2P */
253 #ifdef CONFIG_INTERWORKING
254 if (sta->gas_dialog) {
256 for (i = 0; i < GAS_DIALOG_MAX; i++)
257 gas_serv_dialog_clear(&sta->gas_dialog[i]);
258 os_free(sta->gas_dialog);
260 #endif /* CONFIG_INTERWORKING */
262 wpabuf_free(sta->wps_ie);
263 wpabuf_free(sta->p2p_ie);
264 wpabuf_free(sta->hs20_ie);
266 os_free(sta->ht_capabilities);
267 os_free(sta->vht_capabilities);
268 hostapd_free_psk_list(sta->psk);
269 os_free(sta->identity);
270 os_free(sta->radius_cui);
271 os_free(sta->remediation_url);
272 wpabuf_free(sta->hs20_deauth_req);
273 os_free(sta->hs20_session_info_url);
276 sae_clear_data(sta->sae);
278 #endif /* CONFIG_SAE */
284 void hostapd_free_stas(struct hostapd_data *hapd)
286 struct sta_info *sta, *prev;
288 sta = hapd->sta_list;
292 if (sta->flags & WLAN_STA_AUTH) {
293 mlme_deauthenticate_indication(
294 hapd, sta, WLAN_REASON_UNSPECIFIED);
297 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
298 MAC2STR(prev->addr));
299 ap_free_sta(hapd, prev);
305 * ap_handle_timer - Per STA timer handler
306 * @eloop_ctx: struct hostapd_data *
307 * @timeout_ctx: struct sta_info *
309 * This function is called to check station activity and to remove inactive
312 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
314 struct hostapd_data *hapd = eloop_ctx;
315 struct sta_info *sta = timeout_ctx;
316 unsigned long next_time = 0;
319 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
320 __func__, MAC2STR(sta->addr), sta->flags,
322 if (sta->timeout_next == STA_REMOVE) {
323 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
324 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
325 "local deauth request");
326 ap_free_sta(hapd, sta);
330 if ((sta->flags & WLAN_STA_ASSOC) &&
331 (sta->timeout_next == STA_NULLFUNC ||
332 sta->timeout_next == STA_DISASSOC)) {
335 * Add random value to timeout so that we don't end up bouncing
336 * all stations at the same time if we have lots of associated
337 * stations that are idle (but keep re-associating).
339 int fuzz = os_random() % 20;
340 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
341 if (inactive_sec == -1) {
342 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
343 "Check inactivity: Could not "
344 "get station info from kernel driver for "
345 MACSTR, MAC2STR(sta->addr));
347 * The driver may not support this functionality.
348 * Anyway, try again after the next inactivity timeout,
349 * but do not disconnect the station now.
351 next_time = hapd->conf->ap_max_inactivity + fuzz;
352 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
353 sta->flags & WLAN_STA_ASSOC) {
354 /* station activity detected; reset timeout state */
355 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
356 "Station " MACSTR " has been active %is ago",
357 MAC2STR(sta->addr), inactive_sec);
358 sta->timeout_next = STA_NULLFUNC;
359 next_time = hapd->conf->ap_max_inactivity + fuzz -
362 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
363 "Station " MACSTR " has been "
364 "inactive too long: %d sec, max allowed: %d",
365 MAC2STR(sta->addr), inactive_sec,
366 hapd->conf->ap_max_inactivity);
368 if (hapd->conf->skip_inactivity_poll)
369 sta->timeout_next = STA_DISASSOC;
373 if ((sta->flags & WLAN_STA_ASSOC) &&
374 sta->timeout_next == STA_DISASSOC &&
375 !(sta->flags & WLAN_STA_PENDING_POLL) &&
376 !hapd->conf->skip_inactivity_poll) {
377 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
378 " has ACKed data poll", MAC2STR(sta->addr));
379 /* data nullfunc frame poll did not produce TX errors; assume
380 * station ACKed it */
381 sta->timeout_next = STA_NULLFUNC;
382 next_time = hapd->conf->ap_max_inactivity;
386 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
387 "for " MACSTR " (%lu seconds)",
388 __func__, MAC2STR(sta->addr), next_time);
389 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
394 if (sta->timeout_next == STA_NULLFUNC &&
395 (sta->flags & WLAN_STA_ASSOC)) {
396 wpa_printf(MSG_DEBUG, " Polling STA");
397 sta->flags |= WLAN_STA_PENDING_POLL;
398 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
399 sta->flags & WLAN_STA_WMM);
400 } else if (sta->timeout_next != STA_REMOVE) {
401 int deauth = sta->timeout_next == STA_DEAUTH;
403 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
404 "Timeout, sending %s info to STA " MACSTR,
405 deauth ? "deauthentication" : "disassociation",
409 hostapd_drv_sta_deauth(
411 WLAN_REASON_PREV_AUTH_NOT_VALID);
413 reason = (sta->timeout_next == STA_DISASSOC) ?
414 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
415 WLAN_REASON_PREV_AUTH_NOT_VALID;
417 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
421 switch (sta->timeout_next) {
423 sta->timeout_next = STA_DISASSOC;
424 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
425 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
426 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
427 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
431 case STA_DISASSOC_FROM_CLI:
432 ap_sta_set_authorized(hapd, sta, 0);
433 sta->flags &= ~WLAN_STA_ASSOC;
434 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
435 if (!sta->acct_terminate_cause)
436 sta->acct_terminate_cause =
437 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
438 accounting_sta_stop(hapd, sta);
439 ieee802_1x_free_station(sta);
440 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
441 HOSTAPD_LEVEL_INFO, "disassociated due to "
443 reason = (sta->timeout_next == STA_DISASSOC) ?
444 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
445 WLAN_REASON_PREV_AUTH_NOT_VALID;
446 sta->timeout_next = STA_DEAUTH;
447 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
448 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
449 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
450 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
452 mlme_disassociate_indication(hapd, sta, reason);
456 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
457 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
458 "inactivity (timer DEAUTH/REMOVE)");
459 if (!sta->acct_terminate_cause)
460 sta->acct_terminate_cause =
461 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
462 mlme_deauthenticate_indication(
464 WLAN_REASON_PREV_AUTH_NOT_VALID);
465 ap_free_sta(hapd, sta);
471 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
473 struct hostapd_data *hapd = eloop_ctx;
474 struct sta_info *sta = timeout_ctx;
477 if (!(sta->flags & WLAN_STA_AUTH)) {
478 if (sta->flags & WLAN_STA_GAS) {
479 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
480 "entry " MACSTR, MAC2STR(sta->addr));
481 ap_free_sta(hapd, sta);
486 mlme_deauthenticate_indication(hapd, sta,
487 WLAN_REASON_PREV_AUTH_NOT_VALID);
488 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
489 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
491 sta->acct_terminate_cause =
492 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
493 os_memcpy(addr, sta->addr, ETH_ALEN);
494 ap_free_sta(hapd, sta);
495 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
499 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
502 if (eloop_replenish_timeout(session_timeout, 0,
503 ap_handle_session_timer, hapd, sta) == 1) {
504 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
505 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
506 "to %d seconds", session_timeout);
511 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
514 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
515 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
516 "seconds", session_timeout);
517 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
518 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
523 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
525 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
529 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
532 struct hostapd_data *hapd = eloop_ctx;
533 struct sta_info *sta = timeout_ctx;
535 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
537 if (sta->hs20_session_info_url == NULL)
540 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
541 sta->hs20_disassoc_timer);
542 #endif /* CONFIG_WNM */
546 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
547 struct sta_info *sta, int warning_time)
549 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
550 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
555 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
557 struct sta_info *sta;
559 sta = ap_get_sta(hapd, addr);
563 wpa_printf(MSG_DEBUG, " New STA");
564 if (hapd->num_sta >= hapd->conf->max_num_sta) {
565 /* FIX: might try to remove some old STAs first? */
566 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
567 hapd->num_sta, hapd->conf->max_num_sta);
571 sta = os_zalloc(sizeof(struct sta_info));
573 wpa_printf(MSG_ERROR, "malloc failed");
576 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
577 accounting_sta_get_id(hapd, sta);
579 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
580 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
581 "for " MACSTR " (%d seconds - ap_max_inactivity)",
582 __func__, MAC2STR(addr),
583 hapd->conf->ap_max_inactivity);
584 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
585 ap_handle_timer, hapd, sta);
588 /* initialize STA info data */
589 os_memcpy(sta->addr, addr, ETH_ALEN);
590 sta->next = hapd->sta_list;
591 hapd->sta_list = sta;
593 ap_sta_hash_add(hapd, sta);
594 sta->ssid = &hapd->conf->ssid;
595 ap_sta_remove_in_other_bss(hapd, sta);
601 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
603 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
605 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
607 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
608 sta->flags & WLAN_STA_ASSOC) {
609 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
610 " from kernel driver.", MAC2STR(sta->addr));
617 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
618 struct sta_info *sta)
620 struct hostapd_iface *iface = hapd->iface;
623 for (i = 0; i < iface->num_bss; i++) {
624 struct hostapd_data *bss = iface->bss[i];
625 struct sta_info *sta2;
626 /* bss should always be set during operation, but it may be
627 * NULL during reconfiguration. Assume the STA is not
628 * associated to another BSS in that case to avoid NULL pointer
630 if (bss == hapd || bss == NULL)
632 sta2 = ap_get_sta(bss, sta->addr);
636 ap_sta_disconnect(bss, sta2, sta2->addr,
637 WLAN_REASON_PREV_AUTH_NOT_VALID);
642 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
644 struct hostapd_data *hapd = eloop_ctx;
645 struct sta_info *sta = timeout_ctx;
647 ap_sta_remove(hapd, sta);
648 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
652 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
655 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
656 hapd->conf->iface, MAC2STR(sta->addr));
657 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
658 ap_sta_set_authorized(hapd, sta, 0);
659 sta->timeout_next = STA_DEAUTH;
660 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
661 "for " MACSTR " (%d seconds - "
662 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
663 __func__, MAC2STR(sta->addr),
664 AP_MAX_INACTIVITY_AFTER_DISASSOC);
665 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
666 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
667 ap_handle_timer, hapd, sta);
668 accounting_sta_stop(hapd, sta);
669 ieee802_1x_free_station(sta);
671 sta->disassoc_reason = reason;
672 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
673 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
674 eloop_register_timeout(hapd->iface->drv_flags &
675 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
676 ap_sta_disassoc_cb_timeout, hapd, sta);
680 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
682 struct hostapd_data *hapd = eloop_ctx;
683 struct sta_info *sta = timeout_ctx;
685 ap_sta_remove(hapd, sta);
686 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
690 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
693 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
694 hapd->conf->iface, MAC2STR(sta->addr));
695 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
696 ap_sta_set_authorized(hapd, sta, 0);
697 sta->timeout_next = STA_REMOVE;
698 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
699 "for " MACSTR " (%d seconds - "
700 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
701 __func__, MAC2STR(sta->addr),
702 AP_MAX_INACTIVITY_AFTER_DEAUTH);
703 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
704 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
705 ap_handle_timer, hapd, sta);
706 accounting_sta_stop(hapd, sta);
707 ieee802_1x_free_station(sta);
709 sta->deauth_reason = reason;
710 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
711 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
712 eloop_register_timeout(hapd->iface->drv_flags &
713 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
714 ap_sta_deauth_cb_timeout, hapd, sta);
719 int ap_sta_wps_cancel(struct hostapd_data *hapd,
720 struct sta_info *sta, void *ctx)
722 if (sta && (sta->flags & WLAN_STA_WPS)) {
723 ap_sta_deauthenticate(hapd, sta,
724 WLAN_REASON_PREV_AUTH_NOT_VALID);
725 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
726 __func__, MAC2STR(sta->addr));
732 #endif /* CONFIG_WPS */
735 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
738 #ifndef CONFIG_NO_VLAN
740 struct hostapd_vlan *vlan = NULL;
744 * Do not proceed furthur if the vlan id remains same. We do not want
745 * duplicate dynamic vlan entries.
747 if (sta->vlan_id == old_vlanid)
751 * During 1x reauth, if the vlan id changes, then remove the old id and
752 * proceed furthur to add the new one.
755 vlan_remove_dynamic(hapd, old_vlanid);
757 iface = hapd->conf->iface;
758 if (sta->ssid->vlan[0])
759 iface = sta->ssid->vlan;
761 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
763 else if (sta->vlan_id > 0) {
764 struct hostapd_vlan *wildcard_vlan = NULL;
765 vlan = hapd->conf->vlan;
767 if (vlan->vlan_id == sta->vlan_id)
769 if (vlan->vlan_id == VLAN_ID_WILDCARD)
770 wildcard_vlan = vlan;
774 vlan = wildcard_vlan;
776 iface = vlan->ifname;
779 if (sta->vlan_id > 0 && vlan == NULL) {
780 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
781 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
782 "binding station to (vlan_id=%d)",
785 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
786 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
788 hostapd_logger(hapd, sta->addr,
789 HOSTAPD_MODULE_IEEE80211,
790 HOSTAPD_LEVEL_DEBUG, "could not add "
791 "dynamic VLAN interface for vlan_id=%d",
796 iface = vlan->ifname;
797 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
798 hostapd_logger(hapd, sta->addr,
799 HOSTAPD_MODULE_IEEE80211,
800 HOSTAPD_LEVEL_DEBUG, "could not "
801 "configure encryption for dynamic VLAN "
802 "interface for vlan_id=%d",
806 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
807 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
808 "interface '%s'", iface);
809 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
810 if (sta->vlan_id > 0) {
811 vlan->dynamic_vlan++;
812 hostapd_logger(hapd, sta->addr,
813 HOSTAPD_MODULE_IEEE80211,
814 HOSTAPD_LEVEL_DEBUG, "updated existing "
815 "dynamic VLAN interface '%s'", iface);
819 * Update encryption configuration for statically generated
820 * VLAN interface. This is only used for static WEP
821 * configuration for the case where hostapd did not yet know
822 * which keys are to be used when the interface was added.
824 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
825 hostapd_logger(hapd, sta->addr,
826 HOSTAPD_MODULE_IEEE80211,
827 HOSTAPD_LEVEL_DEBUG, "could not "
828 "configure encryption for VLAN "
829 "interface for vlan_id=%d",
834 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
835 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
838 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
839 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
841 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
843 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
844 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
845 "entry to vlan_id=%d", sta->vlan_id);
848 #else /* CONFIG_NO_VLAN */
850 #endif /* CONFIG_NO_VLAN */
854 #ifdef CONFIG_IEEE80211W
856 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
859 struct os_reltime now, passed;
860 os_get_reltime(&now);
861 os_reltime_sub(&now, &sta->sa_query_start, &passed);
862 tu = (passed.sec * 1000000 + passed.usec) / 1024;
863 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
864 hostapd_logger(hapd, sta->addr,
865 HOSTAPD_MODULE_IEEE80211,
867 "association SA Query timed out");
868 sta->sa_query_timed_out = 1;
869 os_free(sta->sa_query_trans_id);
870 sta->sa_query_trans_id = NULL;
871 sta->sa_query_count = 0;
872 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
880 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
882 struct hostapd_data *hapd = eloop_ctx;
883 struct sta_info *sta = timeout_ctx;
884 unsigned int timeout, sec, usec;
887 if (sta->sa_query_count > 0 &&
888 ap_check_sa_query_timeout(hapd, sta))
891 nbuf = os_realloc_array(sta->sa_query_trans_id,
892 sta->sa_query_count + 1,
893 WLAN_SA_QUERY_TR_ID_LEN);
896 if (sta->sa_query_count == 0) {
897 /* Starting a new SA Query procedure */
898 os_get_reltime(&sta->sa_query_start);
900 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
901 sta->sa_query_trans_id = nbuf;
902 sta->sa_query_count++;
904 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
906 timeout = hapd->conf->assoc_sa_query_retry_timeout;
907 sec = ((timeout / 1000) * 1024) / 1000;
908 usec = (timeout % 1000) * 1024;
909 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
911 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
913 "association SA Query attempt %d", sta->sa_query_count);
915 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
919 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
921 ap_sa_query_timer(hapd, sta);
925 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
927 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
928 os_free(sta->sa_query_trans_id);
929 sta->sa_query_trans_id = NULL;
930 sta->sa_query_count = 0;
933 #endif /* CONFIG_IEEE80211W */
936 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
939 const u8 *dev_addr = NULL;
944 #endif /* CONFIG_P2P */
946 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
950 if (hapd->p2p_group == NULL) {
951 if (sta->p2p_ie != NULL &&
952 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
955 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
956 #endif /* CONFIG_P2P */
959 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
960 MAC2STR(sta->addr), MAC2STR(dev_addr));
962 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
968 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
969 os_snprintf(ip_addr, sizeof(ip_addr),
970 " ip_addr=%u.%u.%u.%u",
971 ip_addr_buf[0], ip_addr_buf[1],
972 ip_addr_buf[2], ip_addr_buf[3]);
974 #endif /* CONFIG_P2P */
976 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
979 if (hapd->msg_ctx_parent &&
980 hapd->msg_ctx_parent != hapd->msg_ctx)
981 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
982 AP_STA_CONNECTED "%s%s",
985 sta->flags |= WLAN_STA_AUTHORIZED;
987 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
989 if (hapd->msg_ctx_parent &&
990 hapd->msg_ctx_parent != hapd->msg_ctx)
991 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
992 AP_STA_DISCONNECTED "%s", buf);
994 sta->flags &= ~WLAN_STA_AUTHORIZED;
997 if (hapd->sta_authorized_cb)
998 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
999 sta->addr, authorized, dev_addr);
1003 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1004 const u8 *addr, u16 reason)
1007 if (sta == NULL && addr)
1008 sta = ap_get_sta(hapd, addr);
1011 hostapd_drv_sta_deauth(hapd, addr, reason);
1015 ap_sta_set_authorized(hapd, sta, 0);
1016 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1017 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1018 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1019 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1020 "for " MACSTR " (%d seconds - "
1021 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1022 __func__, MAC2STR(sta->addr),
1023 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1024 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1025 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1026 ap_handle_timer, hapd, sta);
1027 sta->timeout_next = STA_REMOVE;
1029 sta->deauth_reason = reason;
1030 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1031 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1032 eloop_register_timeout(hapd->iface->drv_flags &
1033 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1034 ap_sta_deauth_cb_timeout, hapd, sta);
1038 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1040 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1041 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1044 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1045 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1046 ap_sta_deauth_cb_timeout(hapd, sta);
1050 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1052 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1053 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1056 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1057 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1058 ap_sta_disassoc_cb_timeout(hapd, sta);
1062 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1067 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1068 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1069 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1070 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1071 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1073 (flags & WLAN_STA_SHORT_PREAMBLE ?
1074 "[SHORT_PREAMBLE]" : ""),
1075 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1076 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1077 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1078 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1079 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1080 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1081 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1082 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1083 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1084 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1085 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1086 "[WNM_SLEEP_MODE]" : ""));