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 os_free(sta->ht_capabilities);
301 os_free(sta->vht_capabilities);
302 hostapd_free_psk_list(sta->psk);
303 os_free(sta->identity);
304 os_free(sta->radius_cui);
305 os_free(sta->remediation_url);
306 wpabuf_free(sta->hs20_deauth_req);
307 os_free(sta->hs20_session_info_url);
310 sae_clear_data(sta->sae);
312 #endif /* CONFIG_SAE */
318 void hostapd_free_stas(struct hostapd_data *hapd)
320 struct sta_info *sta, *prev;
322 sta = hapd->sta_list;
326 if (sta->flags & WLAN_STA_AUTH) {
327 mlme_deauthenticate_indication(
328 hapd, sta, WLAN_REASON_UNSPECIFIED);
331 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
332 MAC2STR(prev->addr));
333 ap_free_sta(hapd, prev);
339 * ap_handle_timer - Per STA timer handler
340 * @eloop_ctx: struct hostapd_data *
341 * @timeout_ctx: struct sta_info *
343 * This function is called to check station activity and to remove inactive
346 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
348 struct hostapd_data *hapd = eloop_ctx;
349 struct sta_info *sta = timeout_ctx;
350 unsigned long next_time = 0;
353 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
354 __func__, MAC2STR(sta->addr), sta->flags,
356 if (sta->timeout_next == STA_REMOVE) {
357 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
358 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
359 "local deauth request");
360 ap_free_sta(hapd, sta);
364 if ((sta->flags & WLAN_STA_ASSOC) &&
365 (sta->timeout_next == STA_NULLFUNC ||
366 sta->timeout_next == STA_DISASSOC)) {
369 * Add random value to timeout so that we don't end up bouncing
370 * all stations at the same time if we have lots of associated
371 * stations that are idle (but keep re-associating).
373 int fuzz = os_random() % 20;
374 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
375 if (inactive_sec == -1) {
376 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
377 "Check inactivity: Could not "
378 "get station info from kernel driver for "
379 MACSTR, MAC2STR(sta->addr));
381 * The driver may not support this functionality.
382 * Anyway, try again after the next inactivity timeout,
383 * but do not disconnect the station now.
385 next_time = hapd->conf->ap_max_inactivity + fuzz;
386 } else if (inactive_sec == -ENOENT) {
387 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
388 "Station " MACSTR " has lost its driver entry",
391 /* Avoid sending client probe on removed client */
392 sta->timeout_next = STA_DISASSOC;
394 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
395 /* station activity detected; reset timeout state */
396 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
397 "Station " MACSTR " has been active %is ago",
398 MAC2STR(sta->addr), inactive_sec);
399 sta->timeout_next = STA_NULLFUNC;
400 next_time = hapd->conf->ap_max_inactivity + fuzz -
403 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
404 "Station " MACSTR " has been "
405 "inactive too long: %d sec, max allowed: %d",
406 MAC2STR(sta->addr), inactive_sec,
407 hapd->conf->ap_max_inactivity);
409 if (hapd->conf->skip_inactivity_poll)
410 sta->timeout_next = STA_DISASSOC;
414 if ((sta->flags & WLAN_STA_ASSOC) &&
415 sta->timeout_next == STA_DISASSOC &&
416 !(sta->flags & WLAN_STA_PENDING_POLL) &&
417 !hapd->conf->skip_inactivity_poll) {
418 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
419 " has ACKed data poll", MAC2STR(sta->addr));
420 /* data nullfunc frame poll did not produce TX errors; assume
421 * station ACKed it */
422 sta->timeout_next = STA_NULLFUNC;
423 next_time = hapd->conf->ap_max_inactivity;
428 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
429 "for " MACSTR " (%lu seconds)",
430 __func__, MAC2STR(sta->addr), next_time);
431 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
436 if (sta->timeout_next == STA_NULLFUNC &&
437 (sta->flags & WLAN_STA_ASSOC)) {
438 wpa_printf(MSG_DEBUG, " Polling STA");
439 sta->flags |= WLAN_STA_PENDING_POLL;
440 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
441 sta->flags & WLAN_STA_WMM);
442 } else if (sta->timeout_next != STA_REMOVE) {
443 int deauth = sta->timeout_next == STA_DEAUTH;
445 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
446 "Timeout, sending %s info to STA " MACSTR,
447 deauth ? "deauthentication" : "disassociation",
451 hostapd_drv_sta_deauth(
453 WLAN_REASON_PREV_AUTH_NOT_VALID);
455 reason = (sta->timeout_next == STA_DISASSOC) ?
456 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
457 WLAN_REASON_PREV_AUTH_NOT_VALID;
459 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
463 switch (sta->timeout_next) {
465 sta->timeout_next = STA_DISASSOC;
466 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
467 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
468 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
469 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
473 case STA_DISASSOC_FROM_CLI:
474 ap_sta_set_authorized(hapd, sta, 0);
475 sta->flags &= ~WLAN_STA_ASSOC;
476 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
477 if (!sta->acct_terminate_cause)
478 sta->acct_terminate_cause =
479 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
480 accounting_sta_stop(hapd, sta);
481 ieee802_1x_free_station(sta);
482 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
483 HOSTAPD_LEVEL_INFO, "disassociated due to "
485 reason = (sta->timeout_next == STA_DISASSOC) ?
486 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
487 WLAN_REASON_PREV_AUTH_NOT_VALID;
488 sta->timeout_next = STA_DEAUTH;
489 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
490 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
491 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
492 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
494 mlme_disassociate_indication(hapd, sta, reason);
498 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
499 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
500 "inactivity (timer DEAUTH/REMOVE)");
501 if (!sta->acct_terminate_cause)
502 sta->acct_terminate_cause =
503 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
504 mlme_deauthenticate_indication(
506 WLAN_REASON_PREV_AUTH_NOT_VALID);
507 ap_free_sta(hapd, sta);
513 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
515 struct hostapd_data *hapd = eloop_ctx;
516 struct sta_info *sta = timeout_ctx;
518 if (!(sta->flags & WLAN_STA_AUTH)) {
519 if (sta->flags & WLAN_STA_GAS) {
520 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
521 "entry " MACSTR, MAC2STR(sta->addr));
522 ap_free_sta(hapd, sta);
527 hostapd_drv_sta_deauth(hapd, sta->addr,
528 WLAN_REASON_PREV_AUTH_NOT_VALID);
529 mlme_deauthenticate_indication(hapd, sta,
530 WLAN_REASON_PREV_AUTH_NOT_VALID);
531 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
532 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
534 sta->acct_terminate_cause =
535 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
536 ap_free_sta(hapd, sta);
540 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
543 if (eloop_replenish_timeout(session_timeout, 0,
544 ap_handle_session_timer, hapd, sta) == 1) {
545 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
546 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
547 "to %d seconds", session_timeout);
552 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
555 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
556 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
557 "seconds", session_timeout);
558 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
559 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
564 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
566 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
570 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
573 struct hostapd_data *hapd = eloop_ctx;
574 struct sta_info *sta = timeout_ctx;
576 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
578 if (sta->hs20_session_info_url == NULL)
581 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
582 sta->hs20_disassoc_timer);
583 #endif /* CONFIG_WNM */
587 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
588 struct sta_info *sta, int warning_time)
590 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
591 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
596 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
598 struct sta_info *sta;
600 sta = ap_get_sta(hapd, addr);
604 wpa_printf(MSG_DEBUG, " New STA");
605 if (hapd->num_sta >= hapd->conf->max_num_sta) {
606 /* FIX: might try to remove some old STAs first? */
607 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
608 hapd->num_sta, hapd->conf->max_num_sta);
612 sta = os_zalloc(sizeof(struct sta_info));
614 wpa_printf(MSG_ERROR, "malloc failed");
617 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
618 accounting_sta_get_id(hapd, sta);
620 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
621 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
622 "for " MACSTR " (%d seconds - ap_max_inactivity)",
623 __func__, MAC2STR(addr),
624 hapd->conf->ap_max_inactivity);
625 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
626 ap_handle_timer, hapd, sta);
629 /* initialize STA info data */
630 os_memcpy(sta->addr, addr, ETH_ALEN);
631 sta->next = hapd->sta_list;
632 hapd->sta_list = sta;
634 ap_sta_hash_add(hapd, sta);
635 sta->ssid = &hapd->conf->ssid;
636 ap_sta_remove_in_other_bss(hapd, sta);
637 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
638 dl_list_init(&sta->ip6addr);
644 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
646 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
649 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
650 ap_sta_ip6addr_del(hapd, sta);
652 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
654 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
655 sta->flags & WLAN_STA_ASSOC) {
656 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
657 " from kernel driver.", MAC2STR(sta->addr));
664 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
665 struct sta_info *sta)
667 struct hostapd_iface *iface = hapd->iface;
670 for (i = 0; i < iface->num_bss; i++) {
671 struct hostapd_data *bss = iface->bss[i];
672 struct sta_info *sta2;
673 /* bss should always be set during operation, but it may be
674 * NULL during reconfiguration. Assume the STA is not
675 * associated to another BSS in that case to avoid NULL pointer
677 if (bss == hapd || bss == NULL)
679 sta2 = ap_get_sta(bss, sta->addr);
683 ap_sta_disconnect(bss, sta2, sta2->addr,
684 WLAN_REASON_PREV_AUTH_NOT_VALID);
689 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
691 struct hostapd_data *hapd = eloop_ctx;
692 struct sta_info *sta = timeout_ctx;
694 ap_sta_remove(hapd, sta);
695 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
699 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
702 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
703 hapd->conf->iface, MAC2STR(sta->addr));
704 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
705 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
706 ap_sta_set_authorized(hapd, sta, 0);
707 sta->timeout_next = STA_DEAUTH;
708 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
709 "for " MACSTR " (%d seconds - "
710 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
711 __func__, MAC2STR(sta->addr),
712 AP_MAX_INACTIVITY_AFTER_DISASSOC);
713 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
714 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
715 ap_handle_timer, hapd, sta);
716 accounting_sta_stop(hapd, sta);
717 ieee802_1x_free_station(sta);
719 sta->disassoc_reason = reason;
720 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
721 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
722 eloop_register_timeout(hapd->iface->drv_flags &
723 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
724 ap_sta_disassoc_cb_timeout, hapd, sta);
728 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
730 struct hostapd_data *hapd = eloop_ctx;
731 struct sta_info *sta = timeout_ctx;
733 ap_sta_remove(hapd, sta);
734 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
738 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
741 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
742 hapd->conf->iface, MAC2STR(sta->addr));
743 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
744 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
745 ap_sta_set_authorized(hapd, sta, 0);
746 sta->timeout_next = STA_REMOVE;
747 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
748 "for " MACSTR " (%d seconds - "
749 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
750 __func__, MAC2STR(sta->addr),
751 AP_MAX_INACTIVITY_AFTER_DEAUTH);
752 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
753 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
754 ap_handle_timer, hapd, sta);
755 accounting_sta_stop(hapd, sta);
756 ieee802_1x_free_station(sta);
758 sta->deauth_reason = reason;
759 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
760 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
761 eloop_register_timeout(hapd->iface->drv_flags &
762 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
763 ap_sta_deauth_cb_timeout, hapd, sta);
768 int ap_sta_wps_cancel(struct hostapd_data *hapd,
769 struct sta_info *sta, void *ctx)
771 if (sta && (sta->flags & WLAN_STA_WPS)) {
772 ap_sta_deauthenticate(hapd, sta,
773 WLAN_REASON_PREV_AUTH_NOT_VALID);
774 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
775 __func__, MAC2STR(sta->addr));
781 #endif /* CONFIG_WPS */
784 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
786 #ifndef CONFIG_NO_VLAN
788 struct hostapd_vlan *vlan = NULL;
790 int old_vlanid = sta->vlan_id_bound;
792 iface = hapd->conf->iface;
793 if (sta->ssid->vlan[0])
794 iface = sta->ssid->vlan;
796 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
798 else if (sta->vlan_id > 0) {
799 struct hostapd_vlan *wildcard_vlan = NULL;
800 vlan = hapd->conf->vlan;
802 if (vlan->vlan_id == sta->vlan_id)
804 if (vlan->vlan_id == VLAN_ID_WILDCARD)
805 wildcard_vlan = vlan;
809 vlan = wildcard_vlan;
811 iface = vlan->ifname;
815 * Do not increment ref counters if the VLAN ID remains same, but do
816 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
817 * have been called before.
819 if (sta->vlan_id == old_vlanid)
822 if (sta->vlan_id > 0 && vlan == NULL) {
823 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
824 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
825 "binding station to (vlan_id=%d)",
829 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
830 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
832 hostapd_logger(hapd, sta->addr,
833 HOSTAPD_MODULE_IEEE80211,
834 HOSTAPD_LEVEL_DEBUG, "could not add "
835 "dynamic VLAN interface for vlan_id=%d",
841 iface = vlan->ifname;
842 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
843 hostapd_logger(hapd, sta->addr,
844 HOSTAPD_MODULE_IEEE80211,
845 HOSTAPD_LEVEL_DEBUG, "could not "
846 "configure encryption for dynamic VLAN "
847 "interface for vlan_id=%d",
851 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
852 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
853 "interface '%s'", iface);
854 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
855 if (vlan->dynamic_vlan > 0) {
856 vlan->dynamic_vlan++;
857 hostapd_logger(hapd, sta->addr,
858 HOSTAPD_MODULE_IEEE80211,
859 HOSTAPD_LEVEL_DEBUG, "updated existing "
860 "dynamic VLAN interface '%s'", iface);
864 * Update encryption configuration for statically generated
865 * VLAN interface. This is only used for static WEP
866 * configuration for the case where hostapd did not yet know
867 * which keys are to be used when the interface was added.
869 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
870 hostapd_logger(hapd, sta->addr,
871 HOSTAPD_MODULE_IEEE80211,
872 HOSTAPD_LEVEL_DEBUG, "could not "
873 "configure encryption for VLAN "
874 "interface for vlan_id=%d",
879 /* ref counters have been increased, so mark the station */
880 sta->vlan_id_bound = sta->vlan_id;
883 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
884 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
887 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
888 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
890 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
892 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
893 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
894 "entry to vlan_id=%d", sta->vlan_id);
897 /* During 1x reauth, if the vlan id changes, then remove the old id. */
898 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
899 vlan_remove_dynamic(hapd, old_vlanid);
903 #else /* CONFIG_NO_VLAN */
905 #endif /* CONFIG_NO_VLAN */
909 #ifdef CONFIG_IEEE80211W
911 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
914 struct os_reltime now, passed;
915 os_get_reltime(&now);
916 os_reltime_sub(&now, &sta->sa_query_start, &passed);
917 tu = (passed.sec * 1000000 + passed.usec) / 1024;
918 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
919 hostapd_logger(hapd, sta->addr,
920 HOSTAPD_MODULE_IEEE80211,
922 "association SA Query timed out");
923 sta->sa_query_timed_out = 1;
924 os_free(sta->sa_query_trans_id);
925 sta->sa_query_trans_id = NULL;
926 sta->sa_query_count = 0;
927 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
935 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
937 struct hostapd_data *hapd = eloop_ctx;
938 struct sta_info *sta = timeout_ctx;
939 unsigned int timeout, sec, usec;
942 if (sta->sa_query_count > 0 &&
943 ap_check_sa_query_timeout(hapd, sta))
946 nbuf = os_realloc_array(sta->sa_query_trans_id,
947 sta->sa_query_count + 1,
948 WLAN_SA_QUERY_TR_ID_LEN);
951 if (sta->sa_query_count == 0) {
952 /* Starting a new SA Query procedure */
953 os_get_reltime(&sta->sa_query_start);
955 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
956 sta->sa_query_trans_id = nbuf;
957 sta->sa_query_count++;
959 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
961 * We don't really care which ID is used here, so simply
962 * hardcode this if the mostly theoretical os_get_random()
969 timeout = hapd->conf->assoc_sa_query_retry_timeout;
970 sec = ((timeout / 1000) * 1024) / 1000;
971 usec = (timeout % 1000) * 1024;
972 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
974 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
976 "association SA Query attempt %d", sta->sa_query_count);
978 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
982 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
984 ap_sa_query_timer(hapd, sta);
988 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
990 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
991 os_free(sta->sa_query_trans_id);
992 sta->sa_query_trans_id = NULL;
993 sta->sa_query_count = 0;
996 #endif /* CONFIG_IEEE80211W */
999 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1002 const u8 *dev_addr = NULL;
1007 #endif /* CONFIG_P2P */
1009 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1013 sta->flags |= WLAN_STA_AUTHORIZED;
1015 sta->flags &= ~WLAN_STA_AUTHORIZED;
1018 if (hapd->p2p_group == NULL) {
1019 if (sta->p2p_ie != NULL &&
1020 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1023 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1026 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1027 MAC2STR(sta->addr), MAC2STR(dev_addr));
1029 #endif /* CONFIG_P2P */
1030 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1032 if (hapd->sta_authorized_cb)
1033 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1034 sta->addr, authorized, dev_addr);
1040 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1041 os_snprintf(ip_addr, sizeof(ip_addr),
1042 " ip_addr=%u.%u.%u.%u",
1043 ip_addr_buf[0], ip_addr_buf[1],
1044 ip_addr_buf[2], ip_addr_buf[3]);
1046 #endif /* CONFIG_P2P */
1048 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1051 if (hapd->msg_ctx_parent &&
1052 hapd->msg_ctx_parent != hapd->msg_ctx)
1053 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1054 AP_STA_CONNECTED "%s%s",
1057 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1059 if (hapd->msg_ctx_parent &&
1060 hapd->msg_ctx_parent != hapd->msg_ctx)
1061 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1062 AP_STA_DISCONNECTED "%s", buf);
1067 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1068 const u8 *addr, u16 reason)
1071 if (sta == NULL && addr)
1072 sta = ap_get_sta(hapd, addr);
1075 hostapd_drv_sta_deauth(hapd, addr, reason);
1079 ap_sta_set_authorized(hapd, sta, 0);
1080 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1081 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1082 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1083 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1084 "for " MACSTR " (%d seconds - "
1085 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1086 __func__, MAC2STR(sta->addr),
1087 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1088 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1089 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1090 ap_handle_timer, hapd, sta);
1091 sta->timeout_next = STA_REMOVE;
1093 sta->deauth_reason = reason;
1094 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1095 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1096 eloop_register_timeout(hapd->iface->drv_flags &
1097 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1098 ap_sta_deauth_cb_timeout, hapd, sta);
1102 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1104 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1105 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1108 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1109 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1110 ap_sta_deauth_cb_timeout(hapd, sta);
1114 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1116 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1117 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1120 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1121 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1122 ap_sta_disassoc_cb_timeout(hapd, sta);
1126 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1131 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1132 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1133 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1134 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1135 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1137 (flags & WLAN_STA_SHORT_PREAMBLE ?
1138 "[SHORT_PREAMBLE]" : ""),
1139 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1140 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1141 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1142 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1143 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1144 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1145 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1146 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1147 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1148 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1149 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1150 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1151 "[WNM_SLEEP_MODE]" : ""));
1152 if (os_snprintf_error(buflen, res))