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"
21 #include "accounting.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "ieee802_11_auth.h"
26 #include "preauth_auth.h"
27 #include "ap_config.h"
30 #include "vlan_init.h"
31 #include "p2p_hostapd.h"
32 #include "ap_drv_ops.h"
35 #include "ndisc_snoop.h"
38 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
39 struct sta_info *sta);
40 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
41 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
42 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
44 #ifdef CONFIG_IEEE80211W
45 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
46 #endif /* CONFIG_IEEE80211W */
47 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
49 int ap_for_each_sta(struct hostapd_data *hapd,
50 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
56 for (sta = hapd->sta_list; sta; sta = sta->next) {
57 if (cb(hapd, sta, ctx))
65 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
69 s = hapd->sta_hash[STA_HASH(sta)];
70 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
77 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
81 for (sta = hapd->sta_list; sta; sta = sta->next) {
82 const u8 *p2p_dev_addr;
84 if (sta->p2p_ie == NULL)
87 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
88 if (p2p_dev_addr == NULL)
91 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
97 #endif /* CONFIG_P2P */
100 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
102 struct sta_info *tmp;
104 if (hapd->sta_list == sta) {
105 hapd->sta_list = sta->next;
109 tmp = hapd->sta_list;
110 while (tmp != NULL && tmp->next != sta)
113 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
114 "list.", MAC2STR(sta->addr));
116 tmp->next = sta->next;
120 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
122 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
123 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
127 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
131 s = hapd->sta_hash[STA_HASH(sta->addr)];
132 if (s == NULL) return;
133 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
134 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
138 while (s->hnext != NULL &&
139 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
141 if (s->hnext != NULL)
142 s->hnext = s->hnext->hnext;
144 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
145 " from hash table", MAC2STR(sta->addr));
149 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
151 sta_ip6addr_del(hapd, sta);
155 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
159 accounting_sta_stop(hapd, sta);
162 ap_sta_set_authorized(hapd, sta, 0);
164 if (sta->flags & WLAN_STA_WDS)
165 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
168 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
169 ap_sta_ip6addr_del(hapd, sta);
171 if (!hapd->iface->driver_ap_teardown &&
172 !(sta->flags & WLAN_STA_PREAUTH))
173 hostapd_drv_sta_remove(hapd, sta->addr);
175 ap_sta_hash_del(hapd, sta);
176 ap_sta_list_del(hapd, sta);
179 hapd->sta_aid[(sta->aid - 1) / 32] &=
180 ~BIT((sta->aid - 1) % 32);
183 if (sta->nonerp_set) {
185 hapd->iface->num_sta_non_erp--;
186 if (hapd->iface->num_sta_non_erp == 0)
190 if (sta->no_short_slot_time_set) {
191 sta->no_short_slot_time_set = 0;
192 hapd->iface->num_sta_no_short_slot_time--;
193 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
194 && hapd->iface->num_sta_no_short_slot_time == 0)
198 if (sta->no_short_preamble_set) {
199 sta->no_short_preamble_set = 0;
200 hapd->iface->num_sta_no_short_preamble--;
201 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
202 && hapd->iface->num_sta_no_short_preamble == 0)
206 if (sta->no_ht_gf_set) {
207 sta->no_ht_gf_set = 0;
208 hapd->iface->num_sta_ht_no_gf--;
211 if (sta->no_ht_set) {
213 hapd->iface->num_sta_no_ht--;
216 if (sta->ht_20mhz_set) {
217 sta->ht_20mhz_set = 0;
218 hapd->iface->num_sta_ht_20mhz--;
221 #ifdef CONFIG_IEEE80211N
222 ht40_intolerant_remove(hapd->iface, sta);
223 #endif /* CONFIG_IEEE80211N */
226 if (sta->no_p2p_set) {
228 hapd->num_sta_no_p2p--;
229 if (hapd->num_sta_no_p2p == 0)
230 hostapd_p2p_non_p2p_sta_disconnected(hapd);
232 #endif /* CONFIG_P2P */
234 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
235 if (hostapd_ht_operation_update(hapd->iface) > 0)
237 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
240 if (hapd->mesh_sta_free_cb)
241 hapd->mesh_sta_free_cb(sta);
242 #endif /* CONFIG_MESH */
245 ieee802_11_set_beacons(hapd->iface);
247 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
248 __func__, MAC2STR(sta->addr));
249 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
250 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
251 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
252 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
253 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
254 sae_clear_retransmit_timer(hapd, sta);
256 ieee802_1x_free_station(sta);
257 wpa_auth_sta_deinit(sta->wpa_sm);
258 rsn_preauth_free_station(hapd, sta);
259 #ifndef CONFIG_NO_RADIUS
261 radius_client_flush_auth(hapd->radius, sta->addr);
262 #endif /* CONFIG_NO_RADIUS */
264 #ifndef CONFIG_NO_VLAN
266 * sta->wpa_sm->group needs to be released before so that
267 * vlan_remove_dynamic() can check that no stations are left on the
270 if (sta->vlan_id_bound) {
272 * Need to remove the STA entry before potentially removing the
275 if (hapd->iface->driver_ap_teardown &&
276 !(sta->flags & WLAN_STA_PREAUTH))
277 hostapd_drv_sta_remove(hapd, sta->addr);
278 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
280 #endif /* CONFIG_NO_VLAN */
282 os_free(sta->challenge);
284 #ifdef CONFIG_IEEE80211W
285 os_free(sta->sa_query_trans_id);
286 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
287 #endif /* CONFIG_IEEE80211W */
290 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
291 #endif /* CONFIG_P2P */
293 #ifdef CONFIG_INTERWORKING
294 if (sta->gas_dialog) {
296 for (i = 0; i < GAS_DIALOG_MAX; i++)
297 gas_serv_dialog_clear(&sta->gas_dialog[i]);
298 os_free(sta->gas_dialog);
300 #endif /* CONFIG_INTERWORKING */
302 wpabuf_free(sta->wps_ie);
303 wpabuf_free(sta->p2p_ie);
304 wpabuf_free(sta->hs20_ie);
306 wpabuf_free(sta->mb_ies);
307 #endif /* CONFIG_FST */
309 os_free(sta->ht_capabilities);
310 os_free(sta->vht_capabilities);
311 hostapd_free_psk_list(sta->psk);
312 os_free(sta->identity);
313 os_free(sta->radius_cui);
314 os_free(sta->remediation_url);
315 wpabuf_free(sta->hs20_deauth_req);
316 os_free(sta->hs20_session_info_url);
319 sae_clear_data(sta->sae);
321 #endif /* CONFIG_SAE */
327 void hostapd_free_stas(struct hostapd_data *hapd)
329 struct sta_info *sta, *prev;
331 sta = hapd->sta_list;
335 if (sta->flags & WLAN_STA_AUTH) {
336 mlme_deauthenticate_indication(
337 hapd, sta, WLAN_REASON_UNSPECIFIED);
340 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
341 MAC2STR(prev->addr));
342 ap_free_sta(hapd, prev);
348 * ap_handle_timer - Per STA timer handler
349 * @eloop_ctx: struct hostapd_data *
350 * @timeout_ctx: struct sta_info *
352 * This function is called to check station activity and to remove inactive
355 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
357 struct hostapd_data *hapd = eloop_ctx;
358 struct sta_info *sta = timeout_ctx;
359 unsigned long next_time = 0;
362 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
363 __func__, MAC2STR(sta->addr), sta->flags,
365 if (sta->timeout_next == STA_REMOVE) {
366 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
367 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
368 "local deauth request");
369 ap_free_sta(hapd, sta);
373 if ((sta->flags & WLAN_STA_ASSOC) &&
374 (sta->timeout_next == STA_NULLFUNC ||
375 sta->timeout_next == STA_DISASSOC)) {
378 * Add random value to timeout so that we don't end up bouncing
379 * all stations at the same time if we have lots of associated
380 * stations that are idle (but keep re-associating).
382 int fuzz = os_random() % 20;
383 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
384 if (inactive_sec == -1) {
385 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
386 "Check inactivity: Could not "
387 "get station info from kernel driver for "
388 MACSTR, MAC2STR(sta->addr));
390 * The driver may not support this functionality.
391 * Anyway, try again after the next inactivity timeout,
392 * but do not disconnect the station now.
394 next_time = hapd->conf->ap_max_inactivity + fuzz;
395 } else if (inactive_sec == -ENOENT) {
396 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
397 "Station " MACSTR " has lost its driver entry",
400 /* Avoid sending client probe on removed client */
401 sta->timeout_next = STA_DISASSOC;
403 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
404 /* station activity detected; reset timeout state */
405 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
406 "Station " MACSTR " has been active %is ago",
407 MAC2STR(sta->addr), inactive_sec);
408 sta->timeout_next = STA_NULLFUNC;
409 next_time = hapd->conf->ap_max_inactivity + fuzz -
412 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
413 "Station " MACSTR " has been "
414 "inactive too long: %d sec, max allowed: %d",
415 MAC2STR(sta->addr), inactive_sec,
416 hapd->conf->ap_max_inactivity);
418 if (hapd->conf->skip_inactivity_poll)
419 sta->timeout_next = STA_DISASSOC;
423 if ((sta->flags & WLAN_STA_ASSOC) &&
424 sta->timeout_next == STA_DISASSOC &&
425 !(sta->flags & WLAN_STA_PENDING_POLL) &&
426 !hapd->conf->skip_inactivity_poll) {
427 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
428 " has ACKed data poll", MAC2STR(sta->addr));
429 /* data nullfunc frame poll did not produce TX errors; assume
430 * station ACKed it */
431 sta->timeout_next = STA_NULLFUNC;
432 next_time = hapd->conf->ap_max_inactivity;
437 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
438 "for " MACSTR " (%lu seconds)",
439 __func__, MAC2STR(sta->addr), next_time);
440 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
445 if (sta->timeout_next == STA_NULLFUNC &&
446 (sta->flags & WLAN_STA_ASSOC)) {
447 wpa_printf(MSG_DEBUG, " Polling STA");
448 sta->flags |= WLAN_STA_PENDING_POLL;
449 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
450 sta->flags & WLAN_STA_WMM);
451 } else if (sta->timeout_next != STA_REMOVE) {
452 int deauth = sta->timeout_next == STA_DEAUTH;
454 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
455 "Timeout, sending %s info to STA " MACSTR,
456 deauth ? "deauthentication" : "disassociation",
460 hostapd_drv_sta_deauth(
462 WLAN_REASON_PREV_AUTH_NOT_VALID);
464 reason = (sta->timeout_next == STA_DISASSOC) ?
465 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
466 WLAN_REASON_PREV_AUTH_NOT_VALID;
468 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
472 switch (sta->timeout_next) {
474 sta->timeout_next = STA_DISASSOC;
475 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
476 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
477 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
478 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
482 case STA_DISASSOC_FROM_CLI:
483 ap_sta_set_authorized(hapd, sta, 0);
484 sta->flags &= ~WLAN_STA_ASSOC;
485 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
486 if (!sta->acct_terminate_cause)
487 sta->acct_terminate_cause =
488 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
489 accounting_sta_stop(hapd, sta);
490 ieee802_1x_free_station(sta);
491 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
492 HOSTAPD_LEVEL_INFO, "disassociated due to "
494 reason = (sta->timeout_next == STA_DISASSOC) ?
495 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
496 WLAN_REASON_PREV_AUTH_NOT_VALID;
497 sta->timeout_next = STA_DEAUTH;
498 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
499 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
500 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
501 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
503 mlme_disassociate_indication(hapd, sta, reason);
507 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
508 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
509 "inactivity (timer DEAUTH/REMOVE)");
510 if (!sta->acct_terminate_cause)
511 sta->acct_terminate_cause =
512 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
513 mlme_deauthenticate_indication(
515 WLAN_REASON_PREV_AUTH_NOT_VALID);
516 ap_free_sta(hapd, sta);
522 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
524 struct hostapd_data *hapd = eloop_ctx;
525 struct sta_info *sta = timeout_ctx;
527 if (!(sta->flags & WLAN_STA_AUTH)) {
528 if (sta->flags & WLAN_STA_GAS) {
529 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
530 "entry " MACSTR, MAC2STR(sta->addr));
531 ap_free_sta(hapd, sta);
536 hostapd_drv_sta_deauth(hapd, sta->addr,
537 WLAN_REASON_PREV_AUTH_NOT_VALID);
538 mlme_deauthenticate_indication(hapd, sta,
539 WLAN_REASON_PREV_AUTH_NOT_VALID);
540 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
541 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
543 sta->acct_terminate_cause =
544 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
545 ap_free_sta(hapd, sta);
549 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
552 if (eloop_replenish_timeout(session_timeout, 0,
553 ap_handle_session_timer, hapd, sta) == 1) {
554 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
555 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
556 "to %d seconds", session_timeout);
561 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
564 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
565 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
566 "seconds", session_timeout);
567 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
568 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
573 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
575 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
579 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
582 struct hostapd_data *hapd = eloop_ctx;
583 struct sta_info *sta = timeout_ctx;
585 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
587 if (sta->hs20_session_info_url == NULL)
590 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
591 sta->hs20_disassoc_timer);
592 #endif /* CONFIG_WNM */
596 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
597 struct sta_info *sta, int warning_time)
599 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
600 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
605 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
607 struct sta_info *sta;
609 sta = ap_get_sta(hapd, addr);
613 wpa_printf(MSG_DEBUG, " New STA");
614 if (hapd->num_sta >= hapd->conf->max_num_sta) {
615 /* FIX: might try to remove some old STAs first? */
616 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
617 hapd->num_sta, hapd->conf->max_num_sta);
621 sta = os_zalloc(sizeof(struct sta_info));
623 wpa_printf(MSG_ERROR, "malloc failed");
626 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
627 accounting_sta_get_id(hapd, sta);
629 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
630 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
631 "for " MACSTR " (%d seconds - ap_max_inactivity)",
632 __func__, MAC2STR(addr),
633 hapd->conf->ap_max_inactivity);
634 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
635 ap_handle_timer, hapd, sta);
638 /* initialize STA info data */
639 os_memcpy(sta->addr, addr, ETH_ALEN);
640 sta->next = hapd->sta_list;
641 hapd->sta_list = sta;
643 ap_sta_hash_add(hapd, sta);
644 ap_sta_remove_in_other_bss(hapd, sta);
645 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
646 dl_list_init(&sta->ip6addr);
652 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
654 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
657 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
658 ap_sta_ip6addr_del(hapd, sta);
660 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
662 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
663 sta->flags & WLAN_STA_ASSOC) {
664 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
665 " from kernel driver.", MAC2STR(sta->addr));
672 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
673 struct sta_info *sta)
675 struct hostapd_iface *iface = hapd->iface;
678 for (i = 0; i < iface->num_bss; i++) {
679 struct hostapd_data *bss = iface->bss[i];
680 struct sta_info *sta2;
681 /* bss should always be set during operation, but it may be
682 * NULL during reconfiguration. Assume the STA is not
683 * associated to another BSS in that case to avoid NULL pointer
685 if (bss == hapd || bss == NULL)
687 sta2 = ap_get_sta(bss, sta->addr);
691 ap_sta_disconnect(bss, sta2, sta2->addr,
692 WLAN_REASON_PREV_AUTH_NOT_VALID);
697 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
699 struct hostapd_data *hapd = eloop_ctx;
700 struct sta_info *sta = timeout_ctx;
702 ap_sta_remove(hapd, sta);
703 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
707 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
710 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
711 hapd->conf->iface, MAC2STR(sta->addr));
712 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
713 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
714 ap_sta_set_authorized(hapd, sta, 0);
715 sta->timeout_next = STA_DEAUTH;
716 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
717 "for " MACSTR " (%d seconds - "
718 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
719 __func__, MAC2STR(sta->addr),
720 AP_MAX_INACTIVITY_AFTER_DISASSOC);
721 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
722 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
723 ap_handle_timer, hapd, sta);
724 accounting_sta_stop(hapd, sta);
725 ieee802_1x_free_station(sta);
727 sta->disassoc_reason = reason;
728 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
729 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
730 eloop_register_timeout(hapd->iface->drv_flags &
731 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
732 ap_sta_disassoc_cb_timeout, hapd, sta);
736 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
738 struct hostapd_data *hapd = eloop_ctx;
739 struct sta_info *sta = timeout_ctx;
741 ap_sta_remove(hapd, sta);
742 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
746 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
749 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
750 hapd->conf->iface, MAC2STR(sta->addr));
751 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
752 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
753 ap_sta_set_authorized(hapd, sta, 0);
754 sta->timeout_next = STA_REMOVE;
755 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
756 "for " MACSTR " (%d seconds - "
757 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
758 __func__, MAC2STR(sta->addr),
759 AP_MAX_INACTIVITY_AFTER_DEAUTH);
760 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
761 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
762 ap_handle_timer, hapd, sta);
763 accounting_sta_stop(hapd, sta);
764 ieee802_1x_free_station(sta);
766 sta->deauth_reason = reason;
767 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
768 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
769 eloop_register_timeout(hapd->iface->drv_flags &
770 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
771 ap_sta_deauth_cb_timeout, hapd, sta);
776 int ap_sta_wps_cancel(struct hostapd_data *hapd,
777 struct sta_info *sta, void *ctx)
779 if (sta && (sta->flags & WLAN_STA_WPS)) {
780 ap_sta_deauthenticate(hapd, sta,
781 WLAN_REASON_PREV_AUTH_NOT_VALID);
782 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
783 __func__, MAC2STR(sta->addr));
789 #endif /* CONFIG_WPS */
792 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
794 #ifndef CONFIG_NO_VLAN
796 struct hostapd_vlan *vlan = NULL;
798 int old_vlanid = sta->vlan_id_bound;
800 iface = hapd->conf->iface;
801 if (hapd->conf->ssid.vlan[0])
802 iface = hapd->conf->ssid.vlan;
804 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
806 else if (sta->vlan_id > 0) {
807 struct hostapd_vlan *wildcard_vlan = NULL;
808 vlan = hapd->conf->vlan;
810 if (vlan->vlan_id == sta->vlan_id)
812 if (vlan->vlan_id == VLAN_ID_WILDCARD)
813 wildcard_vlan = vlan;
817 vlan = wildcard_vlan;
819 iface = vlan->ifname;
823 * Do not increment ref counters if the VLAN ID remains same, but do
824 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
825 * have been called before.
827 if (sta->vlan_id == old_vlanid)
830 if (sta->vlan_id > 0 && vlan == NULL) {
831 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
832 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
833 "binding station to (vlan_id=%d)",
837 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
838 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
840 hostapd_logger(hapd, sta->addr,
841 HOSTAPD_MODULE_IEEE80211,
842 HOSTAPD_LEVEL_DEBUG, "could not add "
843 "dynamic VLAN interface for vlan_id=%d",
849 iface = vlan->ifname;
850 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
851 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
852 "interface '%s'", iface);
853 } else if (vlan && vlan->vlan_id == sta->vlan_id &&
854 vlan->dynamic_vlan > 0) {
855 vlan->dynamic_vlan++;
856 hostapd_logger(hapd, sta->addr,
857 HOSTAPD_MODULE_IEEE80211,
859 "updated existing dynamic VLAN interface '%s'",
863 /* ref counters have been increased, so mark the station */
864 sta->vlan_id_bound = sta->vlan_id;
867 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
868 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
871 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
872 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
874 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
876 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
877 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
878 "entry to vlan_id=%d", sta->vlan_id);
881 /* During 1x reauth, if the vlan id changes, then remove the old id. */
882 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
883 vlan_remove_dynamic(hapd, old_vlanid);
887 #else /* CONFIG_NO_VLAN */
889 #endif /* CONFIG_NO_VLAN */
893 #ifdef CONFIG_IEEE80211W
895 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
898 struct os_reltime now, passed;
899 os_get_reltime(&now);
900 os_reltime_sub(&now, &sta->sa_query_start, &passed);
901 tu = (passed.sec * 1000000 + passed.usec) / 1024;
902 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
903 hostapd_logger(hapd, sta->addr,
904 HOSTAPD_MODULE_IEEE80211,
906 "association SA Query timed out");
907 sta->sa_query_timed_out = 1;
908 os_free(sta->sa_query_trans_id);
909 sta->sa_query_trans_id = NULL;
910 sta->sa_query_count = 0;
911 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
919 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
921 struct hostapd_data *hapd = eloop_ctx;
922 struct sta_info *sta = timeout_ctx;
923 unsigned int timeout, sec, usec;
926 if (sta->sa_query_count > 0 &&
927 ap_check_sa_query_timeout(hapd, sta))
930 nbuf = os_realloc_array(sta->sa_query_trans_id,
931 sta->sa_query_count + 1,
932 WLAN_SA_QUERY_TR_ID_LEN);
935 if (sta->sa_query_count == 0) {
936 /* Starting a new SA Query procedure */
937 os_get_reltime(&sta->sa_query_start);
939 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
940 sta->sa_query_trans_id = nbuf;
941 sta->sa_query_count++;
943 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
945 * We don't really care which ID is used here, so simply
946 * hardcode this if the mostly theoretical os_get_random()
953 timeout = hapd->conf->assoc_sa_query_retry_timeout;
954 sec = ((timeout / 1000) * 1024) / 1000;
955 usec = (timeout % 1000) * 1024;
956 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
958 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
960 "association SA Query attempt %d", sta->sa_query_count);
962 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
966 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
968 ap_sa_query_timer(hapd, sta);
972 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
974 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
975 os_free(sta->sa_query_trans_id);
976 sta->sa_query_trans_id = NULL;
977 sta->sa_query_count = 0;
980 #endif /* CONFIG_IEEE80211W */
983 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
986 const u8 *dev_addr = NULL;
991 #endif /* CONFIG_P2P */
993 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
997 sta->flags |= WLAN_STA_AUTHORIZED;
999 sta->flags &= ~WLAN_STA_AUTHORIZED;
1002 if (hapd->p2p_group == NULL) {
1003 if (sta->p2p_ie != NULL &&
1004 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1007 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1010 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1011 MAC2STR(sta->addr), MAC2STR(dev_addr));
1013 #endif /* CONFIG_P2P */
1014 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1016 if (hapd->sta_authorized_cb)
1017 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1018 sta->addr, authorized, dev_addr);
1024 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1025 os_snprintf(ip_addr, sizeof(ip_addr),
1026 " ip_addr=%u.%u.%u.%u",
1027 ip_addr_buf[0], ip_addr_buf[1],
1028 ip_addr_buf[2], ip_addr_buf[3]);
1030 #endif /* CONFIG_P2P */
1032 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1035 if (hapd->msg_ctx_parent &&
1036 hapd->msg_ctx_parent != hapd->msg_ctx)
1037 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1038 AP_STA_CONNECTED "%s%s",
1041 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1043 if (hapd->msg_ctx_parent &&
1044 hapd->msg_ctx_parent != hapd->msg_ctx)
1045 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1046 AP_STA_DISCONNECTED "%s", buf);
1050 if (hapd->iface->fst) {
1052 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1054 fst_notify_peer_disconnected(hapd->iface->fst,
1057 #endif /* CONFIG_FST */
1061 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1062 const u8 *addr, u16 reason)
1065 if (sta == NULL && addr)
1066 sta = ap_get_sta(hapd, addr);
1069 hostapd_drv_sta_deauth(hapd, addr, reason);
1073 ap_sta_set_authorized(hapd, sta, 0);
1074 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1075 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1076 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1077 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1078 "for " MACSTR " (%d seconds - "
1079 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1080 __func__, MAC2STR(sta->addr),
1081 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1082 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1083 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1084 ap_handle_timer, hapd, sta);
1085 sta->timeout_next = STA_REMOVE;
1087 sta->deauth_reason = reason;
1088 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1089 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1090 eloop_register_timeout(hapd->iface->drv_flags &
1091 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1092 ap_sta_deauth_cb_timeout, hapd, sta);
1096 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1098 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1099 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1102 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1103 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1104 ap_sta_deauth_cb_timeout(hapd, sta);
1108 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1110 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1111 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1114 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1115 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1116 ap_sta_disassoc_cb_timeout(hapd, sta);
1120 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1125 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1126 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1127 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1128 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1129 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1131 (flags & WLAN_STA_SHORT_PREAMBLE ?
1132 "[SHORT_PREAMBLE]" : ""),
1133 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1134 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1135 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1136 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1137 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1138 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1139 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1140 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1141 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1142 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1143 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1144 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1145 "[WNM_SLEEP_MODE]" : ""));
1146 if (os_snprintf_error(buflen, res))