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 < hapd->conf->ap_max_inactivity &&
374 sta->flags & WLAN_STA_ASSOC) {
375 /* station activity detected; reset timeout state */
376 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
377 "Station " MACSTR " has been active %is ago",
378 MAC2STR(sta->addr), inactive_sec);
379 sta->timeout_next = STA_NULLFUNC;
380 next_time = hapd->conf->ap_max_inactivity + fuzz -
383 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
384 "Station " MACSTR " has been "
385 "inactive too long: %d sec, max allowed: %d",
386 MAC2STR(sta->addr), inactive_sec,
387 hapd->conf->ap_max_inactivity);
389 if (hapd->conf->skip_inactivity_poll)
390 sta->timeout_next = STA_DISASSOC;
394 if ((sta->flags & WLAN_STA_ASSOC) &&
395 sta->timeout_next == STA_DISASSOC &&
396 !(sta->flags & WLAN_STA_PENDING_POLL) &&
397 !hapd->conf->skip_inactivity_poll) {
398 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
399 " has ACKed data poll", MAC2STR(sta->addr));
400 /* data nullfunc frame poll did not produce TX errors; assume
401 * station ACKed it */
402 sta->timeout_next = STA_NULLFUNC;
403 next_time = hapd->conf->ap_max_inactivity;
407 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
408 "for " MACSTR " (%lu seconds)",
409 __func__, MAC2STR(sta->addr), next_time);
410 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
415 if (sta->timeout_next == STA_NULLFUNC &&
416 (sta->flags & WLAN_STA_ASSOC)) {
417 wpa_printf(MSG_DEBUG, " Polling STA");
418 sta->flags |= WLAN_STA_PENDING_POLL;
419 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
420 sta->flags & WLAN_STA_WMM);
421 } else if (sta->timeout_next != STA_REMOVE) {
422 int deauth = sta->timeout_next == STA_DEAUTH;
424 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
425 "Timeout, sending %s info to STA " MACSTR,
426 deauth ? "deauthentication" : "disassociation",
430 hostapd_drv_sta_deauth(
432 WLAN_REASON_PREV_AUTH_NOT_VALID);
434 reason = (sta->timeout_next == STA_DISASSOC) ?
435 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
436 WLAN_REASON_PREV_AUTH_NOT_VALID;
438 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
442 switch (sta->timeout_next) {
444 sta->timeout_next = STA_DISASSOC;
445 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
446 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
447 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
448 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
452 case STA_DISASSOC_FROM_CLI:
453 ap_sta_set_authorized(hapd, sta, 0);
454 sta->flags &= ~WLAN_STA_ASSOC;
455 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
456 if (!sta->acct_terminate_cause)
457 sta->acct_terminate_cause =
458 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
459 accounting_sta_stop(hapd, sta);
460 ieee802_1x_free_station(sta);
461 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
462 HOSTAPD_LEVEL_INFO, "disassociated due to "
464 reason = (sta->timeout_next == STA_DISASSOC) ?
465 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
466 WLAN_REASON_PREV_AUTH_NOT_VALID;
467 sta->timeout_next = STA_DEAUTH;
468 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
469 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
470 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
471 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
473 mlme_disassociate_indication(hapd, sta, reason);
477 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
478 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
479 "inactivity (timer DEAUTH/REMOVE)");
480 if (!sta->acct_terminate_cause)
481 sta->acct_terminate_cause =
482 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
483 mlme_deauthenticate_indication(
485 WLAN_REASON_PREV_AUTH_NOT_VALID);
486 ap_free_sta(hapd, sta);
492 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
494 struct hostapd_data *hapd = eloop_ctx;
495 struct sta_info *sta = timeout_ctx;
497 if (!(sta->flags & WLAN_STA_AUTH)) {
498 if (sta->flags & WLAN_STA_GAS) {
499 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
500 "entry " MACSTR, MAC2STR(sta->addr));
501 ap_free_sta(hapd, sta);
506 hostapd_drv_sta_deauth(hapd, sta->addr,
507 WLAN_REASON_PREV_AUTH_NOT_VALID);
508 mlme_deauthenticate_indication(hapd, sta,
509 WLAN_REASON_PREV_AUTH_NOT_VALID);
510 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
511 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
513 sta->acct_terminate_cause =
514 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
515 ap_free_sta(hapd, sta);
519 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
522 if (eloop_replenish_timeout(session_timeout, 0,
523 ap_handle_session_timer, hapd, sta) == 1) {
524 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
525 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
526 "to %d seconds", session_timeout);
531 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
534 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
535 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
536 "seconds", session_timeout);
537 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
538 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
543 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
545 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
549 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
552 struct hostapd_data *hapd = eloop_ctx;
553 struct sta_info *sta = timeout_ctx;
555 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
557 if (sta->hs20_session_info_url == NULL)
560 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
561 sta->hs20_disassoc_timer);
562 #endif /* CONFIG_WNM */
566 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
567 struct sta_info *sta, int warning_time)
569 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
570 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
575 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
577 struct sta_info *sta;
579 sta = ap_get_sta(hapd, addr);
583 wpa_printf(MSG_DEBUG, " New STA");
584 if (hapd->num_sta >= hapd->conf->max_num_sta) {
585 /* FIX: might try to remove some old STAs first? */
586 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
587 hapd->num_sta, hapd->conf->max_num_sta);
591 sta = os_zalloc(sizeof(struct sta_info));
593 wpa_printf(MSG_ERROR, "malloc failed");
596 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
597 accounting_sta_get_id(hapd, sta);
599 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
600 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
601 "for " MACSTR " (%d seconds - ap_max_inactivity)",
602 __func__, MAC2STR(addr),
603 hapd->conf->ap_max_inactivity);
604 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
605 ap_handle_timer, hapd, sta);
608 /* initialize STA info data */
609 os_memcpy(sta->addr, addr, ETH_ALEN);
610 sta->next = hapd->sta_list;
611 hapd->sta_list = sta;
613 ap_sta_hash_add(hapd, sta);
614 sta->ssid = &hapd->conf->ssid;
615 ap_sta_remove_in_other_bss(hapd, sta);
616 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
617 dl_list_init(&sta->ip6addr);
623 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
625 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
628 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
629 ap_sta_ip6addr_del(hapd, sta);
631 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
633 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
634 sta->flags & WLAN_STA_ASSOC) {
635 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
636 " from kernel driver.", MAC2STR(sta->addr));
643 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
644 struct sta_info *sta)
646 struct hostapd_iface *iface = hapd->iface;
649 for (i = 0; i < iface->num_bss; i++) {
650 struct hostapd_data *bss = iface->bss[i];
651 struct sta_info *sta2;
652 /* bss should always be set during operation, but it may be
653 * NULL during reconfiguration. Assume the STA is not
654 * associated to another BSS in that case to avoid NULL pointer
656 if (bss == hapd || bss == NULL)
658 sta2 = ap_get_sta(bss, sta->addr);
662 ap_sta_disconnect(bss, sta2, sta2->addr,
663 WLAN_REASON_PREV_AUTH_NOT_VALID);
668 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
670 struct hostapd_data *hapd = eloop_ctx;
671 struct sta_info *sta = timeout_ctx;
673 ap_sta_remove(hapd, sta);
674 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
678 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
681 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
682 hapd->conf->iface, MAC2STR(sta->addr));
683 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
684 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
685 ap_sta_set_authorized(hapd, sta, 0);
686 sta->timeout_next = STA_DEAUTH;
687 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
688 "for " MACSTR " (%d seconds - "
689 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
690 __func__, MAC2STR(sta->addr),
691 AP_MAX_INACTIVITY_AFTER_DISASSOC);
692 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
693 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
694 ap_handle_timer, hapd, sta);
695 accounting_sta_stop(hapd, sta);
696 ieee802_1x_free_station(sta);
698 sta->disassoc_reason = reason;
699 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
700 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
701 eloop_register_timeout(hapd->iface->drv_flags &
702 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
703 ap_sta_disassoc_cb_timeout, hapd, sta);
707 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
709 struct hostapd_data *hapd = eloop_ctx;
710 struct sta_info *sta = timeout_ctx;
712 ap_sta_remove(hapd, sta);
713 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
717 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
720 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
721 hapd->conf->iface, MAC2STR(sta->addr));
722 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
723 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
724 ap_sta_set_authorized(hapd, sta, 0);
725 sta->timeout_next = STA_REMOVE;
726 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
727 "for " MACSTR " (%d seconds - "
728 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
729 __func__, MAC2STR(sta->addr),
730 AP_MAX_INACTIVITY_AFTER_DEAUTH);
731 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
732 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
733 ap_handle_timer, hapd, sta);
734 accounting_sta_stop(hapd, sta);
735 ieee802_1x_free_station(sta);
737 sta->deauth_reason = reason;
738 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
739 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
740 eloop_register_timeout(hapd->iface->drv_flags &
741 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
742 ap_sta_deauth_cb_timeout, hapd, sta);
747 int ap_sta_wps_cancel(struct hostapd_data *hapd,
748 struct sta_info *sta, void *ctx)
750 if (sta && (sta->flags & WLAN_STA_WPS)) {
751 ap_sta_deauthenticate(hapd, sta,
752 WLAN_REASON_PREV_AUTH_NOT_VALID);
753 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
754 __func__, MAC2STR(sta->addr));
760 #endif /* CONFIG_WPS */
763 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
766 #ifndef CONFIG_NO_VLAN
768 struct hostapd_vlan *vlan = NULL;
772 * Do not proceed furthur if the vlan id remains same. We do not want
773 * duplicate dynamic vlan entries.
775 if (sta->vlan_id == old_vlanid)
779 * During 1x reauth, if the vlan id changes, then remove the old id and
780 * proceed furthur to add the new one.
783 vlan_remove_dynamic(hapd, old_vlanid);
785 iface = hapd->conf->iface;
786 if (sta->ssid->vlan[0])
787 iface = sta->ssid->vlan;
789 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
791 else if (sta->vlan_id > 0) {
792 struct hostapd_vlan *wildcard_vlan = NULL;
793 vlan = hapd->conf->vlan;
795 if (vlan->vlan_id == sta->vlan_id)
797 if (vlan->vlan_id == VLAN_ID_WILDCARD)
798 wildcard_vlan = vlan;
802 vlan = wildcard_vlan;
804 iface = vlan->ifname;
807 if (sta->vlan_id > 0 && vlan == NULL) {
808 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
809 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
810 "binding station to (vlan_id=%d)",
813 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
814 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
816 hostapd_logger(hapd, sta->addr,
817 HOSTAPD_MODULE_IEEE80211,
818 HOSTAPD_LEVEL_DEBUG, "could not add "
819 "dynamic VLAN interface for vlan_id=%d",
824 iface = vlan->ifname;
825 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
826 hostapd_logger(hapd, sta->addr,
827 HOSTAPD_MODULE_IEEE80211,
828 HOSTAPD_LEVEL_DEBUG, "could not "
829 "configure encryption for dynamic VLAN "
830 "interface for vlan_id=%d",
834 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
835 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
836 "interface '%s'", iface);
837 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
838 if (sta->vlan_id > 0) {
839 vlan->dynamic_vlan++;
840 hostapd_logger(hapd, sta->addr,
841 HOSTAPD_MODULE_IEEE80211,
842 HOSTAPD_LEVEL_DEBUG, "updated existing "
843 "dynamic VLAN interface '%s'", iface);
847 * Update encryption configuration for statically generated
848 * VLAN interface. This is only used for static WEP
849 * configuration for the case where hostapd did not yet know
850 * which keys are to be used when the interface was added.
852 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
853 hostapd_logger(hapd, sta->addr,
854 HOSTAPD_MODULE_IEEE80211,
855 HOSTAPD_LEVEL_DEBUG, "could not "
856 "configure encryption for VLAN "
857 "interface for vlan_id=%d",
862 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
863 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
866 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
867 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
869 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
871 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
872 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
873 "entry to vlan_id=%d", sta->vlan_id);
876 #else /* CONFIG_NO_VLAN */
878 #endif /* CONFIG_NO_VLAN */
882 #ifdef CONFIG_IEEE80211W
884 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
887 struct os_reltime now, passed;
888 os_get_reltime(&now);
889 os_reltime_sub(&now, &sta->sa_query_start, &passed);
890 tu = (passed.sec * 1000000 + passed.usec) / 1024;
891 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
892 hostapd_logger(hapd, sta->addr,
893 HOSTAPD_MODULE_IEEE80211,
895 "association SA Query timed out");
896 sta->sa_query_timed_out = 1;
897 os_free(sta->sa_query_trans_id);
898 sta->sa_query_trans_id = NULL;
899 sta->sa_query_count = 0;
900 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
908 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
910 struct hostapd_data *hapd = eloop_ctx;
911 struct sta_info *sta = timeout_ctx;
912 unsigned int timeout, sec, usec;
915 if (sta->sa_query_count > 0 &&
916 ap_check_sa_query_timeout(hapd, sta))
919 nbuf = os_realloc_array(sta->sa_query_trans_id,
920 sta->sa_query_count + 1,
921 WLAN_SA_QUERY_TR_ID_LEN);
924 if (sta->sa_query_count == 0) {
925 /* Starting a new SA Query procedure */
926 os_get_reltime(&sta->sa_query_start);
928 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
929 sta->sa_query_trans_id = nbuf;
930 sta->sa_query_count++;
932 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
934 * We don't really care which ID is used here, so simply
935 * hardcode this if the mostly theoretical os_get_random()
942 timeout = hapd->conf->assoc_sa_query_retry_timeout;
943 sec = ((timeout / 1000) * 1024) / 1000;
944 usec = (timeout % 1000) * 1024;
945 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
947 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
949 "association SA Query attempt %d", sta->sa_query_count);
951 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
955 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
957 ap_sa_query_timer(hapd, sta);
961 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
963 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
964 os_free(sta->sa_query_trans_id);
965 sta->sa_query_trans_id = NULL;
966 sta->sa_query_count = 0;
969 #endif /* CONFIG_IEEE80211W */
972 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
975 const u8 *dev_addr = NULL;
980 #endif /* CONFIG_P2P */
982 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
986 sta->flags |= WLAN_STA_AUTHORIZED;
988 sta->flags &= ~WLAN_STA_AUTHORIZED;
991 if (hapd->p2p_group == NULL) {
992 if (sta->p2p_ie != NULL &&
993 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
996 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
999 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1000 MAC2STR(sta->addr), MAC2STR(dev_addr));
1002 #endif /* CONFIG_P2P */
1003 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1005 if (hapd->sta_authorized_cb)
1006 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1007 sta->addr, authorized, dev_addr);
1013 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1014 os_snprintf(ip_addr, sizeof(ip_addr),
1015 " ip_addr=%u.%u.%u.%u",
1016 ip_addr_buf[0], ip_addr_buf[1],
1017 ip_addr_buf[2], ip_addr_buf[3]);
1019 #endif /* CONFIG_P2P */
1021 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1024 if (hapd->msg_ctx_parent &&
1025 hapd->msg_ctx_parent != hapd->msg_ctx)
1026 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1027 AP_STA_CONNECTED "%s%s",
1030 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1032 if (hapd->msg_ctx_parent &&
1033 hapd->msg_ctx_parent != hapd->msg_ctx)
1034 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1035 AP_STA_DISCONNECTED "%s", buf);
1040 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1041 const u8 *addr, u16 reason)
1044 if (sta == NULL && addr)
1045 sta = ap_get_sta(hapd, addr);
1048 hostapd_drv_sta_deauth(hapd, addr, reason);
1052 ap_sta_set_authorized(hapd, sta, 0);
1053 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1054 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1055 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1056 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1057 "for " MACSTR " (%d seconds - "
1058 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1059 __func__, MAC2STR(sta->addr),
1060 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1061 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1062 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1063 ap_handle_timer, hapd, sta);
1064 sta->timeout_next = STA_REMOVE;
1066 sta->deauth_reason = reason;
1067 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1068 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1069 eloop_register_timeout(hapd->iface->drv_flags &
1070 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1071 ap_sta_deauth_cb_timeout, hapd, sta);
1075 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1077 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1078 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1081 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1082 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1083 ap_sta_deauth_cb_timeout(hapd, sta);
1087 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1089 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1090 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1093 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1094 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1095 ap_sta_disassoc_cb_timeout(hapd, sta);
1099 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1104 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1105 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1106 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1107 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1108 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1110 (flags & WLAN_STA_SHORT_PREAMBLE ?
1111 "[SHORT_PREAMBLE]" : ""),
1112 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1113 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1114 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1115 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1116 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1117 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1118 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1119 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1120 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1121 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1122 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1123 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1124 "[WNM_SLEEP_MODE]" : ""));
1125 if (os_snprintf_error(buflen, res))