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 #ifndef CONFIG_NO_VLAN
175 if (sta->vlan_id_bound) {
177 * Need to remove the STA entry before potentially removing the
180 if (hapd->iface->driver_ap_teardown &&
181 !(sta->flags & WLAN_STA_PREAUTH))
182 hostapd_drv_sta_remove(hapd, sta->addr);
183 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
185 #endif /* CONFIG_NO_VLAN */
187 ap_sta_hash_del(hapd, sta);
188 ap_sta_list_del(hapd, sta);
191 hapd->sta_aid[(sta->aid - 1) / 32] &=
192 ~BIT((sta->aid - 1) % 32);
195 if (sta->nonerp_set) {
197 hapd->iface->num_sta_non_erp--;
198 if (hapd->iface->num_sta_non_erp == 0)
202 if (sta->no_short_slot_time_set) {
203 sta->no_short_slot_time_set = 0;
204 hapd->iface->num_sta_no_short_slot_time--;
205 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
206 && hapd->iface->num_sta_no_short_slot_time == 0)
210 if (sta->no_short_preamble_set) {
211 sta->no_short_preamble_set = 0;
212 hapd->iface->num_sta_no_short_preamble--;
213 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
214 && hapd->iface->num_sta_no_short_preamble == 0)
218 if (sta->no_ht_gf_set) {
219 sta->no_ht_gf_set = 0;
220 hapd->iface->num_sta_ht_no_gf--;
223 if (sta->no_ht_set) {
225 hapd->iface->num_sta_no_ht--;
228 if (sta->ht_20mhz_set) {
229 sta->ht_20mhz_set = 0;
230 hapd->iface->num_sta_ht_20mhz--;
233 #ifdef CONFIG_IEEE80211N
234 ht40_intolerant_remove(hapd->iface, sta);
235 #endif /* CONFIG_IEEE80211N */
238 if (sta->no_p2p_set) {
240 hapd->num_sta_no_p2p--;
241 if (hapd->num_sta_no_p2p == 0)
242 hostapd_p2p_non_p2p_sta_disconnected(hapd);
244 #endif /* CONFIG_P2P */
246 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
247 if (hostapd_ht_operation_update(hapd->iface) > 0)
249 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
252 if (hapd->mesh_sta_free_cb)
253 hapd->mesh_sta_free_cb(sta);
254 #endif /* CONFIG_MESH */
257 ieee802_11_set_beacons(hapd->iface);
259 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
260 __func__, MAC2STR(sta->addr));
261 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
262 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
263 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
264 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
265 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
266 sae_clear_retransmit_timer(hapd, sta);
268 ieee802_1x_free_station(sta);
269 wpa_auth_sta_deinit(sta->wpa_sm);
270 rsn_preauth_free_station(hapd, sta);
271 #ifndef CONFIG_NO_RADIUS
273 radius_client_flush_auth(hapd->radius, sta->addr);
274 #endif /* CONFIG_NO_RADIUS */
276 os_free(sta->challenge);
278 #ifdef CONFIG_IEEE80211W
279 os_free(sta->sa_query_trans_id);
280 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
281 #endif /* CONFIG_IEEE80211W */
284 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
285 #endif /* CONFIG_P2P */
287 #ifdef CONFIG_INTERWORKING
288 if (sta->gas_dialog) {
290 for (i = 0; i < GAS_DIALOG_MAX; i++)
291 gas_serv_dialog_clear(&sta->gas_dialog[i]);
292 os_free(sta->gas_dialog);
294 #endif /* CONFIG_INTERWORKING */
296 wpabuf_free(sta->wps_ie);
297 wpabuf_free(sta->p2p_ie);
298 wpabuf_free(sta->hs20_ie);
300 wpabuf_free(sta->mb_ies);
301 #endif /* CONFIG_FST */
303 os_free(sta->ht_capabilities);
304 os_free(sta->vht_capabilities);
305 hostapd_free_psk_list(sta->psk);
306 os_free(sta->identity);
307 os_free(sta->radius_cui);
308 os_free(sta->remediation_url);
309 wpabuf_free(sta->hs20_deauth_req);
310 os_free(sta->hs20_session_info_url);
313 sae_clear_data(sta->sae);
315 #endif /* CONFIG_SAE */
321 void hostapd_free_stas(struct hostapd_data *hapd)
323 struct sta_info *sta, *prev;
325 sta = hapd->sta_list;
329 if (sta->flags & WLAN_STA_AUTH) {
330 mlme_deauthenticate_indication(
331 hapd, sta, WLAN_REASON_UNSPECIFIED);
334 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
335 MAC2STR(prev->addr));
336 ap_free_sta(hapd, prev);
342 * ap_handle_timer - Per STA timer handler
343 * @eloop_ctx: struct hostapd_data *
344 * @timeout_ctx: struct sta_info *
346 * This function is called to check station activity and to remove inactive
349 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
351 struct hostapd_data *hapd = eloop_ctx;
352 struct sta_info *sta = timeout_ctx;
353 unsigned long next_time = 0;
356 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
357 __func__, MAC2STR(sta->addr), sta->flags,
359 if (sta->timeout_next == STA_REMOVE) {
360 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
361 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
362 "local deauth request");
363 ap_free_sta(hapd, sta);
367 if ((sta->flags & WLAN_STA_ASSOC) &&
368 (sta->timeout_next == STA_NULLFUNC ||
369 sta->timeout_next == STA_DISASSOC)) {
372 * Add random value to timeout so that we don't end up bouncing
373 * all stations at the same time if we have lots of associated
374 * stations that are idle (but keep re-associating).
376 int fuzz = os_random() % 20;
377 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
378 if (inactive_sec == -1) {
379 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
380 "Check inactivity: Could not "
381 "get station info from kernel driver for "
382 MACSTR, MAC2STR(sta->addr));
384 * The driver may not support this functionality.
385 * Anyway, try again after the next inactivity timeout,
386 * but do not disconnect the station now.
388 next_time = hapd->conf->ap_max_inactivity + fuzz;
389 } else if (inactive_sec == -ENOENT) {
390 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
391 "Station " MACSTR " has lost its driver entry",
394 /* Avoid sending client probe on removed client */
395 sta->timeout_next = STA_DISASSOC;
397 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
398 /* station activity detected; reset timeout state */
399 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
400 "Station " MACSTR " has been active %is ago",
401 MAC2STR(sta->addr), inactive_sec);
402 sta->timeout_next = STA_NULLFUNC;
403 next_time = hapd->conf->ap_max_inactivity + fuzz -
406 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
407 "Station " MACSTR " has been "
408 "inactive too long: %d sec, max allowed: %d",
409 MAC2STR(sta->addr), inactive_sec,
410 hapd->conf->ap_max_inactivity);
412 if (hapd->conf->skip_inactivity_poll)
413 sta->timeout_next = STA_DISASSOC;
417 if ((sta->flags & WLAN_STA_ASSOC) &&
418 sta->timeout_next == STA_DISASSOC &&
419 !(sta->flags & WLAN_STA_PENDING_POLL) &&
420 !hapd->conf->skip_inactivity_poll) {
421 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
422 " has ACKed data poll", MAC2STR(sta->addr));
423 /* data nullfunc frame poll did not produce TX errors; assume
424 * station ACKed it */
425 sta->timeout_next = STA_NULLFUNC;
426 next_time = hapd->conf->ap_max_inactivity;
431 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
432 "for " MACSTR " (%lu seconds)",
433 __func__, MAC2STR(sta->addr), next_time);
434 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
439 if (sta->timeout_next == STA_NULLFUNC &&
440 (sta->flags & WLAN_STA_ASSOC)) {
441 wpa_printf(MSG_DEBUG, " Polling STA");
442 sta->flags |= WLAN_STA_PENDING_POLL;
443 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
444 sta->flags & WLAN_STA_WMM);
445 } else if (sta->timeout_next != STA_REMOVE) {
446 int deauth = sta->timeout_next == STA_DEAUTH;
448 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
449 "Timeout, sending %s info to STA " MACSTR,
450 deauth ? "deauthentication" : "disassociation",
454 hostapd_drv_sta_deauth(
456 WLAN_REASON_PREV_AUTH_NOT_VALID);
458 reason = (sta->timeout_next == STA_DISASSOC) ?
459 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
460 WLAN_REASON_PREV_AUTH_NOT_VALID;
462 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
466 switch (sta->timeout_next) {
468 sta->timeout_next = STA_DISASSOC;
469 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
470 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
471 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
472 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
476 case STA_DISASSOC_FROM_CLI:
477 ap_sta_set_authorized(hapd, sta, 0);
478 sta->flags &= ~WLAN_STA_ASSOC;
479 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
480 if (!sta->acct_terminate_cause)
481 sta->acct_terminate_cause =
482 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
483 accounting_sta_stop(hapd, sta);
484 ieee802_1x_free_station(sta);
485 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
486 HOSTAPD_LEVEL_INFO, "disassociated due to "
488 reason = (sta->timeout_next == STA_DISASSOC) ?
489 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
490 WLAN_REASON_PREV_AUTH_NOT_VALID;
491 sta->timeout_next = STA_DEAUTH;
492 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
493 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
494 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
495 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
497 mlme_disassociate_indication(hapd, sta, reason);
501 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
502 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
503 "inactivity (timer DEAUTH/REMOVE)");
504 if (!sta->acct_terminate_cause)
505 sta->acct_terminate_cause =
506 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
507 mlme_deauthenticate_indication(
509 WLAN_REASON_PREV_AUTH_NOT_VALID);
510 ap_free_sta(hapd, sta);
516 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
518 struct hostapd_data *hapd = eloop_ctx;
519 struct sta_info *sta = timeout_ctx;
521 if (!(sta->flags & WLAN_STA_AUTH)) {
522 if (sta->flags & WLAN_STA_GAS) {
523 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
524 "entry " MACSTR, MAC2STR(sta->addr));
525 ap_free_sta(hapd, sta);
530 hostapd_drv_sta_deauth(hapd, sta->addr,
531 WLAN_REASON_PREV_AUTH_NOT_VALID);
532 mlme_deauthenticate_indication(hapd, sta,
533 WLAN_REASON_PREV_AUTH_NOT_VALID);
534 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
535 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
537 sta->acct_terminate_cause =
538 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
539 ap_free_sta(hapd, sta);
543 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
546 if (eloop_replenish_timeout(session_timeout, 0,
547 ap_handle_session_timer, hapd, sta) == 1) {
548 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
549 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
550 "to %d seconds", session_timeout);
555 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
558 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
559 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
560 "seconds", session_timeout);
561 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
562 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
567 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
569 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
573 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
576 struct hostapd_data *hapd = eloop_ctx;
577 struct sta_info *sta = timeout_ctx;
579 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
581 if (sta->hs20_session_info_url == NULL)
584 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
585 sta->hs20_disassoc_timer);
586 #endif /* CONFIG_WNM */
590 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
591 struct sta_info *sta, int warning_time)
593 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
594 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
599 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
601 struct sta_info *sta;
603 sta = ap_get_sta(hapd, addr);
607 wpa_printf(MSG_DEBUG, " New STA");
608 if (hapd->num_sta >= hapd->conf->max_num_sta) {
609 /* FIX: might try to remove some old STAs first? */
610 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
611 hapd->num_sta, hapd->conf->max_num_sta);
615 sta = os_zalloc(sizeof(struct sta_info));
617 wpa_printf(MSG_ERROR, "malloc failed");
620 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
621 accounting_sta_get_id(hapd, sta);
623 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
624 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
625 "for " MACSTR " (%d seconds - ap_max_inactivity)",
626 __func__, MAC2STR(addr),
627 hapd->conf->ap_max_inactivity);
628 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
629 ap_handle_timer, hapd, sta);
632 /* initialize STA info data */
633 os_memcpy(sta->addr, addr, ETH_ALEN);
634 sta->next = hapd->sta_list;
635 hapd->sta_list = sta;
637 ap_sta_hash_add(hapd, sta);
638 ap_sta_remove_in_other_bss(hapd, sta);
639 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
640 dl_list_init(&sta->ip6addr);
646 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
648 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
651 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
652 ap_sta_ip6addr_del(hapd, sta);
654 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
656 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
657 sta->flags & WLAN_STA_ASSOC) {
658 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
659 " from kernel driver.", MAC2STR(sta->addr));
666 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
667 struct sta_info *sta)
669 struct hostapd_iface *iface = hapd->iface;
672 for (i = 0; i < iface->num_bss; i++) {
673 struct hostapd_data *bss = iface->bss[i];
674 struct sta_info *sta2;
675 /* bss should always be set during operation, but it may be
676 * NULL during reconfiguration. Assume the STA is not
677 * associated to another BSS in that case to avoid NULL pointer
679 if (bss == hapd || bss == NULL)
681 sta2 = ap_get_sta(bss, sta->addr);
685 ap_sta_disconnect(bss, sta2, sta2->addr,
686 WLAN_REASON_PREV_AUTH_NOT_VALID);
691 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
693 struct hostapd_data *hapd = eloop_ctx;
694 struct sta_info *sta = timeout_ctx;
696 ap_sta_remove(hapd, sta);
697 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
701 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
704 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
705 hapd->conf->iface, MAC2STR(sta->addr));
706 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
707 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
708 ap_sta_set_authorized(hapd, sta, 0);
709 sta->timeout_next = STA_DEAUTH;
710 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
711 "for " MACSTR " (%d seconds - "
712 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
713 __func__, MAC2STR(sta->addr),
714 AP_MAX_INACTIVITY_AFTER_DISASSOC);
715 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
716 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
717 ap_handle_timer, hapd, sta);
718 accounting_sta_stop(hapd, sta);
719 ieee802_1x_free_station(sta);
721 sta->disassoc_reason = reason;
722 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
723 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
724 eloop_register_timeout(hapd->iface->drv_flags &
725 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
726 ap_sta_disassoc_cb_timeout, hapd, sta);
730 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
732 struct hostapd_data *hapd = eloop_ctx;
733 struct sta_info *sta = timeout_ctx;
735 ap_sta_remove(hapd, sta);
736 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
740 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
743 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
744 hapd->conf->iface, MAC2STR(sta->addr));
745 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
746 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
747 ap_sta_set_authorized(hapd, sta, 0);
748 sta->timeout_next = STA_REMOVE;
749 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
750 "for " MACSTR " (%d seconds - "
751 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
752 __func__, MAC2STR(sta->addr),
753 AP_MAX_INACTIVITY_AFTER_DEAUTH);
754 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
755 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
756 ap_handle_timer, hapd, sta);
757 accounting_sta_stop(hapd, sta);
758 ieee802_1x_free_station(sta);
760 sta->deauth_reason = reason;
761 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
762 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
763 eloop_register_timeout(hapd->iface->drv_flags &
764 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
765 ap_sta_deauth_cb_timeout, hapd, sta);
770 int ap_sta_wps_cancel(struct hostapd_data *hapd,
771 struct sta_info *sta, void *ctx)
773 if (sta && (sta->flags & WLAN_STA_WPS)) {
774 ap_sta_deauthenticate(hapd, sta,
775 WLAN_REASON_PREV_AUTH_NOT_VALID);
776 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
777 __func__, MAC2STR(sta->addr));
783 #endif /* CONFIG_WPS */
786 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
788 #ifndef CONFIG_NO_VLAN
790 struct hostapd_vlan *vlan = NULL;
792 int old_vlanid = sta->vlan_id_bound;
794 iface = hapd->conf->iface;
795 if (hapd->conf->ssid.vlan[0])
796 iface = hapd->conf->ssid.vlan;
798 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
800 else if (sta->vlan_id > 0) {
801 struct hostapd_vlan *wildcard_vlan = NULL;
802 vlan = hapd->conf->vlan;
804 if (vlan->vlan_id == sta->vlan_id)
806 if (vlan->vlan_id == VLAN_ID_WILDCARD)
807 wildcard_vlan = vlan;
811 vlan = wildcard_vlan;
813 iface = vlan->ifname;
817 * Do not increment ref counters if the VLAN ID remains same, but do
818 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
819 * have been called before.
821 if (sta->vlan_id == old_vlanid)
824 if (sta->vlan_id > 0 && vlan == NULL) {
825 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
826 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
827 "binding station to (vlan_id=%d)",
831 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
832 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
834 hostapd_logger(hapd, sta->addr,
835 HOSTAPD_MODULE_IEEE80211,
836 HOSTAPD_LEVEL_DEBUG, "could not add "
837 "dynamic VLAN interface for vlan_id=%d",
843 iface = vlan->ifname;
844 if (vlan_setup_encryption_dyn(hapd, iface) != 0) {
845 hostapd_logger(hapd, sta->addr,
846 HOSTAPD_MODULE_IEEE80211,
847 HOSTAPD_LEVEL_DEBUG, "could not "
848 "configure encryption for dynamic VLAN "
849 "interface for vlan_id=%d",
853 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
854 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
855 "interface '%s'", iface);
856 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
857 if (vlan->dynamic_vlan > 0) {
858 vlan->dynamic_vlan++;
859 hostapd_logger(hapd, sta->addr,
860 HOSTAPD_MODULE_IEEE80211,
861 HOSTAPD_LEVEL_DEBUG, "updated existing "
862 "dynamic VLAN interface '%s'", iface);
866 * Update encryption configuration for statically generated
867 * VLAN interface. This is only used for static WEP
868 * configuration for the case where hostapd did not yet know
869 * which keys are to be used when the interface was added.
871 if (vlan_setup_encryption_dyn(hapd, iface) != 0) {
872 hostapd_logger(hapd, sta->addr,
873 HOSTAPD_MODULE_IEEE80211,
874 HOSTAPD_LEVEL_DEBUG, "could not "
875 "configure encryption for VLAN "
876 "interface for vlan_id=%d",
881 /* ref counters have been increased, so mark the station */
882 sta->vlan_id_bound = sta->vlan_id;
885 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
886 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
889 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
890 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
892 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
894 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
895 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
896 "entry to vlan_id=%d", sta->vlan_id);
899 /* During 1x reauth, if the vlan id changes, then remove the old id. */
900 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
901 vlan_remove_dynamic(hapd, old_vlanid);
905 #else /* CONFIG_NO_VLAN */
907 #endif /* CONFIG_NO_VLAN */
911 #ifdef CONFIG_IEEE80211W
913 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
916 struct os_reltime now, passed;
917 os_get_reltime(&now);
918 os_reltime_sub(&now, &sta->sa_query_start, &passed);
919 tu = (passed.sec * 1000000 + passed.usec) / 1024;
920 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
921 hostapd_logger(hapd, sta->addr,
922 HOSTAPD_MODULE_IEEE80211,
924 "association SA Query timed out");
925 sta->sa_query_timed_out = 1;
926 os_free(sta->sa_query_trans_id);
927 sta->sa_query_trans_id = NULL;
928 sta->sa_query_count = 0;
929 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
937 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
939 struct hostapd_data *hapd = eloop_ctx;
940 struct sta_info *sta = timeout_ctx;
941 unsigned int timeout, sec, usec;
944 if (sta->sa_query_count > 0 &&
945 ap_check_sa_query_timeout(hapd, sta))
948 nbuf = os_realloc_array(sta->sa_query_trans_id,
949 sta->sa_query_count + 1,
950 WLAN_SA_QUERY_TR_ID_LEN);
953 if (sta->sa_query_count == 0) {
954 /* Starting a new SA Query procedure */
955 os_get_reltime(&sta->sa_query_start);
957 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
958 sta->sa_query_trans_id = nbuf;
959 sta->sa_query_count++;
961 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
963 * We don't really care which ID is used here, so simply
964 * hardcode this if the mostly theoretical os_get_random()
971 timeout = hapd->conf->assoc_sa_query_retry_timeout;
972 sec = ((timeout / 1000) * 1024) / 1000;
973 usec = (timeout % 1000) * 1024;
974 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
976 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
978 "association SA Query attempt %d", sta->sa_query_count);
980 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
984 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
986 ap_sa_query_timer(hapd, sta);
990 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
992 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
993 os_free(sta->sa_query_trans_id);
994 sta->sa_query_trans_id = NULL;
995 sta->sa_query_count = 0;
998 #endif /* CONFIG_IEEE80211W */
1001 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1004 const u8 *dev_addr = NULL;
1009 #endif /* CONFIG_P2P */
1011 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1015 sta->flags |= WLAN_STA_AUTHORIZED;
1017 sta->flags &= ~WLAN_STA_AUTHORIZED;
1020 if (hapd->p2p_group == NULL) {
1021 if (sta->p2p_ie != NULL &&
1022 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1025 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1028 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1029 MAC2STR(sta->addr), MAC2STR(dev_addr));
1031 #endif /* CONFIG_P2P */
1032 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1034 if (hapd->sta_authorized_cb)
1035 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1036 sta->addr, authorized, dev_addr);
1042 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1043 os_snprintf(ip_addr, sizeof(ip_addr),
1044 " ip_addr=%u.%u.%u.%u",
1045 ip_addr_buf[0], ip_addr_buf[1],
1046 ip_addr_buf[2], ip_addr_buf[3]);
1048 #endif /* CONFIG_P2P */
1050 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1053 if (hapd->msg_ctx_parent &&
1054 hapd->msg_ctx_parent != hapd->msg_ctx)
1055 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1056 AP_STA_CONNECTED "%s%s",
1059 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1061 if (hapd->msg_ctx_parent &&
1062 hapd->msg_ctx_parent != hapd->msg_ctx)
1063 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1064 AP_STA_DISCONNECTED "%s", buf);
1069 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1070 const u8 *addr, u16 reason)
1073 if (sta == NULL && addr)
1074 sta = ap_get_sta(hapd, addr);
1077 hostapd_drv_sta_deauth(hapd, addr, reason);
1081 ap_sta_set_authorized(hapd, sta, 0);
1082 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1083 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1084 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1085 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1086 "for " MACSTR " (%d seconds - "
1087 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1088 __func__, MAC2STR(sta->addr),
1089 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1090 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1091 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1092 ap_handle_timer, hapd, sta);
1093 sta->timeout_next = STA_REMOVE;
1095 sta->deauth_reason = reason;
1096 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1097 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1098 eloop_register_timeout(hapd->iface->drv_flags &
1099 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1100 ap_sta_deauth_cb_timeout, hapd, sta);
1104 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1106 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1107 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1110 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1111 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1112 ap_sta_deauth_cb_timeout(hapd, sta);
1116 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1118 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1119 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1122 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1123 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1124 ap_sta_disassoc_cb_timeout(hapd, sta);
1128 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1133 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1134 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1135 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1136 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1137 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1139 (flags & WLAN_STA_SHORT_PREAMBLE ?
1140 "[SHORT_PREAMBLE]" : ""),
1141 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1142 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1143 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1144 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1145 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1146 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1147 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1148 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1149 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1150 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1151 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1152 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1153 "[WNM_SLEEP_MODE]" : ""));
1154 if (os_snprintf_error(buflen, res))