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"
39 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
40 struct sta_info *sta);
41 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
42 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
43 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
44 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
45 #ifdef CONFIG_IEEE80211W
46 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
47 #endif /* CONFIG_IEEE80211W */
48 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
50 int ap_for_each_sta(struct hostapd_data *hapd,
51 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
57 for (sta = hapd->sta_list; sta; sta = sta->next) {
58 if (cb(hapd, sta, ctx))
66 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
70 s = hapd->sta_hash[STA_HASH(sta)];
71 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
78 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
82 for (sta = hapd->sta_list; sta; sta = sta->next) {
83 const u8 *p2p_dev_addr;
85 if (sta->p2p_ie == NULL)
88 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
89 if (p2p_dev_addr == NULL)
92 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
98 #endif /* CONFIG_P2P */
101 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
103 struct sta_info *tmp;
105 if (hapd->sta_list == sta) {
106 hapd->sta_list = sta->next;
110 tmp = hapd->sta_list;
111 while (tmp != NULL && tmp->next != sta)
114 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
115 "list.", MAC2STR(sta->addr));
117 tmp->next = sta->next;
121 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
123 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
124 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
128 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
132 s = hapd->sta_hash[STA_HASH(sta->addr)];
133 if (s == NULL) return;
134 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
135 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
139 while (s->hnext != NULL &&
140 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
142 if (s->hnext != NULL)
143 s->hnext = s->hnext->hnext;
145 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
146 " from hash table", MAC2STR(sta->addr));
150 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
152 sta_ip6addr_del(hapd, sta);
156 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
160 accounting_sta_stop(hapd, sta);
163 ap_sta_set_authorized(hapd, sta, 0);
165 if (sta->flags & WLAN_STA_WDS)
166 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
169 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
170 ap_sta_ip6addr_del(hapd, sta);
172 if (!hapd->iface->driver_ap_teardown &&
173 !(sta->flags & WLAN_STA_PREAUTH))
174 hostapd_drv_sta_remove(hapd, sta->addr);
176 ap_sta_hash_del(hapd, sta);
177 ap_sta_list_del(hapd, sta);
180 hapd->sta_aid[(sta->aid - 1) / 32] &=
181 ~BIT((sta->aid - 1) % 32);
184 if (sta->nonerp_set) {
186 hapd->iface->num_sta_non_erp--;
187 if (hapd->iface->num_sta_non_erp == 0)
191 if (sta->no_short_slot_time_set) {
192 sta->no_short_slot_time_set = 0;
193 hapd->iface->num_sta_no_short_slot_time--;
194 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
195 && hapd->iface->num_sta_no_short_slot_time == 0)
199 if (sta->no_short_preamble_set) {
200 sta->no_short_preamble_set = 0;
201 hapd->iface->num_sta_no_short_preamble--;
202 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
203 && hapd->iface->num_sta_no_short_preamble == 0)
207 if (sta->no_ht_gf_set) {
208 sta->no_ht_gf_set = 0;
209 hapd->iface->num_sta_ht_no_gf--;
212 if (sta->no_ht_set) {
214 hapd->iface->num_sta_no_ht--;
217 if (sta->ht_20mhz_set) {
218 sta->ht_20mhz_set = 0;
219 hapd->iface->num_sta_ht_20mhz--;
222 #ifdef CONFIG_IEEE80211N
223 ht40_intolerant_remove(hapd->iface, sta);
224 #endif /* CONFIG_IEEE80211N */
227 if (sta->no_p2p_set) {
229 hapd->num_sta_no_p2p--;
230 if (hapd->num_sta_no_p2p == 0)
231 hostapd_p2p_non_p2p_sta_disconnected(hapd);
233 #endif /* CONFIG_P2P */
235 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
236 if (hostapd_ht_operation_update(hapd->iface) > 0)
238 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
241 if (hapd->mesh_sta_free_cb)
242 hapd->mesh_sta_free_cb(hapd, sta);
243 #endif /* CONFIG_MESH */
246 ieee802_11_set_beacons(hapd->iface);
248 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
249 __func__, MAC2STR(sta->addr));
250 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
251 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
252 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
253 ap_sta_clear_disconnect_timeouts(hapd, sta);
254 sae_clear_retransmit_timer(hapd, sta);
256 ieee802_1x_free_station(hapd, 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
271 vlan_remove_dynamic(hapd, sta->vlan_id);
272 if (sta->vlan_id_bound) {
274 * Need to remove the STA entry before potentially removing the
277 if (hapd->iface->driver_ap_teardown &&
278 !(sta->flags & WLAN_STA_PREAUTH))
279 hostapd_drv_sta_remove(hapd, sta->addr);
280 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
282 #endif /* CONFIG_NO_VLAN */
284 os_free(sta->challenge);
286 #ifdef CONFIG_IEEE80211W
287 os_free(sta->sa_query_trans_id);
288 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
289 #endif /* CONFIG_IEEE80211W */
292 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
293 #endif /* CONFIG_P2P */
295 #ifdef CONFIG_INTERWORKING
296 if (sta->gas_dialog) {
298 for (i = 0; i < GAS_DIALOG_MAX; i++)
299 gas_serv_dialog_clear(&sta->gas_dialog[i]);
300 os_free(sta->gas_dialog);
302 #endif /* CONFIG_INTERWORKING */
304 wpabuf_free(sta->wps_ie);
305 wpabuf_free(sta->p2p_ie);
306 wpabuf_free(sta->hs20_ie);
308 wpabuf_free(sta->mb_ies);
309 #endif /* CONFIG_FST */
311 os_free(sta->ht_capabilities);
312 os_free(sta->vht_capabilities);
313 hostapd_free_psk_list(sta->psk);
314 os_free(sta->identity);
315 os_free(sta->radius_cui);
316 os_free(sta->remediation_url);
317 wpabuf_free(sta->hs20_deauth_req);
318 os_free(sta->hs20_session_info_url);
321 sae_clear_data(sta->sae);
323 #endif /* CONFIG_SAE */
329 void hostapd_free_stas(struct hostapd_data *hapd)
331 struct sta_info *sta, *prev;
333 sta = hapd->sta_list;
337 if (sta->flags & WLAN_STA_AUTH) {
338 mlme_deauthenticate_indication(
339 hapd, sta, WLAN_REASON_UNSPECIFIED);
342 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
343 MAC2STR(prev->addr));
344 ap_free_sta(hapd, prev);
350 * ap_handle_timer - Per STA timer handler
351 * @eloop_ctx: struct hostapd_data *
352 * @timeout_ctx: struct sta_info *
354 * This function is called to check station activity and to remove inactive
357 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
359 struct hostapd_data *hapd = eloop_ctx;
360 struct sta_info *sta = timeout_ctx;
361 unsigned long next_time = 0;
364 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
365 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
367 if (sta->timeout_next == STA_REMOVE) {
368 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
369 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
370 "local deauth request");
371 ap_free_sta(hapd, sta);
375 if ((sta->flags & WLAN_STA_ASSOC) &&
376 (sta->timeout_next == STA_NULLFUNC ||
377 sta->timeout_next == STA_DISASSOC)) {
380 * Add random value to timeout so that we don't end up bouncing
381 * all stations at the same time if we have lots of associated
382 * stations that are idle (but keep re-associating).
384 int fuzz = os_random() % 20;
385 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
386 if (inactive_sec == -1) {
387 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
388 "Check inactivity: Could not "
389 "get station info from kernel driver for "
390 MACSTR, MAC2STR(sta->addr));
392 * The driver may not support this functionality.
393 * Anyway, try again after the next inactivity timeout,
394 * but do not disconnect the station now.
396 next_time = hapd->conf->ap_max_inactivity + fuzz;
397 } else if (inactive_sec == -ENOENT) {
398 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
399 "Station " MACSTR " has lost its driver entry",
402 /* Avoid sending client probe on removed client */
403 sta->timeout_next = STA_DISASSOC;
405 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
406 /* station activity detected; reset timeout state */
407 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
408 "Station " MACSTR " has been active %is ago",
409 MAC2STR(sta->addr), inactive_sec);
410 sta->timeout_next = STA_NULLFUNC;
411 next_time = hapd->conf->ap_max_inactivity + fuzz -
414 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
415 "Station " MACSTR " has been "
416 "inactive too long: %d sec, max allowed: %d",
417 MAC2STR(sta->addr), inactive_sec,
418 hapd->conf->ap_max_inactivity);
420 if (hapd->conf->skip_inactivity_poll)
421 sta->timeout_next = STA_DISASSOC;
425 if ((sta->flags & WLAN_STA_ASSOC) &&
426 sta->timeout_next == STA_DISASSOC &&
427 !(sta->flags & WLAN_STA_PENDING_POLL) &&
428 !hapd->conf->skip_inactivity_poll) {
429 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
430 " has ACKed data poll", MAC2STR(sta->addr));
431 /* data nullfunc frame poll did not produce TX errors; assume
432 * station ACKed it */
433 sta->timeout_next = STA_NULLFUNC;
434 next_time = hapd->conf->ap_max_inactivity;
439 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
440 "for " MACSTR " (%lu seconds)",
441 __func__, MAC2STR(sta->addr), next_time);
442 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
447 if (sta->timeout_next == STA_NULLFUNC &&
448 (sta->flags & WLAN_STA_ASSOC)) {
449 wpa_printf(MSG_DEBUG, " Polling STA");
450 sta->flags |= WLAN_STA_PENDING_POLL;
451 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
452 sta->flags & WLAN_STA_WMM);
453 } else if (sta->timeout_next != STA_REMOVE) {
454 int deauth = sta->timeout_next == STA_DEAUTH;
456 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
457 "Timeout, sending %s info to STA " MACSTR,
458 deauth ? "deauthentication" : "disassociation",
462 hostapd_drv_sta_deauth(
464 WLAN_REASON_PREV_AUTH_NOT_VALID);
466 reason = (sta->timeout_next == STA_DISASSOC) ?
467 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
468 WLAN_REASON_PREV_AUTH_NOT_VALID;
470 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
474 switch (sta->timeout_next) {
476 sta->timeout_next = STA_DISASSOC;
477 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
478 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
479 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
480 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
484 case STA_DISASSOC_FROM_CLI:
485 ap_sta_set_authorized(hapd, sta, 0);
486 sta->flags &= ~WLAN_STA_ASSOC;
487 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
488 if (!sta->acct_terminate_cause)
489 sta->acct_terminate_cause =
490 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
491 accounting_sta_stop(hapd, sta);
492 ieee802_1x_free_station(hapd, sta);
493 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
494 HOSTAPD_LEVEL_INFO, "disassociated due to "
496 reason = (sta->timeout_next == STA_DISASSOC) ?
497 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
498 WLAN_REASON_PREV_AUTH_NOT_VALID;
499 sta->timeout_next = STA_DEAUTH;
500 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
501 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
502 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
503 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
505 mlme_disassociate_indication(hapd, sta, reason);
509 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
510 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
511 "inactivity (timer DEAUTH/REMOVE)");
512 if (!sta->acct_terminate_cause)
513 sta->acct_terminate_cause =
514 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
515 mlme_deauthenticate_indication(
517 WLAN_REASON_PREV_AUTH_NOT_VALID);
518 ap_free_sta(hapd, sta);
524 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
526 struct hostapd_data *hapd = eloop_ctx;
527 struct sta_info *sta = timeout_ctx;
529 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
530 hapd->conf->iface, MAC2STR(sta->addr));
531 if (!(sta->flags & WLAN_STA_AUTH)) {
532 if (sta->flags & WLAN_STA_GAS) {
533 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
534 "entry " MACSTR, MAC2STR(sta->addr));
535 ap_free_sta(hapd, sta);
540 hostapd_drv_sta_deauth(hapd, sta->addr,
541 WLAN_REASON_PREV_AUTH_NOT_VALID);
542 mlme_deauthenticate_indication(hapd, sta,
543 WLAN_REASON_PREV_AUTH_NOT_VALID);
544 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
545 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
547 sta->acct_terminate_cause =
548 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
549 ap_free_sta(hapd, sta);
553 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
556 if (eloop_replenish_timeout(session_timeout, 0,
557 ap_handle_session_timer, hapd, sta) == 1) {
558 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
559 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
560 "to %d seconds", session_timeout);
565 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
568 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
569 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
570 "seconds", session_timeout);
571 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
572 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
577 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
579 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
583 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
586 struct hostapd_data *hapd = eloop_ctx;
587 struct sta_info *sta = timeout_ctx;
589 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
590 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
591 if (sta->hs20_session_info_url == NULL)
594 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
595 sta->hs20_disassoc_timer);
596 #endif /* CONFIG_WNM */
600 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
601 struct sta_info *sta, int warning_time)
603 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
604 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
609 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
611 struct sta_info *sta;
613 sta = ap_get_sta(hapd, addr);
617 wpa_printf(MSG_DEBUG, " New STA");
618 if (hapd->num_sta >= hapd->conf->max_num_sta) {
619 /* FIX: might try to remove some old STAs first? */
620 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
621 hapd->num_sta, hapd->conf->max_num_sta);
625 sta = os_zalloc(sizeof(struct sta_info));
627 wpa_printf(MSG_ERROR, "malloc failed");
630 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
631 if (accounting_sta_get_id(hapd, sta) < 0) {
636 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
637 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
638 "for " MACSTR " (%d seconds - ap_max_inactivity)",
639 __func__, MAC2STR(addr),
640 hapd->conf->ap_max_inactivity);
641 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
642 ap_handle_timer, hapd, sta);
645 /* initialize STA info data */
646 os_memcpy(sta->addr, addr, ETH_ALEN);
647 sta->next = hapd->sta_list;
648 hapd->sta_list = sta;
650 ap_sta_hash_add(hapd, sta);
651 ap_sta_remove_in_other_bss(hapd, sta);
652 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
653 dl_list_init(&sta->ip6addr);
659 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
661 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
664 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
665 ap_sta_ip6addr_del(hapd, sta);
667 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
668 hapd->conf->iface, MAC2STR(sta->addr));
669 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
670 sta->flags & WLAN_STA_ASSOC) {
671 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
672 " from kernel driver",
673 hapd->conf->iface, MAC2STR(sta->addr));
680 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
681 struct sta_info *sta)
683 struct hostapd_iface *iface = hapd->iface;
686 for (i = 0; i < iface->num_bss; i++) {
687 struct hostapd_data *bss = iface->bss[i];
688 struct sta_info *sta2;
689 /* bss should always be set during operation, but it may be
690 * NULL during reconfiguration. Assume the STA is not
691 * associated to another BSS in that case to avoid NULL pointer
693 if (bss == hapd || bss == NULL)
695 sta2 = ap_get_sta(bss, sta->addr);
699 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
700 " association from another BSS %s",
701 hapd->conf->iface, MAC2STR(sta2->addr),
703 ap_sta_disconnect(bss, sta2, sta2->addr,
704 WLAN_REASON_PREV_AUTH_NOT_VALID);
709 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
711 struct hostapd_data *hapd = eloop_ctx;
712 struct sta_info *sta = timeout_ctx;
714 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
715 hapd->conf->iface, MAC2STR(sta->addr));
716 ap_sta_remove(hapd, sta);
717 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
721 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
724 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
725 hapd->conf->iface, MAC2STR(sta->addr));
726 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
727 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
728 ap_sta_set_authorized(hapd, sta, 0);
729 sta->timeout_next = STA_DEAUTH;
730 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
731 "for " MACSTR " (%d seconds - "
732 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
733 __func__, MAC2STR(sta->addr),
734 AP_MAX_INACTIVITY_AFTER_DISASSOC);
735 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
736 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
737 ap_handle_timer, hapd, sta);
738 accounting_sta_stop(hapd, sta);
739 ieee802_1x_free_station(hapd, sta);
741 sta->disassoc_reason = reason;
742 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
743 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
744 eloop_register_timeout(hapd->iface->drv_flags &
745 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
746 ap_sta_disassoc_cb_timeout, hapd, sta);
750 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
752 struct hostapd_data *hapd = eloop_ctx;
753 struct sta_info *sta = timeout_ctx;
755 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
756 hapd->conf->iface, MAC2STR(sta->addr));
757 ap_sta_remove(hapd, sta);
758 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
762 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
765 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
766 hapd->conf->iface, MAC2STR(sta->addr));
767 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
768 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
769 ap_sta_set_authorized(hapd, sta, 0);
770 sta->timeout_next = STA_REMOVE;
771 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
772 "for " MACSTR " (%d seconds - "
773 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
774 __func__, MAC2STR(sta->addr),
775 AP_MAX_INACTIVITY_AFTER_DEAUTH);
776 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
777 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
778 ap_handle_timer, hapd, sta);
779 accounting_sta_stop(hapd, sta);
780 ieee802_1x_free_station(hapd, sta);
782 sta->deauth_reason = reason;
783 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
784 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
785 eloop_register_timeout(hapd->iface->drv_flags &
786 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
787 ap_sta_deauth_cb_timeout, hapd, sta);
792 int ap_sta_wps_cancel(struct hostapd_data *hapd,
793 struct sta_info *sta, void *ctx)
795 if (sta && (sta->flags & WLAN_STA_WPS)) {
796 ap_sta_deauthenticate(hapd, sta,
797 WLAN_REASON_PREV_AUTH_NOT_VALID);
798 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
799 __func__, MAC2STR(sta->addr));
805 #endif /* CONFIG_WPS */
808 int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
809 struct vlan_description *vlan_desc)
811 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
812 int old_vlan_id, vlan_id = 0, ret = 0;
814 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED) {
816 } else if (vlan_desc && vlan_desc->notempty) {
817 if (!vlan_compare(vlan_desc, sta->vlan_desc))
818 return 0; /* nothing to change */
820 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
821 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
823 if (vlan->vlan_id == VLAN_ID_WILDCARD)
824 wildcard_vlan = vlan;
827 vlan_id = vlan->vlan_id;
828 } else if (wildcard_vlan) {
829 vlan = wildcard_vlan;
830 vlan_id = vlan_desc->untagged;
832 hostapd_logger(hapd, sta->addr,
833 HOSTAPD_MODULE_IEEE80211,
835 "missing VLAN and wildcard for vlan=%d",
836 vlan_desc->untagged);
843 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
844 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
846 hostapd_logger(hapd, sta->addr,
847 HOSTAPD_MODULE_IEEE80211,
849 "could not add dynamic VLAN interface for vlan=%d",
850 vlan_desc->untagged);
856 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
858 "added new dynamic VLAN interface '%s'",
860 } else if (vlan && vlan->dynamic_vlan > 0) {
861 vlan->dynamic_vlan++;
862 hostapd_logger(hapd, sta->addr,
863 HOSTAPD_MODULE_IEEE80211,
865 "updated existing dynamic VLAN interface '%s'",
869 old_vlan_id = sta->vlan_id;
870 sta->vlan_id = vlan_id;
871 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
873 if (vlan_id != old_vlan_id && old_vlan_id)
874 vlan_remove_dynamic(hapd, old_vlan_id);
880 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
882 #ifndef CONFIG_NO_VLAN
884 struct hostapd_vlan *vlan = NULL;
886 int old_vlanid = sta->vlan_id_bound;
888 iface = hapd->conf->iface;
889 if (hapd->conf->ssid.vlan[0])
890 iface = hapd->conf->ssid.vlan;
892 if (sta->vlan_id > 0) {
893 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
894 if (vlan->vlan_id == sta->vlan_id)
898 iface = vlan->ifname;
902 * Do not increment ref counters if the VLAN ID remains same, but do
903 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
904 * have been called before.
906 if (sta->vlan_id == old_vlanid)
909 if (sta->vlan_id > 0 && vlan == NULL) {
910 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
911 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
912 "binding station to (vlan_id=%d)",
916 } else if (vlan && vlan->dynamic_vlan > 0) {
917 vlan->dynamic_vlan++;
918 hostapd_logger(hapd, sta->addr,
919 HOSTAPD_MODULE_IEEE80211,
921 "updated existing dynamic VLAN interface '%s'",
925 /* ref counters have been increased, so mark the station */
926 sta->vlan_id_bound = sta->vlan_id;
929 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
930 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
933 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
934 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
936 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
938 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
939 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
940 "entry to vlan_id=%d", sta->vlan_id);
943 /* During 1x reauth, if the vlan id changes, then remove the old id. */
944 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
945 vlan_remove_dynamic(hapd, old_vlanid);
949 #else /* CONFIG_NO_VLAN */
951 #endif /* CONFIG_NO_VLAN */
955 #ifdef CONFIG_IEEE80211W
957 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
960 struct os_reltime now, passed;
961 os_get_reltime(&now);
962 os_reltime_sub(&now, &sta->sa_query_start, &passed);
963 tu = (passed.sec * 1000000 + passed.usec) / 1024;
964 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
965 hostapd_logger(hapd, sta->addr,
966 HOSTAPD_MODULE_IEEE80211,
968 "association SA Query timed out");
969 sta->sa_query_timed_out = 1;
970 os_free(sta->sa_query_trans_id);
971 sta->sa_query_trans_id = NULL;
972 sta->sa_query_count = 0;
973 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
981 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
983 struct hostapd_data *hapd = eloop_ctx;
984 struct sta_info *sta = timeout_ctx;
985 unsigned int timeout, sec, usec;
988 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
990 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
992 if (sta->sa_query_count > 0 &&
993 ap_check_sa_query_timeout(hapd, sta))
996 nbuf = os_realloc_array(sta->sa_query_trans_id,
997 sta->sa_query_count + 1,
998 WLAN_SA_QUERY_TR_ID_LEN);
1001 if (sta->sa_query_count == 0) {
1002 /* Starting a new SA Query procedure */
1003 os_get_reltime(&sta->sa_query_start);
1005 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1006 sta->sa_query_trans_id = nbuf;
1007 sta->sa_query_count++;
1009 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1011 * We don't really care which ID is used here, so simply
1012 * hardcode this if the mostly theoretical os_get_random()
1019 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1020 sec = ((timeout / 1000) * 1024) / 1000;
1021 usec = (timeout % 1000) * 1024;
1022 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1024 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1025 HOSTAPD_LEVEL_DEBUG,
1026 "association SA Query attempt %d", sta->sa_query_count);
1028 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
1032 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1034 ap_sa_query_timer(hapd, sta);
1038 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1040 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1041 os_free(sta->sa_query_trans_id);
1042 sta->sa_query_trans_id = NULL;
1043 sta->sa_query_count = 0;
1046 #endif /* CONFIG_IEEE80211W */
1049 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1052 const u8 *dev_addr = NULL;
1057 #endif /* CONFIG_P2P */
1059 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1063 sta->flags |= WLAN_STA_AUTHORIZED;
1065 sta->flags &= ~WLAN_STA_AUTHORIZED;
1068 if (hapd->p2p_group == NULL) {
1069 if (sta->p2p_ie != NULL &&
1070 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1073 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1076 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1077 MAC2STR(sta->addr), MAC2STR(dev_addr));
1079 #endif /* CONFIG_P2P */
1080 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1082 if (hapd->sta_authorized_cb)
1083 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1084 sta->addr, authorized, dev_addr);
1090 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1091 os_snprintf(ip_addr, sizeof(ip_addr),
1092 " ip_addr=%u.%u.%u.%u",
1093 ip_addr_buf[0], ip_addr_buf[1],
1094 ip_addr_buf[2], ip_addr_buf[3]);
1096 #endif /* CONFIG_P2P */
1098 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1101 if (hapd->msg_ctx_parent &&
1102 hapd->msg_ctx_parent != hapd->msg_ctx)
1103 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1104 AP_STA_CONNECTED "%s%s",
1107 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1109 if (hapd->msg_ctx_parent &&
1110 hapd->msg_ctx_parent != hapd->msg_ctx)
1111 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1112 AP_STA_DISCONNECTED "%s", buf);
1116 if (hapd->iface->fst) {
1118 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1120 fst_notify_peer_disconnected(hapd->iface->fst,
1123 #endif /* CONFIG_FST */
1127 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1128 const u8 *addr, u16 reason)
1131 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1132 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1135 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1136 hapd->conf->iface, __func__, MAC2STR(addr),
1139 if (sta == NULL && addr)
1140 sta = ap_get_sta(hapd, addr);
1143 hostapd_drv_sta_deauth(hapd, addr, reason);
1147 ap_sta_set_authorized(hapd, sta, 0);
1148 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1149 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1150 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1151 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
1152 "for " MACSTR " (%d seconds - "
1153 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1154 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1155 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1156 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1157 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1158 ap_handle_timer, hapd, sta);
1159 sta->timeout_next = STA_REMOVE;
1161 sta->deauth_reason = reason;
1162 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1163 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1164 eloop_register_timeout(hapd->iface->drv_flags &
1165 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1166 ap_sta_deauth_cb_timeout, hapd, sta);
1170 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1172 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1173 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1176 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1177 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1178 ap_sta_deauth_cb_timeout(hapd, sta);
1182 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1184 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1185 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1188 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1189 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1190 ap_sta_disassoc_cb_timeout(hapd, sta);
1194 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1195 struct sta_info *sta)
1197 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1198 wpa_printf(MSG_DEBUG,
1199 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1201 hapd->conf->iface, MAC2STR(sta->addr));
1202 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1203 wpa_printf(MSG_DEBUG,
1204 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1206 hapd->conf->iface, MAC2STR(sta->addr));
1210 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1215 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1216 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1217 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1218 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1219 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1221 (flags & WLAN_STA_SHORT_PREAMBLE ?
1222 "[SHORT_PREAMBLE]" : ""),
1223 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1224 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1225 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1226 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1227 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1228 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1229 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1230 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1231 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1232 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1233 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1234 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1235 "[WNM_SLEEP_MODE]" : ""));
1236 if (os_snprintf_error(buflen, res))