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"
36 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
37 struct sta_info *sta);
38 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
39 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
40 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
41 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
42 #ifdef CONFIG_IEEE80211W
43 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
44 #endif /* CONFIG_IEEE80211W */
45 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
47 int ap_for_each_sta(struct hostapd_data *hapd,
48 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
54 for (sta = hapd->sta_list; sta; sta = sta->next) {
55 if (cb(hapd, sta, ctx))
63 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
67 s = hapd->sta_hash[STA_HASH(sta)];
68 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
75 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
79 for (sta = hapd->sta_list; sta; sta = sta->next) {
80 const u8 *p2p_dev_addr;
82 if (sta->p2p_ie == NULL)
85 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
86 if (p2p_dev_addr == NULL)
89 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
95 #endif /* CONFIG_P2P */
98 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
100 struct sta_info *tmp;
102 if (hapd->sta_list == sta) {
103 hapd->sta_list = sta->next;
107 tmp = hapd->sta_list;
108 while (tmp != NULL && tmp->next != sta)
111 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
112 "list.", MAC2STR(sta->addr));
114 tmp->next = sta->next;
118 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
120 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
121 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
125 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
129 s = hapd->sta_hash[STA_HASH(sta->addr)];
130 if (s == NULL) return;
131 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
132 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
136 while (s->hnext != NULL &&
137 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
139 if (s->hnext != NULL)
140 s->hnext = s->hnext->hnext;
142 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
143 " from hash table", MAC2STR(sta->addr));
147 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
151 accounting_sta_stop(hapd, sta);
154 ap_sta_set_authorized(hapd, sta, 0);
156 if (sta->flags & WLAN_STA_WDS)
157 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
160 hostapd_drv_br_delete_ip_neigh(hapd, sta->ipaddr);
162 if (!hapd->iface->driver_ap_teardown &&
163 !(sta->flags & WLAN_STA_PREAUTH))
164 hostapd_drv_sta_remove(hapd, sta->addr);
166 ap_sta_hash_del(hapd, sta);
167 ap_sta_list_del(hapd, sta);
170 hapd->sta_aid[(sta->aid - 1) / 32] &=
171 ~BIT((sta->aid - 1) % 32);
174 if (sta->nonerp_set) {
176 hapd->iface->num_sta_non_erp--;
177 if (hapd->iface->num_sta_non_erp == 0)
181 if (sta->no_short_slot_time_set) {
182 sta->no_short_slot_time_set = 0;
183 hapd->iface->num_sta_no_short_slot_time--;
184 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
185 && hapd->iface->num_sta_no_short_slot_time == 0)
189 if (sta->no_short_preamble_set) {
190 sta->no_short_preamble_set = 0;
191 hapd->iface->num_sta_no_short_preamble--;
192 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
193 && hapd->iface->num_sta_no_short_preamble == 0)
197 if (sta->no_ht_gf_set) {
198 sta->no_ht_gf_set = 0;
199 hapd->iface->num_sta_ht_no_gf--;
202 if (sta->no_ht_set) {
204 hapd->iface->num_sta_no_ht--;
207 if (sta->ht_20mhz_set) {
208 sta->ht_20mhz_set = 0;
209 hapd->iface->num_sta_ht_20mhz--;
212 #ifdef CONFIG_IEEE80211N
213 ht40_intolerant_remove(hapd->iface, sta);
214 #endif /* CONFIG_IEEE80211N */
217 if (sta->no_p2p_set) {
219 hapd->num_sta_no_p2p--;
220 if (hapd->num_sta_no_p2p == 0)
221 hostapd_p2p_non_p2p_sta_disconnected(hapd);
223 #endif /* CONFIG_P2P */
225 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
226 if (hostapd_ht_operation_update(hapd->iface) > 0)
228 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
231 if (hapd->mesh_sta_free_cb)
232 hapd->mesh_sta_free_cb(sta);
233 #endif /* CONFIG_MESH */
236 ieee802_11_set_beacons(hapd->iface);
238 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
239 __func__, MAC2STR(sta->addr));
240 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
241 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
242 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
243 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
244 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
246 ieee802_1x_free_station(sta);
247 wpa_auth_sta_deinit(sta->wpa_sm);
248 rsn_preauth_free_station(hapd, sta);
249 #ifndef CONFIG_NO_RADIUS
251 radius_client_flush_auth(hapd->radius, sta->addr);
252 #endif /* CONFIG_NO_RADIUS */
254 os_free(sta->challenge);
256 #ifdef CONFIG_IEEE80211W
257 os_free(sta->sa_query_trans_id);
258 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
259 #endif /* CONFIG_IEEE80211W */
262 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
263 #endif /* CONFIG_P2P */
265 #ifdef CONFIG_INTERWORKING
266 if (sta->gas_dialog) {
268 for (i = 0; i < GAS_DIALOG_MAX; i++)
269 gas_serv_dialog_clear(&sta->gas_dialog[i]);
270 os_free(sta->gas_dialog);
272 #endif /* CONFIG_INTERWORKING */
274 wpabuf_free(sta->wps_ie);
275 wpabuf_free(sta->p2p_ie);
276 wpabuf_free(sta->hs20_ie);
278 os_free(sta->ht_capabilities);
279 os_free(sta->vht_capabilities);
280 hostapd_free_psk_list(sta->psk);
281 os_free(sta->identity);
282 os_free(sta->radius_cui);
283 os_free(sta->remediation_url);
284 wpabuf_free(sta->hs20_deauth_req);
285 os_free(sta->hs20_session_info_url);
288 sae_clear_data(sta->sae);
290 #endif /* CONFIG_SAE */
296 void hostapd_free_stas(struct hostapd_data *hapd)
298 struct sta_info *sta, *prev;
300 sta = hapd->sta_list;
304 if (sta->flags & WLAN_STA_AUTH) {
305 mlme_deauthenticate_indication(
306 hapd, sta, WLAN_REASON_UNSPECIFIED);
309 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
310 MAC2STR(prev->addr));
311 ap_free_sta(hapd, prev);
317 * ap_handle_timer - Per STA timer handler
318 * @eloop_ctx: struct hostapd_data *
319 * @timeout_ctx: struct sta_info *
321 * This function is called to check station activity and to remove inactive
324 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
326 struct hostapd_data *hapd = eloop_ctx;
327 struct sta_info *sta = timeout_ctx;
328 unsigned long next_time = 0;
331 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
332 __func__, MAC2STR(sta->addr), sta->flags,
334 if (sta->timeout_next == STA_REMOVE) {
335 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
336 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
337 "local deauth request");
338 ap_free_sta(hapd, sta);
342 if ((sta->flags & WLAN_STA_ASSOC) &&
343 (sta->timeout_next == STA_NULLFUNC ||
344 sta->timeout_next == STA_DISASSOC)) {
347 * Add random value to timeout so that we don't end up bouncing
348 * all stations at the same time if we have lots of associated
349 * stations that are idle (but keep re-associating).
351 int fuzz = os_random() % 20;
352 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
353 if (inactive_sec == -1) {
354 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
355 "Check inactivity: Could not "
356 "get station info from kernel driver for "
357 MACSTR, MAC2STR(sta->addr));
359 * The driver may not support this functionality.
360 * Anyway, try again after the next inactivity timeout,
361 * but do not disconnect the station now.
363 next_time = hapd->conf->ap_max_inactivity + fuzz;
364 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
365 sta->flags & WLAN_STA_ASSOC) {
366 /* station activity detected; reset timeout state */
367 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
368 "Station " MACSTR " has been active %is ago",
369 MAC2STR(sta->addr), inactive_sec);
370 sta->timeout_next = STA_NULLFUNC;
371 next_time = hapd->conf->ap_max_inactivity + fuzz -
374 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
375 "Station " MACSTR " has been "
376 "inactive too long: %d sec, max allowed: %d",
377 MAC2STR(sta->addr), inactive_sec,
378 hapd->conf->ap_max_inactivity);
380 if (hapd->conf->skip_inactivity_poll)
381 sta->timeout_next = STA_DISASSOC;
385 if ((sta->flags & WLAN_STA_ASSOC) &&
386 sta->timeout_next == STA_DISASSOC &&
387 !(sta->flags & WLAN_STA_PENDING_POLL) &&
388 !hapd->conf->skip_inactivity_poll) {
389 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
390 " has ACKed data poll", MAC2STR(sta->addr));
391 /* data nullfunc frame poll did not produce TX errors; assume
392 * station ACKed it */
393 sta->timeout_next = STA_NULLFUNC;
394 next_time = hapd->conf->ap_max_inactivity;
398 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
399 "for " MACSTR " (%lu seconds)",
400 __func__, MAC2STR(sta->addr), next_time);
401 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
406 if (sta->timeout_next == STA_NULLFUNC &&
407 (sta->flags & WLAN_STA_ASSOC)) {
408 wpa_printf(MSG_DEBUG, " Polling STA");
409 sta->flags |= WLAN_STA_PENDING_POLL;
410 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
411 sta->flags & WLAN_STA_WMM);
412 } else if (sta->timeout_next != STA_REMOVE) {
413 int deauth = sta->timeout_next == STA_DEAUTH;
415 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
416 "Timeout, sending %s info to STA " MACSTR,
417 deauth ? "deauthentication" : "disassociation",
421 hostapd_drv_sta_deauth(
423 WLAN_REASON_PREV_AUTH_NOT_VALID);
425 reason = (sta->timeout_next == STA_DISASSOC) ?
426 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
427 WLAN_REASON_PREV_AUTH_NOT_VALID;
429 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
433 switch (sta->timeout_next) {
435 sta->timeout_next = STA_DISASSOC;
436 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
437 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
438 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
439 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
443 case STA_DISASSOC_FROM_CLI:
444 ap_sta_set_authorized(hapd, sta, 0);
445 sta->flags &= ~WLAN_STA_ASSOC;
446 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
447 if (!sta->acct_terminate_cause)
448 sta->acct_terminate_cause =
449 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
450 accounting_sta_stop(hapd, sta);
451 ieee802_1x_free_station(sta);
452 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
453 HOSTAPD_LEVEL_INFO, "disassociated due to "
455 reason = (sta->timeout_next == STA_DISASSOC) ?
456 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
457 WLAN_REASON_PREV_AUTH_NOT_VALID;
458 sta->timeout_next = STA_DEAUTH;
459 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
460 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
461 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
462 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
464 mlme_disassociate_indication(hapd, sta, reason);
468 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
469 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
470 "inactivity (timer DEAUTH/REMOVE)");
471 if (!sta->acct_terminate_cause)
472 sta->acct_terminate_cause =
473 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
474 mlme_deauthenticate_indication(
476 WLAN_REASON_PREV_AUTH_NOT_VALID);
477 ap_free_sta(hapd, sta);
483 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
485 struct hostapd_data *hapd = eloop_ctx;
486 struct sta_info *sta = timeout_ctx;
488 if (!(sta->flags & WLAN_STA_AUTH)) {
489 if (sta->flags & WLAN_STA_GAS) {
490 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
491 "entry " MACSTR, MAC2STR(sta->addr));
492 ap_free_sta(hapd, sta);
497 hostapd_drv_sta_deauth(hapd, sta->addr,
498 WLAN_REASON_PREV_AUTH_NOT_VALID);
499 mlme_deauthenticate_indication(hapd, sta,
500 WLAN_REASON_PREV_AUTH_NOT_VALID);
501 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
502 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
504 sta->acct_terminate_cause =
505 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
506 ap_free_sta(hapd, sta);
510 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
513 if (eloop_replenish_timeout(session_timeout, 0,
514 ap_handle_session_timer, hapd, sta) == 1) {
515 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
516 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
517 "to %d seconds", session_timeout);
522 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
525 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
526 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
527 "seconds", session_timeout);
528 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
529 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
534 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
536 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
540 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
543 struct hostapd_data *hapd = eloop_ctx;
544 struct sta_info *sta = timeout_ctx;
546 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
548 if (sta->hs20_session_info_url == NULL)
551 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
552 sta->hs20_disassoc_timer);
553 #endif /* CONFIG_WNM */
557 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
558 struct sta_info *sta, int warning_time)
560 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
561 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
566 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
568 struct sta_info *sta;
570 sta = ap_get_sta(hapd, addr);
574 wpa_printf(MSG_DEBUG, " New STA");
575 if (hapd->num_sta >= hapd->conf->max_num_sta) {
576 /* FIX: might try to remove some old STAs first? */
577 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
578 hapd->num_sta, hapd->conf->max_num_sta);
582 sta = os_zalloc(sizeof(struct sta_info));
584 wpa_printf(MSG_ERROR, "malloc failed");
587 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
588 accounting_sta_get_id(hapd, sta);
590 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
591 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
592 "for " MACSTR " (%d seconds - ap_max_inactivity)",
593 __func__, MAC2STR(addr),
594 hapd->conf->ap_max_inactivity);
595 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
596 ap_handle_timer, hapd, sta);
599 /* initialize STA info data */
600 os_memcpy(sta->addr, addr, ETH_ALEN);
601 sta->next = hapd->sta_list;
602 hapd->sta_list = sta;
604 ap_sta_hash_add(hapd, sta);
605 sta->ssid = &hapd->conf->ssid;
606 ap_sta_remove_in_other_bss(hapd, sta);
607 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
613 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
615 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
618 hostapd_drv_br_delete_ip_neigh(hapd, sta->ipaddr);
620 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
622 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
623 sta->flags & WLAN_STA_ASSOC) {
624 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
625 " from kernel driver.", MAC2STR(sta->addr));
632 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
633 struct sta_info *sta)
635 struct hostapd_iface *iface = hapd->iface;
638 for (i = 0; i < iface->num_bss; i++) {
639 struct hostapd_data *bss = iface->bss[i];
640 struct sta_info *sta2;
641 /* bss should always be set during operation, but it may be
642 * NULL during reconfiguration. Assume the STA is not
643 * associated to another BSS in that case to avoid NULL pointer
645 if (bss == hapd || bss == NULL)
647 sta2 = ap_get_sta(bss, sta->addr);
651 ap_sta_disconnect(bss, sta2, sta2->addr,
652 WLAN_REASON_PREV_AUTH_NOT_VALID);
657 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
659 struct hostapd_data *hapd = eloop_ctx;
660 struct sta_info *sta = timeout_ctx;
662 ap_sta_remove(hapd, sta);
663 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
667 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
670 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
671 hapd->conf->iface, MAC2STR(sta->addr));
672 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
673 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
674 ap_sta_set_authorized(hapd, sta, 0);
675 sta->timeout_next = STA_DEAUTH;
676 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
677 "for " MACSTR " (%d seconds - "
678 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
679 __func__, MAC2STR(sta->addr),
680 AP_MAX_INACTIVITY_AFTER_DISASSOC);
681 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
682 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
683 ap_handle_timer, hapd, sta);
684 accounting_sta_stop(hapd, sta);
685 ieee802_1x_free_station(sta);
687 sta->disassoc_reason = reason;
688 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
689 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
690 eloop_register_timeout(hapd->iface->drv_flags &
691 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
692 ap_sta_disassoc_cb_timeout, hapd, sta);
696 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
698 struct hostapd_data *hapd = eloop_ctx;
699 struct sta_info *sta = timeout_ctx;
701 ap_sta_remove(hapd, sta);
702 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
706 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
709 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
710 hapd->conf->iface, MAC2STR(sta->addr));
711 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
712 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
713 ap_sta_set_authorized(hapd, sta, 0);
714 sta->timeout_next = STA_REMOVE;
715 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
716 "for " MACSTR " (%d seconds - "
717 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
718 __func__, MAC2STR(sta->addr),
719 AP_MAX_INACTIVITY_AFTER_DEAUTH);
720 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
721 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
722 ap_handle_timer, hapd, sta);
723 accounting_sta_stop(hapd, sta);
724 ieee802_1x_free_station(sta);
726 sta->deauth_reason = reason;
727 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
728 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
729 eloop_register_timeout(hapd->iface->drv_flags &
730 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
731 ap_sta_deauth_cb_timeout, hapd, sta);
736 int ap_sta_wps_cancel(struct hostapd_data *hapd,
737 struct sta_info *sta, void *ctx)
739 if (sta && (sta->flags & WLAN_STA_WPS)) {
740 ap_sta_deauthenticate(hapd, sta,
741 WLAN_REASON_PREV_AUTH_NOT_VALID);
742 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
743 __func__, MAC2STR(sta->addr));
749 #endif /* CONFIG_WPS */
752 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
755 #ifndef CONFIG_NO_VLAN
757 struct hostapd_vlan *vlan = NULL;
761 * Do not proceed furthur if the vlan id remains same. We do not want
762 * duplicate dynamic vlan entries.
764 if (sta->vlan_id == old_vlanid)
768 * During 1x reauth, if the vlan id changes, then remove the old id and
769 * proceed furthur to add the new one.
772 vlan_remove_dynamic(hapd, old_vlanid);
774 iface = hapd->conf->iface;
775 if (sta->ssid->vlan[0])
776 iface = sta->ssid->vlan;
778 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
780 else if (sta->vlan_id > 0) {
781 struct hostapd_vlan *wildcard_vlan = NULL;
782 vlan = hapd->conf->vlan;
784 if (vlan->vlan_id == sta->vlan_id)
786 if (vlan->vlan_id == VLAN_ID_WILDCARD)
787 wildcard_vlan = vlan;
791 vlan = wildcard_vlan;
793 iface = vlan->ifname;
796 if (sta->vlan_id > 0 && vlan == NULL) {
797 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
798 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
799 "binding station to (vlan_id=%d)",
802 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
803 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
805 hostapd_logger(hapd, sta->addr,
806 HOSTAPD_MODULE_IEEE80211,
807 HOSTAPD_LEVEL_DEBUG, "could not add "
808 "dynamic VLAN interface for vlan_id=%d",
813 iface = vlan->ifname;
814 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
815 hostapd_logger(hapd, sta->addr,
816 HOSTAPD_MODULE_IEEE80211,
817 HOSTAPD_LEVEL_DEBUG, "could not "
818 "configure encryption for dynamic VLAN "
819 "interface for vlan_id=%d",
823 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
824 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
825 "interface '%s'", iface);
826 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
827 if (sta->vlan_id > 0) {
828 vlan->dynamic_vlan++;
829 hostapd_logger(hapd, sta->addr,
830 HOSTAPD_MODULE_IEEE80211,
831 HOSTAPD_LEVEL_DEBUG, "updated existing "
832 "dynamic VLAN interface '%s'", iface);
836 * Update encryption configuration for statically generated
837 * VLAN interface. This is only used for static WEP
838 * configuration for the case where hostapd did not yet know
839 * which keys are to be used when the interface was added.
841 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
842 hostapd_logger(hapd, sta->addr,
843 HOSTAPD_MODULE_IEEE80211,
844 HOSTAPD_LEVEL_DEBUG, "could not "
845 "configure encryption for VLAN "
846 "interface for vlan_id=%d",
851 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
852 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
855 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
856 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
858 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
860 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
861 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
862 "entry to vlan_id=%d", sta->vlan_id);
865 #else /* CONFIG_NO_VLAN */
867 #endif /* CONFIG_NO_VLAN */
871 #ifdef CONFIG_IEEE80211W
873 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
876 struct os_reltime now, passed;
877 os_get_reltime(&now);
878 os_reltime_sub(&now, &sta->sa_query_start, &passed);
879 tu = (passed.sec * 1000000 + passed.usec) / 1024;
880 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
881 hostapd_logger(hapd, sta->addr,
882 HOSTAPD_MODULE_IEEE80211,
884 "association SA Query timed out");
885 sta->sa_query_timed_out = 1;
886 os_free(sta->sa_query_trans_id);
887 sta->sa_query_trans_id = NULL;
888 sta->sa_query_count = 0;
889 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
897 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
899 struct hostapd_data *hapd = eloop_ctx;
900 struct sta_info *sta = timeout_ctx;
901 unsigned int timeout, sec, usec;
904 if (sta->sa_query_count > 0 &&
905 ap_check_sa_query_timeout(hapd, sta))
908 nbuf = os_realloc_array(sta->sa_query_trans_id,
909 sta->sa_query_count + 1,
910 WLAN_SA_QUERY_TR_ID_LEN);
913 if (sta->sa_query_count == 0) {
914 /* Starting a new SA Query procedure */
915 os_get_reltime(&sta->sa_query_start);
917 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
918 sta->sa_query_trans_id = nbuf;
919 sta->sa_query_count++;
921 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
923 * We don't really care which ID is used here, so simply
924 * hardcode this if the mostly theoretical os_get_random()
931 timeout = hapd->conf->assoc_sa_query_retry_timeout;
932 sec = ((timeout / 1000) * 1024) / 1000;
933 usec = (timeout % 1000) * 1024;
934 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
936 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
938 "association SA Query attempt %d", sta->sa_query_count);
940 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
944 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
946 ap_sa_query_timer(hapd, sta);
950 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
952 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
953 os_free(sta->sa_query_trans_id);
954 sta->sa_query_trans_id = NULL;
955 sta->sa_query_count = 0;
958 #endif /* CONFIG_IEEE80211W */
961 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
964 const u8 *dev_addr = NULL;
969 #endif /* CONFIG_P2P */
971 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
975 sta->flags |= WLAN_STA_AUTHORIZED;
977 sta->flags &= ~WLAN_STA_AUTHORIZED;
980 if (hapd->p2p_group == NULL) {
981 if (sta->p2p_ie != NULL &&
982 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
985 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
988 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
989 MAC2STR(sta->addr), MAC2STR(dev_addr));
991 #endif /* CONFIG_P2P */
992 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
994 if (hapd->sta_authorized_cb)
995 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
996 sta->addr, authorized, dev_addr);
1002 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1003 os_snprintf(ip_addr, sizeof(ip_addr),
1004 " ip_addr=%u.%u.%u.%u",
1005 ip_addr_buf[0], ip_addr_buf[1],
1006 ip_addr_buf[2], ip_addr_buf[3]);
1008 #endif /* CONFIG_P2P */
1010 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1013 if (hapd->msg_ctx_parent &&
1014 hapd->msg_ctx_parent != hapd->msg_ctx)
1015 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1016 AP_STA_CONNECTED "%s%s",
1019 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1021 if (hapd->msg_ctx_parent &&
1022 hapd->msg_ctx_parent != hapd->msg_ctx)
1023 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1024 AP_STA_DISCONNECTED "%s", buf);
1029 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1030 const u8 *addr, u16 reason)
1033 if (sta == NULL && addr)
1034 sta = ap_get_sta(hapd, addr);
1037 hostapd_drv_sta_deauth(hapd, addr, reason);
1041 ap_sta_set_authorized(hapd, sta, 0);
1042 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1043 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1044 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1045 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1046 "for " MACSTR " (%d seconds - "
1047 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1048 __func__, MAC2STR(sta->addr),
1049 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1050 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1051 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1052 ap_handle_timer, hapd, sta);
1053 sta->timeout_next = STA_REMOVE;
1055 sta->deauth_reason = reason;
1056 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1057 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1058 eloop_register_timeout(hapd->iface->drv_flags &
1059 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1060 ap_sta_deauth_cb_timeout, hapd, sta);
1064 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1066 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1067 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1070 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1071 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1072 ap_sta_deauth_cb_timeout(hapd, sta);
1076 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1078 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1079 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1082 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1083 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1084 ap_sta_disassoc_cb_timeout(hapd, sta);
1088 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1093 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1094 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1095 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1096 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1097 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1099 (flags & WLAN_STA_SHORT_PREAMBLE ?
1100 "[SHORT_PREAMBLE]" : ""),
1101 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1102 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1103 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1104 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1105 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1106 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1107 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1108 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1109 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1110 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1111 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1112 "[WNM_SLEEP_MODE]" : ""));