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 ap_sta_clear_disconnect_timeouts(hapd, sta);
253 sae_clear_retransmit_timer(hapd, sta);
255 ieee802_1x_free_station(hapd, sta);
256 wpa_auth_sta_deinit(sta->wpa_sm);
257 rsn_preauth_free_station(hapd, sta);
258 #ifndef CONFIG_NO_RADIUS
260 radius_client_flush_auth(hapd->radius, sta->addr);
261 #endif /* CONFIG_NO_RADIUS */
263 #ifndef CONFIG_NO_VLAN
265 * sta->wpa_sm->group needs to be released before so that
266 * vlan_remove_dynamic() can check that no stations are left on the
269 if (sta->vlan_id_bound) {
271 * Need to remove the STA entry before potentially removing the
274 if (hapd->iface->driver_ap_teardown &&
275 !(sta->flags & WLAN_STA_PREAUTH))
276 hostapd_drv_sta_remove(hapd, sta->addr);
277 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
279 #endif /* CONFIG_NO_VLAN */
281 os_free(sta->challenge);
283 #ifdef CONFIG_IEEE80211W
284 os_free(sta->sa_query_trans_id);
285 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
286 #endif /* CONFIG_IEEE80211W */
289 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
290 #endif /* CONFIG_P2P */
292 #ifdef CONFIG_INTERWORKING
293 if (sta->gas_dialog) {
295 for (i = 0; i < GAS_DIALOG_MAX; i++)
296 gas_serv_dialog_clear(&sta->gas_dialog[i]);
297 os_free(sta->gas_dialog);
299 #endif /* CONFIG_INTERWORKING */
301 wpabuf_free(sta->wps_ie);
302 wpabuf_free(sta->p2p_ie);
303 wpabuf_free(sta->hs20_ie);
305 wpabuf_free(sta->mb_ies);
306 #endif /* CONFIG_FST */
308 os_free(sta->ht_capabilities);
309 os_free(sta->vht_capabilities);
310 hostapd_free_psk_list(sta->psk);
311 os_free(sta->identity);
312 os_free(sta->radius_cui);
313 os_free(sta->remediation_url);
314 wpabuf_free(sta->hs20_deauth_req);
315 os_free(sta->hs20_session_info_url);
318 sae_clear_data(sta->sae);
320 #endif /* CONFIG_SAE */
326 void hostapd_free_stas(struct hostapd_data *hapd)
328 struct sta_info *sta, *prev;
330 sta = hapd->sta_list;
334 if (sta->flags & WLAN_STA_AUTH) {
335 mlme_deauthenticate_indication(
336 hapd, sta, WLAN_REASON_UNSPECIFIED);
339 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
340 MAC2STR(prev->addr));
341 ap_free_sta(hapd, prev);
347 * ap_handle_timer - Per STA timer handler
348 * @eloop_ctx: struct hostapd_data *
349 * @timeout_ctx: struct sta_info *
351 * This function is called to check station activity and to remove inactive
354 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
356 struct hostapd_data *hapd = eloop_ctx;
357 struct sta_info *sta = timeout_ctx;
358 unsigned long next_time = 0;
361 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
362 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
364 if (sta->timeout_next == STA_REMOVE) {
365 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
366 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
367 "local deauth request");
368 ap_free_sta(hapd, sta);
372 if ((sta->flags & WLAN_STA_ASSOC) &&
373 (sta->timeout_next == STA_NULLFUNC ||
374 sta->timeout_next == STA_DISASSOC)) {
377 * Add random value to timeout so that we don't end up bouncing
378 * all stations at the same time if we have lots of associated
379 * stations that are idle (but keep re-associating).
381 int fuzz = os_random() % 20;
382 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
383 if (inactive_sec == -1) {
384 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
385 "Check inactivity: Could not "
386 "get station info from kernel driver for "
387 MACSTR, MAC2STR(sta->addr));
389 * The driver may not support this functionality.
390 * Anyway, try again after the next inactivity timeout,
391 * but do not disconnect the station now.
393 next_time = hapd->conf->ap_max_inactivity + fuzz;
394 } else if (inactive_sec == -ENOENT) {
395 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
396 "Station " MACSTR " has lost its driver entry",
399 /* Avoid sending client probe on removed client */
400 sta->timeout_next = STA_DISASSOC;
402 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
403 /* station activity detected; reset timeout state */
404 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
405 "Station " MACSTR " has been active %is ago",
406 MAC2STR(sta->addr), inactive_sec);
407 sta->timeout_next = STA_NULLFUNC;
408 next_time = hapd->conf->ap_max_inactivity + fuzz -
411 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
412 "Station " MACSTR " has been "
413 "inactive too long: %d sec, max allowed: %d",
414 MAC2STR(sta->addr), inactive_sec,
415 hapd->conf->ap_max_inactivity);
417 if (hapd->conf->skip_inactivity_poll)
418 sta->timeout_next = STA_DISASSOC;
422 if ((sta->flags & WLAN_STA_ASSOC) &&
423 sta->timeout_next == STA_DISASSOC &&
424 !(sta->flags & WLAN_STA_PENDING_POLL) &&
425 !hapd->conf->skip_inactivity_poll) {
426 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
427 " has ACKed data poll", MAC2STR(sta->addr));
428 /* data nullfunc frame poll did not produce TX errors; assume
429 * station ACKed it */
430 sta->timeout_next = STA_NULLFUNC;
431 next_time = hapd->conf->ap_max_inactivity;
436 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
437 "for " MACSTR " (%lu seconds)",
438 __func__, MAC2STR(sta->addr), next_time);
439 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
444 if (sta->timeout_next == STA_NULLFUNC &&
445 (sta->flags & WLAN_STA_ASSOC)) {
446 wpa_printf(MSG_DEBUG, " Polling STA");
447 sta->flags |= WLAN_STA_PENDING_POLL;
448 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
449 sta->flags & WLAN_STA_WMM);
450 } else if (sta->timeout_next != STA_REMOVE) {
451 int deauth = sta->timeout_next == STA_DEAUTH;
453 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
454 "Timeout, sending %s info to STA " MACSTR,
455 deauth ? "deauthentication" : "disassociation",
459 hostapd_drv_sta_deauth(
461 WLAN_REASON_PREV_AUTH_NOT_VALID);
463 reason = (sta->timeout_next == STA_DISASSOC) ?
464 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
465 WLAN_REASON_PREV_AUTH_NOT_VALID;
467 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
471 switch (sta->timeout_next) {
473 sta->timeout_next = STA_DISASSOC;
474 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
475 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
476 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
477 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
481 case STA_DISASSOC_FROM_CLI:
482 ap_sta_set_authorized(hapd, sta, 0);
483 sta->flags &= ~WLAN_STA_ASSOC;
484 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
485 if (!sta->acct_terminate_cause)
486 sta->acct_terminate_cause =
487 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
488 accounting_sta_stop(hapd, sta);
489 ieee802_1x_free_station(hapd, sta);
490 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
491 HOSTAPD_LEVEL_INFO, "disassociated due to "
493 reason = (sta->timeout_next == STA_DISASSOC) ?
494 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
495 WLAN_REASON_PREV_AUTH_NOT_VALID;
496 sta->timeout_next = STA_DEAUTH;
497 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
498 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
499 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
500 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
502 mlme_disassociate_indication(hapd, sta, reason);
506 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
507 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
508 "inactivity (timer DEAUTH/REMOVE)");
509 if (!sta->acct_terminate_cause)
510 sta->acct_terminate_cause =
511 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
512 mlme_deauthenticate_indication(
514 WLAN_REASON_PREV_AUTH_NOT_VALID);
515 ap_free_sta(hapd, sta);
521 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
523 struct hostapd_data *hapd = eloop_ctx;
524 struct sta_info *sta = timeout_ctx;
526 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
527 hapd->conf->iface, MAC2STR(sta->addr));
528 if (!(sta->flags & WLAN_STA_AUTH)) {
529 if (sta->flags & WLAN_STA_GAS) {
530 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
531 "entry " MACSTR, MAC2STR(sta->addr));
532 ap_free_sta(hapd, sta);
537 hostapd_drv_sta_deauth(hapd, sta->addr,
538 WLAN_REASON_PREV_AUTH_NOT_VALID);
539 mlme_deauthenticate_indication(hapd, sta,
540 WLAN_REASON_PREV_AUTH_NOT_VALID);
541 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
542 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
544 sta->acct_terminate_cause =
545 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
546 ap_free_sta(hapd, sta);
550 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
553 if (eloop_replenish_timeout(session_timeout, 0,
554 ap_handle_session_timer, hapd, sta) == 1) {
555 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
556 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
557 "to %d seconds", session_timeout);
562 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
565 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
566 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
567 "seconds", session_timeout);
568 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
569 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
574 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
576 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
580 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
583 struct hostapd_data *hapd = eloop_ctx;
584 struct sta_info *sta = timeout_ctx;
586 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
587 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
588 if (sta->hs20_session_info_url == NULL)
591 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
592 sta->hs20_disassoc_timer);
593 #endif /* CONFIG_WNM */
597 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
598 struct sta_info *sta, int warning_time)
600 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
601 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
606 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
608 struct sta_info *sta;
610 sta = ap_get_sta(hapd, addr);
614 wpa_printf(MSG_DEBUG, " New STA");
615 if (hapd->num_sta >= hapd->conf->max_num_sta) {
616 /* FIX: might try to remove some old STAs first? */
617 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
618 hapd->num_sta, hapd->conf->max_num_sta);
622 sta = os_zalloc(sizeof(struct sta_info));
624 wpa_printf(MSG_ERROR, "malloc failed");
627 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
628 accounting_sta_get_id(hapd, sta);
630 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
631 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
632 "for " MACSTR " (%d seconds - ap_max_inactivity)",
633 __func__, MAC2STR(addr),
634 hapd->conf->ap_max_inactivity);
635 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
636 ap_handle_timer, hapd, sta);
639 /* initialize STA info data */
640 os_memcpy(sta->addr, addr, ETH_ALEN);
641 sta->next = hapd->sta_list;
642 hapd->sta_list = sta;
644 ap_sta_hash_add(hapd, sta);
645 ap_sta_remove_in_other_bss(hapd, sta);
646 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
647 dl_list_init(&sta->ip6addr);
653 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
655 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
658 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
659 ap_sta_ip6addr_del(hapd, sta);
661 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
662 hapd->conf->iface, MAC2STR(sta->addr));
663 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
664 sta->flags & WLAN_STA_ASSOC) {
665 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
666 " from kernel driver",
667 hapd->conf->iface, MAC2STR(sta->addr));
674 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
675 struct sta_info *sta)
677 struct hostapd_iface *iface = hapd->iface;
680 for (i = 0; i < iface->num_bss; i++) {
681 struct hostapd_data *bss = iface->bss[i];
682 struct sta_info *sta2;
683 /* bss should always be set during operation, but it may be
684 * NULL during reconfiguration. Assume the STA is not
685 * associated to another BSS in that case to avoid NULL pointer
687 if (bss == hapd || bss == NULL)
689 sta2 = ap_get_sta(bss, sta->addr);
693 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
694 " association from another BSS %s",
695 hapd->conf->iface, MAC2STR(sta2->addr),
697 ap_sta_disconnect(bss, sta2, sta2->addr,
698 WLAN_REASON_PREV_AUTH_NOT_VALID);
703 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
705 struct hostapd_data *hapd = eloop_ctx;
706 struct sta_info *sta = timeout_ctx;
708 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
709 hapd->conf->iface, MAC2STR(sta->addr));
710 ap_sta_remove(hapd, sta);
711 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
715 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
718 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
719 hapd->conf->iface, MAC2STR(sta->addr));
720 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
721 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
722 ap_sta_set_authorized(hapd, sta, 0);
723 sta->timeout_next = STA_DEAUTH;
724 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
725 "for " MACSTR " (%d seconds - "
726 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
727 __func__, MAC2STR(sta->addr),
728 AP_MAX_INACTIVITY_AFTER_DISASSOC);
729 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
730 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
731 ap_handle_timer, hapd, sta);
732 accounting_sta_stop(hapd, sta);
733 ieee802_1x_free_station(hapd, sta);
735 sta->disassoc_reason = reason;
736 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
737 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
738 eloop_register_timeout(hapd->iface->drv_flags &
739 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
740 ap_sta_disassoc_cb_timeout, hapd, sta);
744 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
746 struct hostapd_data *hapd = eloop_ctx;
747 struct sta_info *sta = timeout_ctx;
749 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
750 hapd->conf->iface, MAC2STR(sta->addr));
751 ap_sta_remove(hapd, sta);
752 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
756 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
759 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
760 hapd->conf->iface, MAC2STR(sta->addr));
761 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
762 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
763 ap_sta_set_authorized(hapd, sta, 0);
764 sta->timeout_next = STA_REMOVE;
765 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
766 "for " MACSTR " (%d seconds - "
767 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
768 __func__, MAC2STR(sta->addr),
769 AP_MAX_INACTIVITY_AFTER_DEAUTH);
770 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
771 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
772 ap_handle_timer, hapd, sta);
773 accounting_sta_stop(hapd, sta);
774 ieee802_1x_free_station(hapd, sta);
776 sta->deauth_reason = reason;
777 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
778 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
779 eloop_register_timeout(hapd->iface->drv_flags &
780 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
781 ap_sta_deauth_cb_timeout, hapd, sta);
786 int ap_sta_wps_cancel(struct hostapd_data *hapd,
787 struct sta_info *sta, void *ctx)
789 if (sta && (sta->flags & WLAN_STA_WPS)) {
790 ap_sta_deauthenticate(hapd, sta,
791 WLAN_REASON_PREV_AUTH_NOT_VALID);
792 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
793 __func__, MAC2STR(sta->addr));
799 #endif /* CONFIG_WPS */
802 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
804 #ifndef CONFIG_NO_VLAN
806 struct hostapd_vlan *vlan = NULL;
808 int old_vlanid = sta->vlan_id_bound;
810 iface = hapd->conf->iface;
811 if (hapd->conf->ssid.vlan[0])
812 iface = hapd->conf->ssid.vlan;
814 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
816 else if (sta->vlan_id > 0) {
817 struct hostapd_vlan *wildcard_vlan = NULL;
818 vlan = hapd->conf->vlan;
820 if (vlan->vlan_id == sta->vlan_id)
822 if (vlan->vlan_id == VLAN_ID_WILDCARD)
823 wildcard_vlan = vlan;
827 vlan = wildcard_vlan;
829 iface = vlan->ifname;
833 * Do not increment ref counters if the VLAN ID remains same, but do
834 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
835 * have been called before.
837 if (sta->vlan_id == old_vlanid)
840 if (sta->vlan_id > 0 && vlan == NULL) {
841 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
842 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
843 "binding station to (vlan_id=%d)",
847 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
848 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
850 hostapd_logger(hapd, sta->addr,
851 HOSTAPD_MODULE_IEEE80211,
852 HOSTAPD_LEVEL_DEBUG, "could not add "
853 "dynamic VLAN interface for vlan_id=%d",
859 iface = vlan->ifname;
860 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
861 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
862 "interface '%s'", iface);
863 } else if (vlan && vlan->vlan_id == sta->vlan_id &&
864 vlan->dynamic_vlan > 0) {
865 vlan->dynamic_vlan++;
866 hostapd_logger(hapd, sta->addr,
867 HOSTAPD_MODULE_IEEE80211,
869 "updated existing dynamic VLAN interface '%s'",
873 /* ref counters have been increased, so mark the station */
874 sta->vlan_id_bound = sta->vlan_id;
877 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
878 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
881 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
882 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
884 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
886 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
887 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
888 "entry to vlan_id=%d", sta->vlan_id);
891 /* During 1x reauth, if the vlan id changes, then remove the old id. */
892 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
893 vlan_remove_dynamic(hapd, old_vlanid);
897 #else /* CONFIG_NO_VLAN */
899 #endif /* CONFIG_NO_VLAN */
903 #ifdef CONFIG_IEEE80211W
905 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
908 struct os_reltime now, passed;
909 os_get_reltime(&now);
910 os_reltime_sub(&now, &sta->sa_query_start, &passed);
911 tu = (passed.sec * 1000000 + passed.usec) / 1024;
912 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
913 hostapd_logger(hapd, sta->addr,
914 HOSTAPD_MODULE_IEEE80211,
916 "association SA Query timed out");
917 sta->sa_query_timed_out = 1;
918 os_free(sta->sa_query_trans_id);
919 sta->sa_query_trans_id = NULL;
920 sta->sa_query_count = 0;
921 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
929 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
931 struct hostapd_data *hapd = eloop_ctx;
932 struct sta_info *sta = timeout_ctx;
933 unsigned int timeout, sec, usec;
936 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
938 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
940 if (sta->sa_query_count > 0 &&
941 ap_check_sa_query_timeout(hapd, sta))
944 nbuf = os_realloc_array(sta->sa_query_trans_id,
945 sta->sa_query_count + 1,
946 WLAN_SA_QUERY_TR_ID_LEN);
949 if (sta->sa_query_count == 0) {
950 /* Starting a new SA Query procedure */
951 os_get_reltime(&sta->sa_query_start);
953 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
954 sta->sa_query_trans_id = nbuf;
955 sta->sa_query_count++;
957 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
959 * We don't really care which ID is used here, so simply
960 * hardcode this if the mostly theoretical os_get_random()
967 timeout = hapd->conf->assoc_sa_query_retry_timeout;
968 sec = ((timeout / 1000) * 1024) / 1000;
969 usec = (timeout % 1000) * 1024;
970 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
972 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
974 "association SA Query attempt %d", sta->sa_query_count);
976 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
980 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
982 ap_sa_query_timer(hapd, sta);
986 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
988 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
989 os_free(sta->sa_query_trans_id);
990 sta->sa_query_trans_id = NULL;
991 sta->sa_query_count = 0;
994 #endif /* CONFIG_IEEE80211W */
997 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1000 const u8 *dev_addr = NULL;
1005 #endif /* CONFIG_P2P */
1007 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1011 sta->flags |= WLAN_STA_AUTHORIZED;
1013 sta->flags &= ~WLAN_STA_AUTHORIZED;
1016 if (hapd->p2p_group == NULL) {
1017 if (sta->p2p_ie != NULL &&
1018 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1021 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1024 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1025 MAC2STR(sta->addr), MAC2STR(dev_addr));
1027 #endif /* CONFIG_P2P */
1028 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1030 if (hapd->sta_authorized_cb)
1031 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1032 sta->addr, authorized, dev_addr);
1038 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1039 os_snprintf(ip_addr, sizeof(ip_addr),
1040 " ip_addr=%u.%u.%u.%u",
1041 ip_addr_buf[0], ip_addr_buf[1],
1042 ip_addr_buf[2], ip_addr_buf[3]);
1044 #endif /* CONFIG_P2P */
1046 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1049 if (hapd->msg_ctx_parent &&
1050 hapd->msg_ctx_parent != hapd->msg_ctx)
1051 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1052 AP_STA_CONNECTED "%s%s",
1055 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1057 if (hapd->msg_ctx_parent &&
1058 hapd->msg_ctx_parent != hapd->msg_ctx)
1059 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1060 AP_STA_DISCONNECTED "%s", buf);
1064 if (hapd->iface->fst) {
1066 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1068 fst_notify_peer_disconnected(hapd->iface->fst,
1071 #endif /* CONFIG_FST */
1075 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1076 const u8 *addr, u16 reason)
1079 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1080 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1083 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1084 hapd->conf->iface, __func__, MAC2STR(addr),
1087 if (sta == NULL && addr)
1088 sta = ap_get_sta(hapd, addr);
1091 hostapd_drv_sta_deauth(hapd, addr, reason);
1095 ap_sta_set_authorized(hapd, sta, 0);
1096 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1097 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1098 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1099 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
1100 "for " MACSTR " (%d seconds - "
1101 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1102 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1103 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1104 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1105 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1106 ap_handle_timer, hapd, sta);
1107 sta->timeout_next = STA_REMOVE;
1109 sta->deauth_reason = reason;
1110 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1111 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1112 eloop_register_timeout(hapd->iface->drv_flags &
1113 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1114 ap_sta_deauth_cb_timeout, hapd, sta);
1118 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1120 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1121 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1124 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1125 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1126 ap_sta_deauth_cb_timeout(hapd, sta);
1130 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1132 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1133 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1136 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1137 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1138 ap_sta_disassoc_cb_timeout(hapd, sta);
1142 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1143 struct sta_info *sta)
1145 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1146 wpa_printf(MSG_DEBUG,
1147 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1149 hapd->conf->iface, MAC2STR(sta->addr));
1150 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1151 wpa_printf(MSG_DEBUG,
1152 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1154 hapd->conf->iface, MAC2STR(sta->addr));
1158 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1163 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1164 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1165 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1166 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1167 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1169 (flags & WLAN_STA_SHORT_PREAMBLE ?
1170 "[SHORT_PREAMBLE]" : ""),
1171 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1172 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1173 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1174 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1175 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1176 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1177 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1178 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1179 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1180 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1181 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1182 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1183 "[WNM_SLEEP_MODE]" : ""));
1184 if (os_snprintf_error(buflen, res))