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 static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
810 struct hostapd_vlan *vlan;
811 int vlan_id = MAX_VLAN_ID + 2;
814 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
815 if (vlan->vlan_id == vlan_id) {
824 int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
825 struct vlan_description *vlan_desc)
827 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
828 int old_vlan_id, vlan_id = 0, ret = 0;
830 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
833 /* Check if there is something to do */
834 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
835 /* This sta is lacking its own vif */
836 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
837 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
838 /* sta->vlan_id needs to be reset */
839 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
840 return 0; /* nothing to change */
843 /* Now the real VLAN changed or the STA just needs its own vif */
844 if (hapd->conf->ssid.per_sta_vif) {
845 /* Assign a new vif, always */
846 /* find a free vlan_id sufficiently big */
847 vlan_id = ap_sta_get_free_vlan_id(hapd);
848 /* Get wildcard VLAN */
849 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
850 if (vlan->vlan_id == VLAN_ID_WILDCARD)
854 hostapd_logger(hapd, sta->addr,
855 HOSTAPD_MODULE_IEEE80211,
857 "per_sta_vif missing wildcard");
862 } else if (vlan_desc && vlan_desc->notempty) {
863 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
864 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
866 if (vlan->vlan_id == VLAN_ID_WILDCARD)
867 wildcard_vlan = vlan;
870 vlan_id = vlan->vlan_id;
871 } else if (wildcard_vlan) {
872 vlan = wildcard_vlan;
873 vlan_id = vlan_desc->untagged;
874 if (vlan_desc->tagged[0]) {
875 /* Tagged VLAN configuration */
876 vlan_id = ap_sta_get_free_vlan_id(hapd);
879 hostapd_logger(hapd, sta->addr,
880 HOSTAPD_MODULE_IEEE80211,
882 "missing vlan and wildcard for vlan=%d%s",
884 vlan_desc->tagged[0] ? "+" : "");
891 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
892 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
894 hostapd_logger(hapd, sta->addr,
895 HOSTAPD_MODULE_IEEE80211,
897 "could not add dynamic VLAN interface for vlan=%d%s",
899 vlan_desc->tagged[0] ? "+" : "");
905 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
907 "added new dynamic VLAN interface '%s'",
909 } else if (vlan && vlan->dynamic_vlan > 0) {
910 vlan->dynamic_vlan++;
911 hostapd_logger(hapd, sta->addr,
912 HOSTAPD_MODULE_IEEE80211,
914 "updated existing dynamic VLAN interface '%s'",
918 old_vlan_id = sta->vlan_id;
919 sta->vlan_id = vlan_id;
920 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
922 if (vlan_id != old_vlan_id && old_vlan_id)
923 vlan_remove_dynamic(hapd, old_vlan_id);
929 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
931 #ifndef CONFIG_NO_VLAN
933 struct hostapd_vlan *vlan = NULL;
935 int old_vlanid = sta->vlan_id_bound;
937 iface = hapd->conf->iface;
938 if (hapd->conf->ssid.vlan[0])
939 iface = hapd->conf->ssid.vlan;
941 if (sta->vlan_id > 0) {
942 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
943 if (vlan->vlan_id == sta->vlan_id)
947 iface = vlan->ifname;
951 * Do not increment ref counters if the VLAN ID remains same, but do
952 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
953 * have been called before.
955 if (sta->vlan_id == old_vlanid)
958 if (sta->vlan_id > 0 && vlan == NULL) {
959 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
960 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
961 "binding station to (vlan_id=%d)",
965 } else if (vlan && vlan->dynamic_vlan > 0) {
966 vlan->dynamic_vlan++;
967 hostapd_logger(hapd, sta->addr,
968 HOSTAPD_MODULE_IEEE80211,
970 "updated existing dynamic VLAN interface '%s'",
974 /* ref counters have been increased, so mark the station */
975 sta->vlan_id_bound = sta->vlan_id;
978 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
979 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
982 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
983 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
985 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
987 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
988 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
989 "entry to vlan_id=%d", sta->vlan_id);
992 /* During 1x reauth, if the vlan id changes, then remove the old id. */
993 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
994 vlan_remove_dynamic(hapd, old_vlanid);
998 #else /* CONFIG_NO_VLAN */
1000 #endif /* CONFIG_NO_VLAN */
1004 #ifdef CONFIG_IEEE80211W
1006 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1009 struct os_reltime now, passed;
1010 os_get_reltime(&now);
1011 os_reltime_sub(&now, &sta->sa_query_start, &passed);
1012 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1013 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1014 hostapd_logger(hapd, sta->addr,
1015 HOSTAPD_MODULE_IEEE80211,
1016 HOSTAPD_LEVEL_DEBUG,
1017 "association SA Query timed out");
1018 sta->sa_query_timed_out = 1;
1019 os_free(sta->sa_query_trans_id);
1020 sta->sa_query_trans_id = NULL;
1021 sta->sa_query_count = 0;
1022 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1030 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1032 struct hostapd_data *hapd = eloop_ctx;
1033 struct sta_info *sta = timeout_ctx;
1034 unsigned int timeout, sec, usec;
1035 u8 *trans_id, *nbuf;
1037 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1039 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1041 if (sta->sa_query_count > 0 &&
1042 ap_check_sa_query_timeout(hapd, sta))
1045 nbuf = os_realloc_array(sta->sa_query_trans_id,
1046 sta->sa_query_count + 1,
1047 WLAN_SA_QUERY_TR_ID_LEN);
1050 if (sta->sa_query_count == 0) {
1051 /* Starting a new SA Query procedure */
1052 os_get_reltime(&sta->sa_query_start);
1054 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1055 sta->sa_query_trans_id = nbuf;
1056 sta->sa_query_count++;
1058 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1060 * We don't really care which ID is used here, so simply
1061 * hardcode this if the mostly theoretical os_get_random()
1068 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1069 sec = ((timeout / 1000) * 1024) / 1000;
1070 usec = (timeout % 1000) * 1024;
1071 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1073 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1074 HOSTAPD_LEVEL_DEBUG,
1075 "association SA Query attempt %d", sta->sa_query_count);
1077 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
1081 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1083 ap_sa_query_timer(hapd, sta);
1087 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1089 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1090 os_free(sta->sa_query_trans_id);
1091 sta->sa_query_trans_id = NULL;
1092 sta->sa_query_count = 0;
1095 #endif /* CONFIG_IEEE80211W */
1098 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1101 const u8 *dev_addr = NULL;
1106 #endif /* CONFIG_P2P */
1108 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1112 sta->flags |= WLAN_STA_AUTHORIZED;
1114 sta->flags &= ~WLAN_STA_AUTHORIZED;
1117 if (hapd->p2p_group == NULL) {
1118 if (sta->p2p_ie != NULL &&
1119 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1122 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1125 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1126 MAC2STR(sta->addr), MAC2STR(dev_addr));
1128 #endif /* CONFIG_P2P */
1129 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1131 if (hapd->sta_authorized_cb)
1132 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1133 sta->addr, authorized, dev_addr);
1139 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1140 os_snprintf(ip_addr, sizeof(ip_addr),
1141 " ip_addr=%u.%u.%u.%u",
1142 ip_addr_buf[0], ip_addr_buf[1],
1143 ip_addr_buf[2], ip_addr_buf[3]);
1145 #endif /* CONFIG_P2P */
1147 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1150 if (hapd->msg_ctx_parent &&
1151 hapd->msg_ctx_parent != hapd->msg_ctx)
1152 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1153 AP_STA_CONNECTED "%s%s",
1156 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1158 if (hapd->msg_ctx_parent &&
1159 hapd->msg_ctx_parent != hapd->msg_ctx)
1160 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1161 AP_STA_DISCONNECTED "%s", buf);
1165 if (hapd->iface->fst) {
1167 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1169 fst_notify_peer_disconnected(hapd->iface->fst,
1172 #endif /* CONFIG_FST */
1176 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1177 const u8 *addr, u16 reason)
1180 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1181 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1184 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1185 hapd->conf->iface, __func__, MAC2STR(addr),
1188 if (sta == NULL && addr)
1189 sta = ap_get_sta(hapd, addr);
1192 hostapd_drv_sta_deauth(hapd, addr, reason);
1196 ap_sta_set_authorized(hapd, sta, 0);
1197 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1198 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1199 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1200 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
1201 "for " MACSTR " (%d seconds - "
1202 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1203 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1204 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1205 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1206 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1207 ap_handle_timer, hapd, sta);
1208 sta->timeout_next = STA_REMOVE;
1210 sta->deauth_reason = reason;
1211 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1212 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1213 eloop_register_timeout(hapd->iface->drv_flags &
1214 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1215 ap_sta_deauth_cb_timeout, hapd, sta);
1219 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1221 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1222 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1225 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1226 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1227 ap_sta_deauth_cb_timeout(hapd, sta);
1231 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1233 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1234 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1237 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1238 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1239 ap_sta_disassoc_cb_timeout(hapd, sta);
1243 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1244 struct sta_info *sta)
1246 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1247 wpa_printf(MSG_DEBUG,
1248 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1250 hapd->conf->iface, MAC2STR(sta->addr));
1251 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1252 wpa_printf(MSG_DEBUG,
1253 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1255 hapd->conf->iface, MAC2STR(sta->addr));
1259 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1264 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1265 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1266 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1267 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1268 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1270 (flags & WLAN_STA_SHORT_PREAMBLE ?
1271 "[SHORT_PREAMBLE]" : ""),
1272 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1273 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1274 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1275 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1276 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1277 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1278 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1279 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1280 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1281 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1282 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1283 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1284 "[WNM_SLEEP_MODE]" : ""));
1285 if (os_snprintf_error(buflen, res))