2 * hostapd / Station table
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/sae.h"
16 #include "radius/radius.h"
17 #include "radius/radius_client.h"
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
23 #include "ieee802_11_auth.h"
25 #include "preauth_auth.h"
26 #include "ap_config.h"
29 #include "vlan_init.h"
30 #include "p2p_hostapd.h"
31 #include "ap_drv_ops.h"
34 #include "ndisc_snoop.h"
37 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
38 struct sta_info *sta);
39 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
40 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
41 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
42 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43 #ifdef CONFIG_IEEE80211W
44 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
45 #endif /* CONFIG_IEEE80211W */
46 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
48 int ap_for_each_sta(struct hostapd_data *hapd,
49 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
55 for (sta = hapd->sta_list; sta; sta = sta->next) {
56 if (cb(hapd, sta, ctx))
64 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
68 s = hapd->sta_hash[STA_HASH(sta)];
69 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
76 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
80 for (sta = hapd->sta_list; sta; sta = sta->next) {
81 const u8 *p2p_dev_addr;
83 if (sta->p2p_ie == NULL)
86 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
87 if (p2p_dev_addr == NULL)
90 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
96 #endif /* CONFIG_P2P */
99 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
101 struct sta_info *tmp;
103 if (hapd->sta_list == sta) {
104 hapd->sta_list = sta->next;
108 tmp = hapd->sta_list;
109 while (tmp != NULL && tmp->next != sta)
112 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
113 "list.", MAC2STR(sta->addr));
115 tmp->next = sta->next;
119 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
121 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
122 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
126 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
130 s = hapd->sta_hash[STA_HASH(sta->addr)];
131 if (s == NULL) return;
132 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
133 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
137 while (s->hnext != NULL &&
138 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
140 if (s->hnext != NULL)
141 s->hnext = s->hnext->hnext;
143 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
144 " from hash table", MAC2STR(sta->addr));
148 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
150 sta_ip6addr_del(hapd, sta);
154 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
158 accounting_sta_stop(hapd, sta);
161 ap_sta_set_authorized(hapd, sta, 0);
163 if (sta->flags & WLAN_STA_WDS)
164 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
167 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
168 ap_sta_ip6addr_del(hapd, sta);
170 if (!hapd->iface->driver_ap_teardown &&
171 !(sta->flags & WLAN_STA_PREAUTH))
172 hostapd_drv_sta_remove(hapd, sta->addr);
174 ap_sta_hash_del(hapd, sta);
175 ap_sta_list_del(hapd, sta);
178 hapd->sta_aid[(sta->aid - 1) / 32] &=
179 ~BIT((sta->aid - 1) % 32);
182 if (sta->nonerp_set) {
184 hapd->iface->num_sta_non_erp--;
185 if (hapd->iface->num_sta_non_erp == 0)
189 if (sta->no_short_slot_time_set) {
190 sta->no_short_slot_time_set = 0;
191 hapd->iface->num_sta_no_short_slot_time--;
192 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
193 && hapd->iface->num_sta_no_short_slot_time == 0)
197 if (sta->no_short_preamble_set) {
198 sta->no_short_preamble_set = 0;
199 hapd->iface->num_sta_no_short_preamble--;
200 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
201 && hapd->iface->num_sta_no_short_preamble == 0)
205 if (sta->no_ht_gf_set) {
206 sta->no_ht_gf_set = 0;
207 hapd->iface->num_sta_ht_no_gf--;
210 if (sta->no_ht_set) {
212 hapd->iface->num_sta_no_ht--;
215 if (sta->ht_20mhz_set) {
216 sta->ht_20mhz_set = 0;
217 hapd->iface->num_sta_ht_20mhz--;
220 #ifdef CONFIG_IEEE80211N
221 ht40_intolerant_remove(hapd->iface, sta);
222 #endif /* CONFIG_IEEE80211N */
225 if (sta->no_p2p_set) {
227 hapd->num_sta_no_p2p--;
228 if (hapd->num_sta_no_p2p == 0)
229 hostapd_p2p_non_p2p_sta_disconnected(hapd);
231 #endif /* CONFIG_P2P */
233 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
234 if (hostapd_ht_operation_update(hapd->iface) > 0)
236 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
239 if (hapd->mesh_sta_free_cb)
240 hapd->mesh_sta_free_cb(sta);
241 #endif /* CONFIG_MESH */
244 ieee802_11_set_beacons(hapd->iface);
246 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
247 __func__, MAC2STR(sta->addr));
248 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
249 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
250 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
251 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
252 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
253 sae_clear_retransmit_timer(hapd, sta);
255 ieee802_1x_free_station(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 os_free(sta->challenge);
265 #ifdef CONFIG_IEEE80211W
266 os_free(sta->sa_query_trans_id);
267 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
268 #endif /* CONFIG_IEEE80211W */
271 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
272 #endif /* CONFIG_P2P */
274 #ifdef CONFIG_INTERWORKING
275 if (sta->gas_dialog) {
277 for (i = 0; i < GAS_DIALOG_MAX; i++)
278 gas_serv_dialog_clear(&sta->gas_dialog[i]);
279 os_free(sta->gas_dialog);
281 #endif /* CONFIG_INTERWORKING */
283 wpabuf_free(sta->wps_ie);
284 wpabuf_free(sta->p2p_ie);
285 wpabuf_free(sta->hs20_ie);
287 os_free(sta->ht_capabilities);
288 os_free(sta->vht_capabilities);
289 hostapd_free_psk_list(sta->psk);
290 os_free(sta->identity);
291 os_free(sta->radius_cui);
292 os_free(sta->remediation_url);
293 wpabuf_free(sta->hs20_deauth_req);
294 os_free(sta->hs20_session_info_url);
297 sae_clear_data(sta->sae);
299 #endif /* CONFIG_SAE */
305 void hostapd_free_stas(struct hostapd_data *hapd)
307 struct sta_info *sta, *prev;
309 sta = hapd->sta_list;
313 if (sta->flags & WLAN_STA_AUTH) {
314 mlme_deauthenticate_indication(
315 hapd, sta, WLAN_REASON_UNSPECIFIED);
318 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
319 MAC2STR(prev->addr));
320 ap_free_sta(hapd, prev);
326 * ap_handle_timer - Per STA timer handler
327 * @eloop_ctx: struct hostapd_data *
328 * @timeout_ctx: struct sta_info *
330 * This function is called to check station activity and to remove inactive
333 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
335 struct hostapd_data *hapd = eloop_ctx;
336 struct sta_info *sta = timeout_ctx;
337 unsigned long next_time = 0;
340 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
341 __func__, MAC2STR(sta->addr), sta->flags,
343 if (sta->timeout_next == STA_REMOVE) {
344 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
345 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
346 "local deauth request");
347 ap_free_sta(hapd, sta);
351 if ((sta->flags & WLAN_STA_ASSOC) &&
352 (sta->timeout_next == STA_NULLFUNC ||
353 sta->timeout_next == STA_DISASSOC)) {
356 * Add random value to timeout so that we don't end up bouncing
357 * all stations at the same time if we have lots of associated
358 * stations that are idle (but keep re-associating).
360 int fuzz = os_random() % 20;
361 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
362 if (inactive_sec == -1) {
363 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
364 "Check inactivity: Could not "
365 "get station info from kernel driver for "
366 MACSTR, MAC2STR(sta->addr));
368 * The driver may not support this functionality.
369 * Anyway, try again after the next inactivity timeout,
370 * but do not disconnect the station now.
372 next_time = hapd->conf->ap_max_inactivity + fuzz;
373 } else if (inactive_sec == -ENOENT) {
374 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
375 "Station " MACSTR " has lost its driver entry",
378 if (hapd->conf->skip_inactivity_poll)
379 sta->timeout_next = STA_DISASSOC;
380 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
381 /* station activity detected; reset timeout state */
382 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
383 "Station " MACSTR " has been active %is ago",
384 MAC2STR(sta->addr), inactive_sec);
385 sta->timeout_next = STA_NULLFUNC;
386 next_time = hapd->conf->ap_max_inactivity + fuzz -
389 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
390 "Station " MACSTR " has been "
391 "inactive too long: %d sec, max allowed: %d",
392 MAC2STR(sta->addr), inactive_sec,
393 hapd->conf->ap_max_inactivity);
395 if (hapd->conf->skip_inactivity_poll)
396 sta->timeout_next = STA_DISASSOC;
400 if ((sta->flags & WLAN_STA_ASSOC) &&
401 sta->timeout_next == STA_DISASSOC &&
402 !(sta->flags & WLAN_STA_PENDING_POLL) &&
403 !hapd->conf->skip_inactivity_poll) {
404 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
405 " has ACKed data poll", MAC2STR(sta->addr));
406 /* data nullfunc frame poll did not produce TX errors; assume
407 * station ACKed it */
408 sta->timeout_next = STA_NULLFUNC;
409 next_time = hapd->conf->ap_max_inactivity;
413 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
414 "for " MACSTR " (%lu seconds)",
415 __func__, MAC2STR(sta->addr), next_time);
416 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
421 if (sta->timeout_next == STA_NULLFUNC &&
422 (sta->flags & WLAN_STA_ASSOC)) {
423 wpa_printf(MSG_DEBUG, " Polling STA");
424 sta->flags |= WLAN_STA_PENDING_POLL;
425 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
426 sta->flags & WLAN_STA_WMM);
427 } else if (sta->timeout_next != STA_REMOVE) {
428 int deauth = sta->timeout_next == STA_DEAUTH;
430 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
431 "Timeout, sending %s info to STA " MACSTR,
432 deauth ? "deauthentication" : "disassociation",
436 hostapd_drv_sta_deauth(
438 WLAN_REASON_PREV_AUTH_NOT_VALID);
440 reason = (sta->timeout_next == STA_DISASSOC) ?
441 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
442 WLAN_REASON_PREV_AUTH_NOT_VALID;
444 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
448 switch (sta->timeout_next) {
450 sta->timeout_next = STA_DISASSOC;
451 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
452 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
453 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
454 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
458 case STA_DISASSOC_FROM_CLI:
459 ap_sta_set_authorized(hapd, sta, 0);
460 sta->flags &= ~WLAN_STA_ASSOC;
461 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
462 if (!sta->acct_terminate_cause)
463 sta->acct_terminate_cause =
464 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
465 accounting_sta_stop(hapd, sta);
466 ieee802_1x_free_station(sta);
467 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
468 HOSTAPD_LEVEL_INFO, "disassociated due to "
470 reason = (sta->timeout_next == STA_DISASSOC) ?
471 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
472 WLAN_REASON_PREV_AUTH_NOT_VALID;
473 sta->timeout_next = STA_DEAUTH;
474 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
475 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
476 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
477 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
479 mlme_disassociate_indication(hapd, sta, reason);
483 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
484 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
485 "inactivity (timer DEAUTH/REMOVE)");
486 if (!sta->acct_terminate_cause)
487 sta->acct_terminate_cause =
488 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
489 mlme_deauthenticate_indication(
491 WLAN_REASON_PREV_AUTH_NOT_VALID);
492 ap_free_sta(hapd, sta);
498 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
500 struct hostapd_data *hapd = eloop_ctx;
501 struct sta_info *sta = timeout_ctx;
503 if (!(sta->flags & WLAN_STA_AUTH)) {
504 if (sta->flags & WLAN_STA_GAS) {
505 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
506 "entry " MACSTR, MAC2STR(sta->addr));
507 ap_free_sta(hapd, sta);
512 hostapd_drv_sta_deauth(hapd, sta->addr,
513 WLAN_REASON_PREV_AUTH_NOT_VALID);
514 mlme_deauthenticate_indication(hapd, sta,
515 WLAN_REASON_PREV_AUTH_NOT_VALID);
516 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
517 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
519 sta->acct_terminate_cause =
520 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
521 ap_free_sta(hapd, sta);
525 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
528 if (eloop_replenish_timeout(session_timeout, 0,
529 ap_handle_session_timer, hapd, sta) == 1) {
530 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
531 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
532 "to %d seconds", session_timeout);
537 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
540 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
541 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
542 "seconds", session_timeout);
543 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
544 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
549 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
551 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
555 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
558 struct hostapd_data *hapd = eloop_ctx;
559 struct sta_info *sta = timeout_ctx;
561 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
563 if (sta->hs20_session_info_url == NULL)
566 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
567 sta->hs20_disassoc_timer);
568 #endif /* CONFIG_WNM */
572 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
573 struct sta_info *sta, int warning_time)
575 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
576 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
581 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
583 struct sta_info *sta;
585 sta = ap_get_sta(hapd, addr);
589 wpa_printf(MSG_DEBUG, " New STA");
590 if (hapd->num_sta >= hapd->conf->max_num_sta) {
591 /* FIX: might try to remove some old STAs first? */
592 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
593 hapd->num_sta, hapd->conf->max_num_sta);
597 sta = os_zalloc(sizeof(struct sta_info));
599 wpa_printf(MSG_ERROR, "malloc failed");
602 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
603 accounting_sta_get_id(hapd, sta);
605 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
606 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
607 "for " MACSTR " (%d seconds - ap_max_inactivity)",
608 __func__, MAC2STR(addr),
609 hapd->conf->ap_max_inactivity);
610 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
611 ap_handle_timer, hapd, sta);
614 /* initialize STA info data */
615 os_memcpy(sta->addr, addr, ETH_ALEN);
616 sta->next = hapd->sta_list;
617 hapd->sta_list = sta;
619 ap_sta_hash_add(hapd, sta);
620 sta->ssid = &hapd->conf->ssid;
621 ap_sta_remove_in_other_bss(hapd, sta);
622 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
623 dl_list_init(&sta->ip6addr);
629 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
631 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
634 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
635 ap_sta_ip6addr_del(hapd, sta);
637 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
639 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
640 sta->flags & WLAN_STA_ASSOC) {
641 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
642 " from kernel driver.", MAC2STR(sta->addr));
649 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
650 struct sta_info *sta)
652 struct hostapd_iface *iface = hapd->iface;
655 for (i = 0; i < iface->num_bss; i++) {
656 struct hostapd_data *bss = iface->bss[i];
657 struct sta_info *sta2;
658 /* bss should always be set during operation, but it may be
659 * NULL during reconfiguration. Assume the STA is not
660 * associated to another BSS in that case to avoid NULL pointer
662 if (bss == hapd || bss == NULL)
664 sta2 = ap_get_sta(bss, sta->addr);
668 ap_sta_disconnect(bss, sta2, sta2->addr,
669 WLAN_REASON_PREV_AUTH_NOT_VALID);
674 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
676 struct hostapd_data *hapd = eloop_ctx;
677 struct sta_info *sta = timeout_ctx;
679 ap_sta_remove(hapd, sta);
680 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
684 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
687 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
688 hapd->conf->iface, MAC2STR(sta->addr));
689 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
690 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
691 ap_sta_set_authorized(hapd, sta, 0);
692 sta->timeout_next = STA_DEAUTH;
693 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
694 "for " MACSTR " (%d seconds - "
695 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
696 __func__, MAC2STR(sta->addr),
697 AP_MAX_INACTIVITY_AFTER_DISASSOC);
698 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
699 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
700 ap_handle_timer, hapd, sta);
701 accounting_sta_stop(hapd, sta);
702 ieee802_1x_free_station(sta);
704 sta->disassoc_reason = reason;
705 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
706 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
707 eloop_register_timeout(hapd->iface->drv_flags &
708 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
709 ap_sta_disassoc_cb_timeout, hapd, sta);
713 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
715 struct hostapd_data *hapd = eloop_ctx;
716 struct sta_info *sta = timeout_ctx;
718 ap_sta_remove(hapd, sta);
719 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
723 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
726 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
727 hapd->conf->iface, MAC2STR(sta->addr));
728 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
729 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
730 ap_sta_set_authorized(hapd, sta, 0);
731 sta->timeout_next = STA_REMOVE;
732 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
733 "for " MACSTR " (%d seconds - "
734 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
735 __func__, MAC2STR(sta->addr),
736 AP_MAX_INACTIVITY_AFTER_DEAUTH);
737 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
738 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
739 ap_handle_timer, hapd, sta);
740 accounting_sta_stop(hapd, sta);
741 ieee802_1x_free_station(sta);
743 sta->deauth_reason = reason;
744 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
745 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
746 eloop_register_timeout(hapd->iface->drv_flags &
747 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
748 ap_sta_deauth_cb_timeout, hapd, sta);
753 int ap_sta_wps_cancel(struct hostapd_data *hapd,
754 struct sta_info *sta, void *ctx)
756 if (sta && (sta->flags & WLAN_STA_WPS)) {
757 ap_sta_deauthenticate(hapd, sta,
758 WLAN_REASON_PREV_AUTH_NOT_VALID);
759 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
760 __func__, MAC2STR(sta->addr));
766 #endif /* CONFIG_WPS */
769 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
772 #ifndef CONFIG_NO_VLAN
774 struct hostapd_vlan *vlan = NULL;
778 * Do not proceed furthur if the vlan id remains same. We do not want
779 * duplicate dynamic vlan entries.
781 if (sta->vlan_id == old_vlanid)
785 * During 1x reauth, if the vlan id changes, then remove the old id and
786 * proceed furthur to add the new one.
789 vlan_remove_dynamic(hapd, old_vlanid);
791 iface = hapd->conf->iface;
792 if (sta->ssid->vlan[0])
793 iface = sta->ssid->vlan;
795 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
797 else if (sta->vlan_id > 0) {
798 struct hostapd_vlan *wildcard_vlan = NULL;
799 vlan = hapd->conf->vlan;
801 if (vlan->vlan_id == sta->vlan_id)
803 if (vlan->vlan_id == VLAN_ID_WILDCARD)
804 wildcard_vlan = vlan;
808 vlan = wildcard_vlan;
810 iface = vlan->ifname;
813 if (sta->vlan_id > 0 && vlan == NULL) {
814 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
815 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
816 "binding station to (vlan_id=%d)",
819 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
820 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
822 hostapd_logger(hapd, sta->addr,
823 HOSTAPD_MODULE_IEEE80211,
824 HOSTAPD_LEVEL_DEBUG, "could not add "
825 "dynamic VLAN interface for vlan_id=%d",
830 iface = vlan->ifname;
831 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
832 hostapd_logger(hapd, sta->addr,
833 HOSTAPD_MODULE_IEEE80211,
834 HOSTAPD_LEVEL_DEBUG, "could not "
835 "configure encryption for dynamic VLAN "
836 "interface for vlan_id=%d",
840 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
841 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
842 "interface '%s'", iface);
843 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
844 if (sta->vlan_id > 0) {
845 vlan->dynamic_vlan++;
846 hostapd_logger(hapd, sta->addr,
847 HOSTAPD_MODULE_IEEE80211,
848 HOSTAPD_LEVEL_DEBUG, "updated existing "
849 "dynamic VLAN interface '%s'", iface);
853 * Update encryption configuration for statically generated
854 * VLAN interface. This is only used for static WEP
855 * configuration for the case where hostapd did not yet know
856 * which keys are to be used when the interface was added.
858 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
859 hostapd_logger(hapd, sta->addr,
860 HOSTAPD_MODULE_IEEE80211,
861 HOSTAPD_LEVEL_DEBUG, "could not "
862 "configure encryption for VLAN "
863 "interface for vlan_id=%d",
868 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
869 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
872 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
873 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
875 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
877 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
878 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
879 "entry to vlan_id=%d", sta->vlan_id);
882 #else /* CONFIG_NO_VLAN */
884 #endif /* CONFIG_NO_VLAN */
888 #ifdef CONFIG_IEEE80211W
890 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
893 struct os_reltime now, passed;
894 os_get_reltime(&now);
895 os_reltime_sub(&now, &sta->sa_query_start, &passed);
896 tu = (passed.sec * 1000000 + passed.usec) / 1024;
897 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
898 hostapd_logger(hapd, sta->addr,
899 HOSTAPD_MODULE_IEEE80211,
901 "association SA Query timed out");
902 sta->sa_query_timed_out = 1;
903 os_free(sta->sa_query_trans_id);
904 sta->sa_query_trans_id = NULL;
905 sta->sa_query_count = 0;
906 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
914 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
916 struct hostapd_data *hapd = eloop_ctx;
917 struct sta_info *sta = timeout_ctx;
918 unsigned int timeout, sec, usec;
921 if (sta->sa_query_count > 0 &&
922 ap_check_sa_query_timeout(hapd, sta))
925 nbuf = os_realloc_array(sta->sa_query_trans_id,
926 sta->sa_query_count + 1,
927 WLAN_SA_QUERY_TR_ID_LEN);
930 if (sta->sa_query_count == 0) {
931 /* Starting a new SA Query procedure */
932 os_get_reltime(&sta->sa_query_start);
934 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
935 sta->sa_query_trans_id = nbuf;
936 sta->sa_query_count++;
938 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
940 * We don't really care which ID is used here, so simply
941 * hardcode this if the mostly theoretical os_get_random()
948 timeout = hapd->conf->assoc_sa_query_retry_timeout;
949 sec = ((timeout / 1000) * 1024) / 1000;
950 usec = (timeout % 1000) * 1024;
951 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
953 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
955 "association SA Query attempt %d", sta->sa_query_count);
957 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
961 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
963 ap_sa_query_timer(hapd, sta);
967 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
969 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
970 os_free(sta->sa_query_trans_id);
971 sta->sa_query_trans_id = NULL;
972 sta->sa_query_count = 0;
975 #endif /* CONFIG_IEEE80211W */
978 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
981 const u8 *dev_addr = NULL;
986 #endif /* CONFIG_P2P */
988 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
992 sta->flags |= WLAN_STA_AUTHORIZED;
994 sta->flags &= ~WLAN_STA_AUTHORIZED;
997 if (hapd->p2p_group == NULL) {
998 if (sta->p2p_ie != NULL &&
999 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1002 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1005 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1006 MAC2STR(sta->addr), MAC2STR(dev_addr));
1008 #endif /* CONFIG_P2P */
1009 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1011 if (hapd->sta_authorized_cb)
1012 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1013 sta->addr, authorized, dev_addr);
1019 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1020 os_snprintf(ip_addr, sizeof(ip_addr),
1021 " ip_addr=%u.%u.%u.%u",
1022 ip_addr_buf[0], ip_addr_buf[1],
1023 ip_addr_buf[2], ip_addr_buf[3]);
1025 #endif /* CONFIG_P2P */
1027 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1030 if (hapd->msg_ctx_parent &&
1031 hapd->msg_ctx_parent != hapd->msg_ctx)
1032 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1033 AP_STA_CONNECTED "%s%s",
1036 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1038 if (hapd->msg_ctx_parent &&
1039 hapd->msg_ctx_parent != hapd->msg_ctx)
1040 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1041 AP_STA_DISCONNECTED "%s", buf);
1046 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1047 const u8 *addr, u16 reason)
1050 if (sta == NULL && addr)
1051 sta = ap_get_sta(hapd, addr);
1054 hostapd_drv_sta_deauth(hapd, addr, reason);
1058 ap_sta_set_authorized(hapd, sta, 0);
1059 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1060 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1061 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1062 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1063 "for " MACSTR " (%d seconds - "
1064 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1065 __func__, MAC2STR(sta->addr),
1066 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1067 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1068 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1069 ap_handle_timer, hapd, sta);
1070 sta->timeout_next = STA_REMOVE;
1072 sta->deauth_reason = reason;
1073 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1074 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1075 eloop_register_timeout(hapd->iface->drv_flags &
1076 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1077 ap_sta_deauth_cb_timeout, hapd, sta);
1081 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1083 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1084 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1087 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1088 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1089 ap_sta_deauth_cb_timeout(hapd, sta);
1093 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1095 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1096 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1099 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1100 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1101 ap_sta_disassoc_cb_timeout(hapd, sta);
1105 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1110 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1111 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1112 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1113 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1114 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1116 (flags & WLAN_STA_SHORT_PREAMBLE ?
1117 "[SHORT_PREAMBLE]" : ""),
1118 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1119 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1120 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1121 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1122 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1123 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1124 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1125 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1126 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1127 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1128 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1129 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1130 "[WNM_SLEEP_MODE]" : ""));
1131 if (os_snprintf_error(buflen, res))