2 * hostapd / Station table
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/sae.h"
16 #include "radius/radius.h"
17 #include "radius/radius_client.h"
21 #include "accounting.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "ieee802_11_auth.h"
26 #include "preauth_auth.h"
27 #include "ap_config.h"
30 #include "vlan_init.h"
31 #include "p2p_hostapd.h"
32 #include "ap_drv_ops.h"
35 #include "ndisc_snoop.h"
38 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
39 struct sta_info *sta);
40 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
41 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
42 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
44 #ifdef CONFIG_IEEE80211W
45 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
46 #endif /* CONFIG_IEEE80211W */
47 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
49 int ap_for_each_sta(struct hostapd_data *hapd,
50 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
56 for (sta = hapd->sta_list; sta; sta = sta->next) {
57 if (cb(hapd, sta, ctx))
65 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
69 s = hapd->sta_hash[STA_HASH(sta)];
70 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
77 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
81 for (sta = hapd->sta_list; sta; sta = sta->next) {
82 const u8 *p2p_dev_addr;
84 if (sta->p2p_ie == NULL)
87 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
88 if (p2p_dev_addr == NULL)
91 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
97 #endif /* CONFIG_P2P */
100 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
102 struct sta_info *tmp;
104 if (hapd->sta_list == sta) {
105 hapd->sta_list = sta->next;
109 tmp = hapd->sta_list;
110 while (tmp != NULL && tmp->next != sta)
113 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
114 "list.", MAC2STR(sta->addr));
116 tmp->next = sta->next;
120 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
122 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
123 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
127 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
131 s = hapd->sta_hash[STA_HASH(sta->addr)];
132 if (s == NULL) return;
133 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
134 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
138 while (s->hnext != NULL &&
139 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
141 if (s->hnext != NULL)
142 s->hnext = s->hnext->hnext;
144 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
145 " from hash table", MAC2STR(sta->addr));
149 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
151 sta_ip6addr_del(hapd, sta);
155 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
159 accounting_sta_stop(hapd, sta);
162 ap_sta_set_authorized(hapd, sta, 0);
164 if (sta->flags & WLAN_STA_WDS)
165 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
168 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
169 ap_sta_ip6addr_del(hapd, sta);
171 if (!hapd->iface->driver_ap_teardown &&
172 !(sta->flags & WLAN_STA_PREAUTH))
173 hostapd_drv_sta_remove(hapd, sta->addr);
175 #ifndef CONFIG_NO_VLAN
176 if (sta->vlan_id_bound) {
178 * Need to remove the STA entry before potentially removing the
181 if (hapd->iface->driver_ap_teardown &&
182 !(sta->flags & WLAN_STA_PREAUTH))
183 hostapd_drv_sta_remove(hapd, sta->addr);
184 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
186 #endif /* CONFIG_NO_VLAN */
188 ap_sta_hash_del(hapd, sta);
189 ap_sta_list_del(hapd, sta);
192 hapd->sta_aid[(sta->aid - 1) / 32] &=
193 ~BIT((sta->aid - 1) % 32);
196 if (sta->nonerp_set) {
198 hapd->iface->num_sta_non_erp--;
199 if (hapd->iface->num_sta_non_erp == 0)
203 if (sta->no_short_slot_time_set) {
204 sta->no_short_slot_time_set = 0;
205 hapd->iface->num_sta_no_short_slot_time--;
206 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
207 && hapd->iface->num_sta_no_short_slot_time == 0)
211 if (sta->no_short_preamble_set) {
212 sta->no_short_preamble_set = 0;
213 hapd->iface->num_sta_no_short_preamble--;
214 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
215 && hapd->iface->num_sta_no_short_preamble == 0)
219 if (sta->no_ht_gf_set) {
220 sta->no_ht_gf_set = 0;
221 hapd->iface->num_sta_ht_no_gf--;
224 if (sta->no_ht_set) {
226 hapd->iface->num_sta_no_ht--;
229 if (sta->ht_20mhz_set) {
230 sta->ht_20mhz_set = 0;
231 hapd->iface->num_sta_ht_20mhz--;
234 #ifdef CONFIG_IEEE80211N
235 ht40_intolerant_remove(hapd->iface, sta);
236 #endif /* CONFIG_IEEE80211N */
239 if (sta->no_p2p_set) {
241 hapd->num_sta_no_p2p--;
242 if (hapd->num_sta_no_p2p == 0)
243 hostapd_p2p_non_p2p_sta_disconnected(hapd);
245 #endif /* CONFIG_P2P */
247 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
248 if (hostapd_ht_operation_update(hapd->iface) > 0)
250 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
253 if (hapd->mesh_sta_free_cb)
254 hapd->mesh_sta_free_cb(sta);
255 #endif /* CONFIG_MESH */
258 ieee802_11_set_beacons(hapd->iface);
260 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
261 __func__, MAC2STR(sta->addr));
262 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
263 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
264 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
265 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
266 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
267 sae_clear_retransmit_timer(hapd, sta);
269 ieee802_1x_free_station(sta);
270 wpa_auth_sta_deinit(sta->wpa_sm);
271 rsn_preauth_free_station(hapd, sta);
272 #ifndef CONFIG_NO_RADIUS
274 radius_client_flush_auth(hapd->radius, sta->addr);
275 #endif /* CONFIG_NO_RADIUS */
277 os_free(sta->challenge);
279 #ifdef CONFIG_IEEE80211W
280 os_free(sta->sa_query_trans_id);
281 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
282 #endif /* CONFIG_IEEE80211W */
285 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
286 #endif /* CONFIG_P2P */
288 #ifdef CONFIG_INTERWORKING
289 if (sta->gas_dialog) {
291 for (i = 0; i < GAS_DIALOG_MAX; i++)
292 gas_serv_dialog_clear(&sta->gas_dialog[i]);
293 os_free(sta->gas_dialog);
295 #endif /* CONFIG_INTERWORKING */
297 wpabuf_free(sta->wps_ie);
298 wpabuf_free(sta->p2p_ie);
299 wpabuf_free(sta->hs20_ie);
301 wpabuf_free(sta->mb_ies);
302 #endif /* CONFIG_FST */
304 os_free(sta->ht_capabilities);
305 os_free(sta->vht_capabilities);
306 hostapd_free_psk_list(sta->psk);
307 os_free(sta->identity);
308 os_free(sta->radius_cui);
309 os_free(sta->remediation_url);
310 wpabuf_free(sta->hs20_deauth_req);
311 os_free(sta->hs20_session_info_url);
314 sae_clear_data(sta->sae);
316 #endif /* CONFIG_SAE */
322 void hostapd_free_stas(struct hostapd_data *hapd)
324 struct sta_info *sta, *prev;
326 sta = hapd->sta_list;
330 if (sta->flags & WLAN_STA_AUTH) {
331 mlme_deauthenticate_indication(
332 hapd, sta, WLAN_REASON_UNSPECIFIED);
335 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
336 MAC2STR(prev->addr));
337 ap_free_sta(hapd, prev);
343 * ap_handle_timer - Per STA timer handler
344 * @eloop_ctx: struct hostapd_data *
345 * @timeout_ctx: struct sta_info *
347 * This function is called to check station activity and to remove inactive
350 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
352 struct hostapd_data *hapd = eloop_ctx;
353 struct sta_info *sta = timeout_ctx;
354 unsigned long next_time = 0;
357 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
358 __func__, MAC2STR(sta->addr), sta->flags,
360 if (sta->timeout_next == STA_REMOVE) {
361 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
362 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
363 "local deauth request");
364 ap_free_sta(hapd, sta);
368 if ((sta->flags & WLAN_STA_ASSOC) &&
369 (sta->timeout_next == STA_NULLFUNC ||
370 sta->timeout_next == STA_DISASSOC)) {
373 * Add random value to timeout so that we don't end up bouncing
374 * all stations at the same time if we have lots of associated
375 * stations that are idle (but keep re-associating).
377 int fuzz = os_random() % 20;
378 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
379 if (inactive_sec == -1) {
380 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
381 "Check inactivity: Could not "
382 "get station info from kernel driver for "
383 MACSTR, MAC2STR(sta->addr));
385 * The driver may not support this functionality.
386 * Anyway, try again after the next inactivity timeout,
387 * but do not disconnect the station now.
389 next_time = hapd->conf->ap_max_inactivity + fuzz;
390 } else if (inactive_sec == -ENOENT) {
391 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
392 "Station " MACSTR " has lost its driver entry",
395 /* Avoid sending client probe on removed client */
396 sta->timeout_next = STA_DISASSOC;
398 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
399 /* station activity detected; reset timeout state */
400 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
401 "Station " MACSTR " has been active %is ago",
402 MAC2STR(sta->addr), inactive_sec);
403 sta->timeout_next = STA_NULLFUNC;
404 next_time = hapd->conf->ap_max_inactivity + fuzz -
407 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
408 "Station " MACSTR " has been "
409 "inactive too long: %d sec, max allowed: %d",
410 MAC2STR(sta->addr), inactive_sec,
411 hapd->conf->ap_max_inactivity);
413 if (hapd->conf->skip_inactivity_poll)
414 sta->timeout_next = STA_DISASSOC;
418 if ((sta->flags & WLAN_STA_ASSOC) &&
419 sta->timeout_next == STA_DISASSOC &&
420 !(sta->flags & WLAN_STA_PENDING_POLL) &&
421 !hapd->conf->skip_inactivity_poll) {
422 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
423 " has ACKed data poll", MAC2STR(sta->addr));
424 /* data nullfunc frame poll did not produce TX errors; assume
425 * station ACKed it */
426 sta->timeout_next = STA_NULLFUNC;
427 next_time = hapd->conf->ap_max_inactivity;
432 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
433 "for " MACSTR " (%lu seconds)",
434 __func__, MAC2STR(sta->addr), next_time);
435 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
440 if (sta->timeout_next == STA_NULLFUNC &&
441 (sta->flags & WLAN_STA_ASSOC)) {
442 wpa_printf(MSG_DEBUG, " Polling STA");
443 sta->flags |= WLAN_STA_PENDING_POLL;
444 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
445 sta->flags & WLAN_STA_WMM);
446 } else if (sta->timeout_next != STA_REMOVE) {
447 int deauth = sta->timeout_next == STA_DEAUTH;
449 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
450 "Timeout, sending %s info to STA " MACSTR,
451 deauth ? "deauthentication" : "disassociation",
455 hostapd_drv_sta_deauth(
457 WLAN_REASON_PREV_AUTH_NOT_VALID);
459 reason = (sta->timeout_next == STA_DISASSOC) ?
460 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
461 WLAN_REASON_PREV_AUTH_NOT_VALID;
463 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
467 switch (sta->timeout_next) {
469 sta->timeout_next = STA_DISASSOC;
470 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
471 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
472 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
473 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
477 case STA_DISASSOC_FROM_CLI:
478 ap_sta_set_authorized(hapd, sta, 0);
479 sta->flags &= ~WLAN_STA_ASSOC;
480 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
481 if (!sta->acct_terminate_cause)
482 sta->acct_terminate_cause =
483 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
484 accounting_sta_stop(hapd, sta);
485 ieee802_1x_free_station(sta);
486 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
487 HOSTAPD_LEVEL_INFO, "disassociated due to "
489 reason = (sta->timeout_next == STA_DISASSOC) ?
490 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
491 WLAN_REASON_PREV_AUTH_NOT_VALID;
492 sta->timeout_next = STA_DEAUTH;
493 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
494 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
495 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
496 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
498 mlme_disassociate_indication(hapd, sta, reason);
502 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
503 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
504 "inactivity (timer DEAUTH/REMOVE)");
505 if (!sta->acct_terminate_cause)
506 sta->acct_terminate_cause =
507 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
508 mlme_deauthenticate_indication(
510 WLAN_REASON_PREV_AUTH_NOT_VALID);
511 ap_free_sta(hapd, sta);
517 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
519 struct hostapd_data *hapd = eloop_ctx;
520 struct sta_info *sta = timeout_ctx;
522 if (!(sta->flags & WLAN_STA_AUTH)) {
523 if (sta->flags & WLAN_STA_GAS) {
524 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
525 "entry " MACSTR, MAC2STR(sta->addr));
526 ap_free_sta(hapd, sta);
531 hostapd_drv_sta_deauth(hapd, sta->addr,
532 WLAN_REASON_PREV_AUTH_NOT_VALID);
533 mlme_deauthenticate_indication(hapd, sta,
534 WLAN_REASON_PREV_AUTH_NOT_VALID);
535 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
536 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
538 sta->acct_terminate_cause =
539 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
540 ap_free_sta(hapd, sta);
544 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
547 if (eloop_replenish_timeout(session_timeout, 0,
548 ap_handle_session_timer, hapd, sta) == 1) {
549 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
550 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
551 "to %d seconds", session_timeout);
556 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
559 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
560 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
561 "seconds", session_timeout);
562 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
563 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
568 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
570 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
574 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
577 struct hostapd_data *hapd = eloop_ctx;
578 struct sta_info *sta = timeout_ctx;
580 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
582 if (sta->hs20_session_info_url == NULL)
585 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
586 sta->hs20_disassoc_timer);
587 #endif /* CONFIG_WNM */
591 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
592 struct sta_info *sta, int warning_time)
594 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
595 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
600 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
602 struct sta_info *sta;
604 sta = ap_get_sta(hapd, addr);
608 wpa_printf(MSG_DEBUG, " New STA");
609 if (hapd->num_sta >= hapd->conf->max_num_sta) {
610 /* FIX: might try to remove some old STAs first? */
611 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
612 hapd->num_sta, hapd->conf->max_num_sta);
616 sta = os_zalloc(sizeof(struct sta_info));
618 wpa_printf(MSG_ERROR, "malloc failed");
621 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
622 accounting_sta_get_id(hapd, sta);
624 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
625 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
626 "for " MACSTR " (%d seconds - ap_max_inactivity)",
627 __func__, MAC2STR(addr),
628 hapd->conf->ap_max_inactivity);
629 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
630 ap_handle_timer, hapd, sta);
633 /* initialize STA info data */
634 os_memcpy(sta->addr, addr, ETH_ALEN);
635 sta->next = hapd->sta_list;
636 hapd->sta_list = sta;
638 ap_sta_hash_add(hapd, sta);
639 ap_sta_remove_in_other_bss(hapd, sta);
640 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
641 dl_list_init(&sta->ip6addr);
647 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
649 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
652 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
653 ap_sta_ip6addr_del(hapd, sta);
655 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
657 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
658 sta->flags & WLAN_STA_ASSOC) {
659 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
660 " from kernel driver.", MAC2STR(sta->addr));
667 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
668 struct sta_info *sta)
670 struct hostapd_iface *iface = hapd->iface;
673 for (i = 0; i < iface->num_bss; i++) {
674 struct hostapd_data *bss = iface->bss[i];
675 struct sta_info *sta2;
676 /* bss should always be set during operation, but it may be
677 * NULL during reconfiguration. Assume the STA is not
678 * associated to another BSS in that case to avoid NULL pointer
680 if (bss == hapd || bss == NULL)
682 sta2 = ap_get_sta(bss, sta->addr);
686 ap_sta_disconnect(bss, sta2, sta2->addr,
687 WLAN_REASON_PREV_AUTH_NOT_VALID);
692 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
694 struct hostapd_data *hapd = eloop_ctx;
695 struct sta_info *sta = timeout_ctx;
697 ap_sta_remove(hapd, sta);
698 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
702 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
705 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
706 hapd->conf->iface, MAC2STR(sta->addr));
707 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
708 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
709 ap_sta_set_authorized(hapd, sta, 0);
710 sta->timeout_next = STA_DEAUTH;
711 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
712 "for " MACSTR " (%d seconds - "
713 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
714 __func__, MAC2STR(sta->addr),
715 AP_MAX_INACTIVITY_AFTER_DISASSOC);
716 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
717 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
718 ap_handle_timer, hapd, sta);
719 accounting_sta_stop(hapd, sta);
720 ieee802_1x_free_station(sta);
722 sta->disassoc_reason = reason;
723 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
724 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
725 eloop_register_timeout(hapd->iface->drv_flags &
726 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
727 ap_sta_disassoc_cb_timeout, hapd, sta);
731 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
733 struct hostapd_data *hapd = eloop_ctx;
734 struct sta_info *sta = timeout_ctx;
736 ap_sta_remove(hapd, sta);
737 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
741 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
744 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
745 hapd->conf->iface, MAC2STR(sta->addr));
746 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
747 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
748 ap_sta_set_authorized(hapd, sta, 0);
749 sta->timeout_next = STA_REMOVE;
750 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
751 "for " MACSTR " (%d seconds - "
752 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
753 __func__, MAC2STR(sta->addr),
754 AP_MAX_INACTIVITY_AFTER_DEAUTH);
755 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
756 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
757 ap_handle_timer, hapd, sta);
758 accounting_sta_stop(hapd, sta);
759 ieee802_1x_free_station(sta);
761 sta->deauth_reason = reason;
762 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
763 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
764 eloop_register_timeout(hapd->iface->drv_flags &
765 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
766 ap_sta_deauth_cb_timeout, hapd, sta);
771 int ap_sta_wps_cancel(struct hostapd_data *hapd,
772 struct sta_info *sta, void *ctx)
774 if (sta && (sta->flags & WLAN_STA_WPS)) {
775 ap_sta_deauthenticate(hapd, sta,
776 WLAN_REASON_PREV_AUTH_NOT_VALID);
777 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
778 __func__, MAC2STR(sta->addr));
784 #endif /* CONFIG_WPS */
787 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
789 #ifndef CONFIG_NO_VLAN
791 struct hostapd_vlan *vlan = NULL;
793 int old_vlanid = sta->vlan_id_bound;
795 iface = hapd->conf->iface;
796 if (hapd->conf->ssid.vlan[0])
797 iface = hapd->conf->ssid.vlan;
799 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
801 else if (sta->vlan_id > 0) {
802 struct hostapd_vlan *wildcard_vlan = NULL;
803 vlan = hapd->conf->vlan;
805 if (vlan->vlan_id == sta->vlan_id)
807 if (vlan->vlan_id == VLAN_ID_WILDCARD)
808 wildcard_vlan = vlan;
812 vlan = wildcard_vlan;
814 iface = vlan->ifname;
818 * Do not increment ref counters if the VLAN ID remains same, but do
819 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
820 * have been called before.
822 if (sta->vlan_id == old_vlanid)
825 if (sta->vlan_id > 0 && vlan == NULL) {
826 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
827 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
828 "binding station to (vlan_id=%d)",
832 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
833 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
835 hostapd_logger(hapd, sta->addr,
836 HOSTAPD_MODULE_IEEE80211,
837 HOSTAPD_LEVEL_DEBUG, "could not add "
838 "dynamic VLAN interface for vlan_id=%d",
844 iface = vlan->ifname;
845 if (vlan_setup_encryption_dyn(hapd, iface) != 0) {
846 hostapd_logger(hapd, sta->addr,
847 HOSTAPD_MODULE_IEEE80211,
848 HOSTAPD_LEVEL_DEBUG, "could not "
849 "configure encryption for dynamic VLAN "
850 "interface for vlan_id=%d",
854 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
855 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
856 "interface '%s'", iface);
857 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
858 if (vlan->dynamic_vlan > 0) {
859 vlan->dynamic_vlan++;
860 hostapd_logger(hapd, sta->addr,
861 HOSTAPD_MODULE_IEEE80211,
862 HOSTAPD_LEVEL_DEBUG, "updated existing "
863 "dynamic VLAN interface '%s'", iface);
867 * Update encryption configuration for statically generated
868 * VLAN interface. This is only used for static WEP
869 * configuration for the case where hostapd did not yet know
870 * which keys are to be used when the interface was added.
872 if (vlan_setup_encryption_dyn(hapd, iface) != 0) {
873 hostapd_logger(hapd, sta->addr,
874 HOSTAPD_MODULE_IEEE80211,
875 HOSTAPD_LEVEL_DEBUG, "could not "
876 "configure encryption for VLAN "
877 "interface for vlan_id=%d",
882 /* ref counters have been increased, so mark the station */
883 sta->vlan_id_bound = sta->vlan_id;
886 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
887 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
890 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
891 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
893 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
895 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
896 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
897 "entry to vlan_id=%d", sta->vlan_id);
900 /* During 1x reauth, if the vlan id changes, then remove the old id. */
901 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
902 vlan_remove_dynamic(hapd, old_vlanid);
906 #else /* CONFIG_NO_VLAN */
908 #endif /* CONFIG_NO_VLAN */
912 #ifdef CONFIG_IEEE80211W
914 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
917 struct os_reltime now, passed;
918 os_get_reltime(&now);
919 os_reltime_sub(&now, &sta->sa_query_start, &passed);
920 tu = (passed.sec * 1000000 + passed.usec) / 1024;
921 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
922 hostapd_logger(hapd, sta->addr,
923 HOSTAPD_MODULE_IEEE80211,
925 "association SA Query timed out");
926 sta->sa_query_timed_out = 1;
927 os_free(sta->sa_query_trans_id);
928 sta->sa_query_trans_id = NULL;
929 sta->sa_query_count = 0;
930 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
938 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
940 struct hostapd_data *hapd = eloop_ctx;
941 struct sta_info *sta = timeout_ctx;
942 unsigned int timeout, sec, usec;
945 if (sta->sa_query_count > 0 &&
946 ap_check_sa_query_timeout(hapd, sta))
949 nbuf = os_realloc_array(sta->sa_query_trans_id,
950 sta->sa_query_count + 1,
951 WLAN_SA_QUERY_TR_ID_LEN);
954 if (sta->sa_query_count == 0) {
955 /* Starting a new SA Query procedure */
956 os_get_reltime(&sta->sa_query_start);
958 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
959 sta->sa_query_trans_id = nbuf;
960 sta->sa_query_count++;
962 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
964 * We don't really care which ID is used here, so simply
965 * hardcode this if the mostly theoretical os_get_random()
972 timeout = hapd->conf->assoc_sa_query_retry_timeout;
973 sec = ((timeout / 1000) * 1024) / 1000;
974 usec = (timeout % 1000) * 1024;
975 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
977 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
979 "association SA Query attempt %d", sta->sa_query_count);
981 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
985 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
987 ap_sa_query_timer(hapd, sta);
991 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
993 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
994 os_free(sta->sa_query_trans_id);
995 sta->sa_query_trans_id = NULL;
996 sta->sa_query_count = 0;
999 #endif /* CONFIG_IEEE80211W */
1002 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1005 const u8 *dev_addr = NULL;
1010 #endif /* CONFIG_P2P */
1012 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1016 sta->flags |= WLAN_STA_AUTHORIZED;
1018 sta->flags &= ~WLAN_STA_AUTHORIZED;
1021 if (hapd->p2p_group == NULL) {
1022 if (sta->p2p_ie != NULL &&
1023 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1026 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1029 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1030 MAC2STR(sta->addr), MAC2STR(dev_addr));
1032 #endif /* CONFIG_P2P */
1033 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1035 if (hapd->sta_authorized_cb)
1036 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1037 sta->addr, authorized, dev_addr);
1043 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1044 os_snprintf(ip_addr, sizeof(ip_addr),
1045 " ip_addr=%u.%u.%u.%u",
1046 ip_addr_buf[0], ip_addr_buf[1],
1047 ip_addr_buf[2], ip_addr_buf[3]);
1049 #endif /* CONFIG_P2P */
1051 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1054 if (hapd->msg_ctx_parent &&
1055 hapd->msg_ctx_parent != hapd->msg_ctx)
1056 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1057 AP_STA_CONNECTED "%s%s",
1060 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1062 if (hapd->msg_ctx_parent &&
1063 hapd->msg_ctx_parent != hapd->msg_ctx)
1064 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1065 AP_STA_DISCONNECTED "%s", buf);
1069 if (hapd->iface->fst) {
1071 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1073 fst_notify_peer_disconnected(hapd->iface->fst,
1076 #endif /* CONFIG_FST */
1080 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1081 const u8 *addr, u16 reason)
1084 if (sta == NULL && addr)
1085 sta = ap_get_sta(hapd, addr);
1088 hostapd_drv_sta_deauth(hapd, addr, reason);
1092 ap_sta_set_authorized(hapd, sta, 0);
1093 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1094 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1095 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1096 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1097 "for " MACSTR " (%d seconds - "
1098 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1099 __func__, MAC2STR(sta->addr),
1100 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1101 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1102 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1103 ap_handle_timer, hapd, sta);
1104 sta->timeout_next = STA_REMOVE;
1106 sta->deauth_reason = reason;
1107 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1108 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1109 eloop_register_timeout(hapd->iface->drv_flags &
1110 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1111 ap_sta_deauth_cb_timeout, hapd, sta);
1115 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1117 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1118 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1121 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1122 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1123 ap_sta_deauth_cb_timeout(hapd, sta);
1127 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1129 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1130 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1133 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1134 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1135 ap_sta_disassoc_cb_timeout(hapd, sta);
1139 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1144 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1145 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1146 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1147 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1148 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1150 (flags & WLAN_STA_SHORT_PREAMBLE ?
1151 "[SHORT_PREAMBLE]" : ""),
1152 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1153 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1154 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1155 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1156 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1157 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1158 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1159 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1160 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1161 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1162 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1163 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1164 "[WNM_SLEEP_MODE]" : ""));
1165 if (os_snprintf_error(buflen, res))