2 * hostapd / Station table
3 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
9 #include "utils/includes.h"
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/sae.h"
16 #include "radius/radius.h"
17 #include "radius/radius_client.h"
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
23 #include "ieee802_11_auth.h"
25 #include "preauth_auth.h"
26 #include "ap_config.h"
29 #include "vlan_init.h"
30 #include "p2p_hostapd.h"
31 #include "ap_drv_ops.h"
34 #include "ndisc_snoop.h"
37 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
38 struct sta_info *sta);
39 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
40 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
41 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
42 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43 #ifdef CONFIG_IEEE80211W
44 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
45 #endif /* CONFIG_IEEE80211W */
46 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
48 int ap_for_each_sta(struct hostapd_data *hapd,
49 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
55 for (sta = hapd->sta_list; sta; sta = sta->next) {
56 if (cb(hapd, sta, ctx))
64 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
68 s = hapd->sta_hash[STA_HASH(sta)];
69 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
76 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
80 for (sta = hapd->sta_list; sta; sta = sta->next) {
81 const u8 *p2p_dev_addr;
83 if (sta->p2p_ie == NULL)
86 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
87 if (p2p_dev_addr == NULL)
90 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
96 #endif /* CONFIG_P2P */
99 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
101 struct sta_info *tmp;
103 if (hapd->sta_list == sta) {
104 hapd->sta_list = sta->next;
108 tmp = hapd->sta_list;
109 while (tmp != NULL && tmp->next != sta)
112 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
113 "list.", MAC2STR(sta->addr));
115 tmp->next = sta->next;
119 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
121 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
122 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
126 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
130 s = hapd->sta_hash[STA_HASH(sta->addr)];
131 if (s == NULL) return;
132 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
133 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
137 while (s->hnext != NULL &&
138 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
140 if (s->hnext != NULL)
141 s->hnext = s->hnext->hnext;
143 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
144 " from hash table", MAC2STR(sta->addr));
148 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
150 sta_ip6addr_del(hapd, sta);
154 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
158 accounting_sta_stop(hapd, sta);
161 ap_sta_set_authorized(hapd, sta, 0);
163 if (sta->flags & WLAN_STA_WDS)
164 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
167 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
168 ap_sta_ip6addr_del(hapd, sta);
170 if (!hapd->iface->driver_ap_teardown &&
171 !(sta->flags & WLAN_STA_PREAUTH))
172 hostapd_drv_sta_remove(hapd, sta->addr);
174 ap_sta_hash_del(hapd, sta);
175 ap_sta_list_del(hapd, sta);
178 hapd->sta_aid[(sta->aid - 1) / 32] &=
179 ~BIT((sta->aid - 1) % 32);
182 if (sta->nonerp_set) {
184 hapd->iface->num_sta_non_erp--;
185 if (hapd->iface->num_sta_non_erp == 0)
189 if (sta->no_short_slot_time_set) {
190 sta->no_short_slot_time_set = 0;
191 hapd->iface->num_sta_no_short_slot_time--;
192 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
193 && hapd->iface->num_sta_no_short_slot_time == 0)
197 if (sta->no_short_preamble_set) {
198 sta->no_short_preamble_set = 0;
199 hapd->iface->num_sta_no_short_preamble--;
200 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
201 && hapd->iface->num_sta_no_short_preamble == 0)
205 if (sta->no_ht_gf_set) {
206 sta->no_ht_gf_set = 0;
207 hapd->iface->num_sta_ht_no_gf--;
210 if (sta->no_ht_set) {
212 hapd->iface->num_sta_no_ht--;
215 if (sta->ht_20mhz_set) {
216 sta->ht_20mhz_set = 0;
217 hapd->iface->num_sta_ht_20mhz--;
220 #ifdef CONFIG_IEEE80211N
221 ht40_intolerant_remove(hapd->iface, sta);
222 #endif /* CONFIG_IEEE80211N */
225 if (sta->no_p2p_set) {
227 hapd->num_sta_no_p2p--;
228 if (hapd->num_sta_no_p2p == 0)
229 hostapd_p2p_non_p2p_sta_disconnected(hapd);
231 #endif /* CONFIG_P2P */
233 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
234 if (hostapd_ht_operation_update(hapd->iface) > 0)
236 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
239 if (hapd->mesh_sta_free_cb)
240 hapd->mesh_sta_free_cb(sta);
241 #endif /* CONFIG_MESH */
244 ieee802_11_set_beacons(hapd->iface);
246 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
247 __func__, MAC2STR(sta->addr));
248 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
249 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
250 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
251 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
252 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
254 ieee802_1x_free_station(sta);
255 wpa_auth_sta_deinit(sta->wpa_sm);
256 rsn_preauth_free_station(hapd, sta);
257 #ifndef CONFIG_NO_RADIUS
259 radius_client_flush_auth(hapd->radius, sta->addr);
260 #endif /* CONFIG_NO_RADIUS */
262 os_free(sta->challenge);
264 #ifdef CONFIG_IEEE80211W
265 os_free(sta->sa_query_trans_id);
266 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
267 #endif /* CONFIG_IEEE80211W */
270 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
271 #endif /* CONFIG_P2P */
273 #ifdef CONFIG_INTERWORKING
274 if (sta->gas_dialog) {
276 for (i = 0; i < GAS_DIALOG_MAX; i++)
277 gas_serv_dialog_clear(&sta->gas_dialog[i]);
278 os_free(sta->gas_dialog);
280 #endif /* CONFIG_INTERWORKING */
282 wpabuf_free(sta->wps_ie);
283 wpabuf_free(sta->p2p_ie);
284 wpabuf_free(sta->hs20_ie);
286 os_free(sta->ht_capabilities);
287 os_free(sta->vht_capabilities);
288 hostapd_free_psk_list(sta->psk);
289 os_free(sta->identity);
290 os_free(sta->radius_cui);
291 os_free(sta->remediation_url);
292 wpabuf_free(sta->hs20_deauth_req);
293 os_free(sta->hs20_session_info_url);
296 sae_clear_data(sta->sae);
298 #endif /* CONFIG_SAE */
304 void hostapd_free_stas(struct hostapd_data *hapd)
306 struct sta_info *sta, *prev;
308 sta = hapd->sta_list;
312 if (sta->flags & WLAN_STA_AUTH) {
313 mlme_deauthenticate_indication(
314 hapd, sta, WLAN_REASON_UNSPECIFIED);
317 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
318 MAC2STR(prev->addr));
319 ap_free_sta(hapd, prev);
325 * ap_handle_timer - Per STA timer handler
326 * @eloop_ctx: struct hostapd_data *
327 * @timeout_ctx: struct sta_info *
329 * This function is called to check station activity and to remove inactive
332 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
334 struct hostapd_data *hapd = eloop_ctx;
335 struct sta_info *sta = timeout_ctx;
336 unsigned long next_time = 0;
339 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
340 __func__, MAC2STR(sta->addr), sta->flags,
342 if (sta->timeout_next == STA_REMOVE) {
343 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
344 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
345 "local deauth request");
346 ap_free_sta(hapd, sta);
350 if ((sta->flags & WLAN_STA_ASSOC) &&
351 (sta->timeout_next == STA_NULLFUNC ||
352 sta->timeout_next == STA_DISASSOC)) {
355 * Add random value to timeout so that we don't end up bouncing
356 * all stations at the same time if we have lots of associated
357 * stations that are idle (but keep re-associating).
359 int fuzz = os_random() % 20;
360 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
361 if (inactive_sec == -1) {
362 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
363 "Check inactivity: Could not "
364 "get station info from kernel driver for "
365 MACSTR, MAC2STR(sta->addr));
367 * The driver may not support this functionality.
368 * Anyway, try again after the next inactivity timeout,
369 * but do not disconnect the station now.
371 next_time = hapd->conf->ap_max_inactivity + fuzz;
372 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
373 sta->flags & WLAN_STA_ASSOC) {
374 /* station activity detected; reset timeout state */
375 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
376 "Station " MACSTR " has been active %is ago",
377 MAC2STR(sta->addr), inactive_sec);
378 sta->timeout_next = STA_NULLFUNC;
379 next_time = hapd->conf->ap_max_inactivity + fuzz -
382 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
383 "Station " MACSTR " has been "
384 "inactive too long: %d sec, max allowed: %d",
385 MAC2STR(sta->addr), inactive_sec,
386 hapd->conf->ap_max_inactivity);
388 if (hapd->conf->skip_inactivity_poll)
389 sta->timeout_next = STA_DISASSOC;
393 if ((sta->flags & WLAN_STA_ASSOC) &&
394 sta->timeout_next == STA_DISASSOC &&
395 !(sta->flags & WLAN_STA_PENDING_POLL) &&
396 !hapd->conf->skip_inactivity_poll) {
397 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
398 " has ACKed data poll", MAC2STR(sta->addr));
399 /* data nullfunc frame poll did not produce TX errors; assume
400 * station ACKed it */
401 sta->timeout_next = STA_NULLFUNC;
402 next_time = hapd->conf->ap_max_inactivity;
406 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
407 "for " MACSTR " (%lu seconds)",
408 __func__, MAC2STR(sta->addr), next_time);
409 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
414 if (sta->timeout_next == STA_NULLFUNC &&
415 (sta->flags & WLAN_STA_ASSOC)) {
416 wpa_printf(MSG_DEBUG, " Polling STA");
417 sta->flags |= WLAN_STA_PENDING_POLL;
418 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
419 sta->flags & WLAN_STA_WMM);
420 } else if (sta->timeout_next != STA_REMOVE) {
421 int deauth = sta->timeout_next == STA_DEAUTH;
423 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
424 "Timeout, sending %s info to STA " MACSTR,
425 deauth ? "deauthentication" : "disassociation",
429 hostapd_drv_sta_deauth(
431 WLAN_REASON_PREV_AUTH_NOT_VALID);
433 reason = (sta->timeout_next == STA_DISASSOC) ?
434 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
435 WLAN_REASON_PREV_AUTH_NOT_VALID;
437 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
441 switch (sta->timeout_next) {
443 sta->timeout_next = STA_DISASSOC;
444 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
445 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
446 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
447 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
451 case STA_DISASSOC_FROM_CLI:
452 ap_sta_set_authorized(hapd, sta, 0);
453 sta->flags &= ~WLAN_STA_ASSOC;
454 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
455 if (!sta->acct_terminate_cause)
456 sta->acct_terminate_cause =
457 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
458 accounting_sta_stop(hapd, sta);
459 ieee802_1x_free_station(sta);
460 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
461 HOSTAPD_LEVEL_INFO, "disassociated due to "
463 reason = (sta->timeout_next == STA_DISASSOC) ?
464 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
465 WLAN_REASON_PREV_AUTH_NOT_VALID;
466 sta->timeout_next = STA_DEAUTH;
467 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
468 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
469 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
470 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
472 mlme_disassociate_indication(hapd, sta, reason);
476 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
477 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
478 "inactivity (timer DEAUTH/REMOVE)");
479 if (!sta->acct_terminate_cause)
480 sta->acct_terminate_cause =
481 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
482 mlme_deauthenticate_indication(
484 WLAN_REASON_PREV_AUTH_NOT_VALID);
485 ap_free_sta(hapd, sta);
491 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
493 struct hostapd_data *hapd = eloop_ctx;
494 struct sta_info *sta = timeout_ctx;
496 if (!(sta->flags & WLAN_STA_AUTH)) {
497 if (sta->flags & WLAN_STA_GAS) {
498 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
499 "entry " MACSTR, MAC2STR(sta->addr));
500 ap_free_sta(hapd, sta);
505 hostapd_drv_sta_deauth(hapd, sta->addr,
506 WLAN_REASON_PREV_AUTH_NOT_VALID);
507 mlme_deauthenticate_indication(hapd, sta,
508 WLAN_REASON_PREV_AUTH_NOT_VALID);
509 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
510 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
512 sta->acct_terminate_cause =
513 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
514 ap_free_sta(hapd, sta);
518 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
521 if (eloop_replenish_timeout(session_timeout, 0,
522 ap_handle_session_timer, hapd, sta) == 1) {
523 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
524 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
525 "to %d seconds", session_timeout);
530 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
533 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
534 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
535 "seconds", session_timeout);
536 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
537 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
542 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
544 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
548 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
551 struct hostapd_data *hapd = eloop_ctx;
552 struct sta_info *sta = timeout_ctx;
554 wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
556 if (sta->hs20_session_info_url == NULL)
559 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
560 sta->hs20_disassoc_timer);
561 #endif /* CONFIG_WNM */
565 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
566 struct sta_info *sta, int warning_time)
568 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
569 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
574 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
576 struct sta_info *sta;
578 sta = ap_get_sta(hapd, addr);
582 wpa_printf(MSG_DEBUG, " New STA");
583 if (hapd->num_sta >= hapd->conf->max_num_sta) {
584 /* FIX: might try to remove some old STAs first? */
585 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
586 hapd->num_sta, hapd->conf->max_num_sta);
590 sta = os_zalloc(sizeof(struct sta_info));
592 wpa_printf(MSG_ERROR, "malloc failed");
595 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
596 accounting_sta_get_id(hapd, sta);
598 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
599 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
600 "for " MACSTR " (%d seconds - ap_max_inactivity)",
601 __func__, MAC2STR(addr),
602 hapd->conf->ap_max_inactivity);
603 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
604 ap_handle_timer, hapd, sta);
607 /* initialize STA info data */
608 os_memcpy(sta->addr, addr, ETH_ALEN);
609 sta->next = hapd->sta_list;
610 hapd->sta_list = sta;
612 ap_sta_hash_add(hapd, sta);
613 sta->ssid = &hapd->conf->ssid;
614 ap_sta_remove_in_other_bss(hapd, sta);
615 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
616 dl_list_init(&sta->ip6addr);
622 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
624 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
627 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
628 ap_sta_ip6addr_del(hapd, sta);
630 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
632 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
633 sta->flags & WLAN_STA_ASSOC) {
634 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
635 " from kernel driver.", MAC2STR(sta->addr));
642 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
643 struct sta_info *sta)
645 struct hostapd_iface *iface = hapd->iface;
648 for (i = 0; i < iface->num_bss; i++) {
649 struct hostapd_data *bss = iface->bss[i];
650 struct sta_info *sta2;
651 /* bss should always be set during operation, but it may be
652 * NULL during reconfiguration. Assume the STA is not
653 * associated to another BSS in that case to avoid NULL pointer
655 if (bss == hapd || bss == NULL)
657 sta2 = ap_get_sta(bss, sta->addr);
661 ap_sta_disconnect(bss, sta2, sta2->addr,
662 WLAN_REASON_PREV_AUTH_NOT_VALID);
667 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
669 struct hostapd_data *hapd = eloop_ctx;
670 struct sta_info *sta = timeout_ctx;
672 ap_sta_remove(hapd, sta);
673 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
677 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
680 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
681 hapd->conf->iface, MAC2STR(sta->addr));
682 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
683 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
684 ap_sta_set_authorized(hapd, sta, 0);
685 sta->timeout_next = STA_DEAUTH;
686 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
687 "for " MACSTR " (%d seconds - "
688 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
689 __func__, MAC2STR(sta->addr),
690 AP_MAX_INACTIVITY_AFTER_DISASSOC);
691 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
692 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
693 ap_handle_timer, hapd, sta);
694 accounting_sta_stop(hapd, sta);
695 ieee802_1x_free_station(sta);
697 sta->disassoc_reason = reason;
698 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
699 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
700 eloop_register_timeout(hapd->iface->drv_flags &
701 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
702 ap_sta_disassoc_cb_timeout, hapd, sta);
706 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
708 struct hostapd_data *hapd = eloop_ctx;
709 struct sta_info *sta = timeout_ctx;
711 ap_sta_remove(hapd, sta);
712 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
716 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
719 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
720 hapd->conf->iface, MAC2STR(sta->addr));
721 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
722 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
723 ap_sta_set_authorized(hapd, sta, 0);
724 sta->timeout_next = STA_REMOVE;
725 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
726 "for " MACSTR " (%d seconds - "
727 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
728 __func__, MAC2STR(sta->addr),
729 AP_MAX_INACTIVITY_AFTER_DEAUTH);
730 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
731 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
732 ap_handle_timer, hapd, sta);
733 accounting_sta_stop(hapd, sta);
734 ieee802_1x_free_station(sta);
736 sta->deauth_reason = reason;
737 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
738 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
739 eloop_register_timeout(hapd->iface->drv_flags &
740 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
741 ap_sta_deauth_cb_timeout, hapd, sta);
746 int ap_sta_wps_cancel(struct hostapd_data *hapd,
747 struct sta_info *sta, void *ctx)
749 if (sta && (sta->flags & WLAN_STA_WPS)) {
750 ap_sta_deauthenticate(hapd, sta,
751 WLAN_REASON_PREV_AUTH_NOT_VALID);
752 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
753 __func__, MAC2STR(sta->addr));
759 #endif /* CONFIG_WPS */
762 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
765 #ifndef CONFIG_NO_VLAN
767 struct hostapd_vlan *vlan = NULL;
771 * Do not proceed furthur if the vlan id remains same. We do not want
772 * duplicate dynamic vlan entries.
774 if (sta->vlan_id == old_vlanid)
778 * During 1x reauth, if the vlan id changes, then remove the old id and
779 * proceed furthur to add the new one.
782 vlan_remove_dynamic(hapd, old_vlanid);
784 iface = hapd->conf->iface;
785 if (sta->ssid->vlan[0])
786 iface = sta->ssid->vlan;
788 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
790 else if (sta->vlan_id > 0) {
791 struct hostapd_vlan *wildcard_vlan = NULL;
792 vlan = hapd->conf->vlan;
794 if (vlan->vlan_id == sta->vlan_id)
796 if (vlan->vlan_id == VLAN_ID_WILDCARD)
797 wildcard_vlan = vlan;
801 vlan = wildcard_vlan;
803 iface = vlan->ifname;
806 if (sta->vlan_id > 0 && vlan == NULL) {
807 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
808 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
809 "binding station to (vlan_id=%d)",
812 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
813 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
815 hostapd_logger(hapd, sta->addr,
816 HOSTAPD_MODULE_IEEE80211,
817 HOSTAPD_LEVEL_DEBUG, "could not add "
818 "dynamic VLAN interface for vlan_id=%d",
823 iface = vlan->ifname;
824 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
825 hostapd_logger(hapd, sta->addr,
826 HOSTAPD_MODULE_IEEE80211,
827 HOSTAPD_LEVEL_DEBUG, "could not "
828 "configure encryption for dynamic VLAN "
829 "interface for vlan_id=%d",
833 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
834 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
835 "interface '%s'", iface);
836 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
837 if (sta->vlan_id > 0) {
838 vlan->dynamic_vlan++;
839 hostapd_logger(hapd, sta->addr,
840 HOSTAPD_MODULE_IEEE80211,
841 HOSTAPD_LEVEL_DEBUG, "updated existing "
842 "dynamic VLAN interface '%s'", iface);
846 * Update encryption configuration for statically generated
847 * VLAN interface. This is only used for static WEP
848 * configuration for the case where hostapd did not yet know
849 * which keys are to be used when the interface was added.
851 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
852 hostapd_logger(hapd, sta->addr,
853 HOSTAPD_MODULE_IEEE80211,
854 HOSTAPD_LEVEL_DEBUG, "could not "
855 "configure encryption for VLAN "
856 "interface for vlan_id=%d",
861 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
862 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
865 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
866 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
868 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
870 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
871 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
872 "entry to vlan_id=%d", sta->vlan_id);
875 #else /* CONFIG_NO_VLAN */
877 #endif /* CONFIG_NO_VLAN */
881 #ifdef CONFIG_IEEE80211W
883 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
886 struct os_reltime now, passed;
887 os_get_reltime(&now);
888 os_reltime_sub(&now, &sta->sa_query_start, &passed);
889 tu = (passed.sec * 1000000 + passed.usec) / 1024;
890 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
891 hostapd_logger(hapd, sta->addr,
892 HOSTAPD_MODULE_IEEE80211,
894 "association SA Query timed out");
895 sta->sa_query_timed_out = 1;
896 os_free(sta->sa_query_trans_id);
897 sta->sa_query_trans_id = NULL;
898 sta->sa_query_count = 0;
899 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
907 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
909 struct hostapd_data *hapd = eloop_ctx;
910 struct sta_info *sta = timeout_ctx;
911 unsigned int timeout, sec, usec;
914 if (sta->sa_query_count > 0 &&
915 ap_check_sa_query_timeout(hapd, sta))
918 nbuf = os_realloc_array(sta->sa_query_trans_id,
919 sta->sa_query_count + 1,
920 WLAN_SA_QUERY_TR_ID_LEN);
923 if (sta->sa_query_count == 0) {
924 /* Starting a new SA Query procedure */
925 os_get_reltime(&sta->sa_query_start);
927 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
928 sta->sa_query_trans_id = nbuf;
929 sta->sa_query_count++;
931 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
933 * We don't really care which ID is used here, so simply
934 * hardcode this if the mostly theoretical os_get_random()
941 timeout = hapd->conf->assoc_sa_query_retry_timeout;
942 sec = ((timeout / 1000) * 1024) / 1000;
943 usec = (timeout % 1000) * 1024;
944 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
946 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
948 "association SA Query attempt %d", sta->sa_query_count);
950 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
954 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
956 ap_sa_query_timer(hapd, sta);
960 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
962 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
963 os_free(sta->sa_query_trans_id);
964 sta->sa_query_trans_id = NULL;
965 sta->sa_query_count = 0;
968 #endif /* CONFIG_IEEE80211W */
971 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
974 const u8 *dev_addr = NULL;
979 #endif /* CONFIG_P2P */
981 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
985 sta->flags |= WLAN_STA_AUTHORIZED;
987 sta->flags &= ~WLAN_STA_AUTHORIZED;
990 if (hapd->p2p_group == NULL) {
991 if (sta->p2p_ie != NULL &&
992 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
995 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
998 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
999 MAC2STR(sta->addr), MAC2STR(dev_addr));
1001 #endif /* CONFIG_P2P */
1002 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1004 if (hapd->sta_authorized_cb)
1005 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1006 sta->addr, authorized, dev_addr);
1012 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1013 os_snprintf(ip_addr, sizeof(ip_addr),
1014 " ip_addr=%u.%u.%u.%u",
1015 ip_addr_buf[0], ip_addr_buf[1],
1016 ip_addr_buf[2], ip_addr_buf[3]);
1018 #endif /* CONFIG_P2P */
1020 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1023 if (hapd->msg_ctx_parent &&
1024 hapd->msg_ctx_parent != hapd->msg_ctx)
1025 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1026 AP_STA_CONNECTED "%s%s",
1029 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1031 if (hapd->msg_ctx_parent &&
1032 hapd->msg_ctx_parent != hapd->msg_ctx)
1033 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1034 AP_STA_DISCONNECTED "%s", buf);
1039 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1040 const u8 *addr, u16 reason)
1043 if (sta == NULL && addr)
1044 sta = ap_get_sta(hapd, addr);
1047 hostapd_drv_sta_deauth(hapd, addr, reason);
1051 ap_sta_set_authorized(hapd, sta, 0);
1052 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1053 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1054 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1055 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1056 "for " MACSTR " (%d seconds - "
1057 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1058 __func__, MAC2STR(sta->addr),
1059 AP_MAX_INACTIVITY_AFTER_DEAUTH);
1060 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1061 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1062 ap_handle_timer, hapd, sta);
1063 sta->timeout_next = STA_REMOVE;
1065 sta->deauth_reason = reason;
1066 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1067 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1068 eloop_register_timeout(hapd->iface->drv_flags &
1069 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1070 ap_sta_deauth_cb_timeout, hapd, sta);
1074 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1076 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1077 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1080 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1081 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1082 ap_sta_deauth_cb_timeout(hapd, sta);
1086 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1088 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1089 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1092 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1093 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1094 ap_sta_disassoc_cb_timeout(hapd, sta);
1098 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1103 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1104 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1105 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1106 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1107 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1109 (flags & WLAN_STA_SHORT_PREAMBLE ?
1110 "[SHORT_PREAMBLE]" : ""),
1111 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1112 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1113 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1114 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1115 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1116 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1117 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1118 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1119 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1120 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1121 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1122 "[WNM_SLEEP_MODE]" : ""));