2 * hostapd / Station table
3 * Copyright (c) 2002-2011, 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 "radius/radius.h"
16 #include "radius/radius_client.h"
17 #include "drivers/driver.h"
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
24 #include "preauth_auth.h"
25 #include "ap_config.h"
28 #include "vlan_init.h"
29 #include "p2p_hostapd.h"
30 #include "ap_drv_ops.h"
34 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
35 struct sta_info *sta);
36 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
37 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
38 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
39 #ifdef CONFIG_IEEE80211W
40 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
41 #endif /* CONFIG_IEEE80211W */
42 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
44 int ap_for_each_sta(struct hostapd_data *hapd,
45 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
51 for (sta = hapd->sta_list; sta; sta = sta->next) {
52 if (cb(hapd, sta, ctx))
60 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
64 s = hapd->sta_hash[STA_HASH(sta)];
65 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
71 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
75 if (hapd->sta_list == sta) {
76 hapd->sta_list = sta->next;
81 while (tmp != NULL && tmp->next != sta)
84 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
85 "list.", MAC2STR(sta->addr));
87 tmp->next = sta->next;
91 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
93 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
94 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
98 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
102 s = hapd->sta_hash[STA_HASH(sta->addr)];
103 if (s == NULL) return;
104 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
105 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
109 while (s->hnext != NULL &&
110 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
112 if (s->hnext != NULL)
113 s->hnext = s->hnext->hnext;
115 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
116 " from hash table", MAC2STR(sta->addr));
120 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
124 accounting_sta_stop(hapd, sta);
127 ap_sta_set_authorized(hapd, sta, 0);
129 if (sta->flags & WLAN_STA_WDS)
130 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
132 if (!(sta->flags & WLAN_STA_PREAUTH))
133 hostapd_drv_sta_remove(hapd, sta->addr);
135 ap_sta_hash_del(hapd, sta);
136 ap_sta_list_del(hapd, sta);
139 hapd->sta_aid[(sta->aid - 1) / 32] &=
140 ~BIT((sta->aid - 1) % 32);
143 if (sta->nonerp_set) {
145 hapd->iface->num_sta_non_erp--;
146 if (hapd->iface->num_sta_non_erp == 0)
150 if (sta->no_short_slot_time_set) {
151 sta->no_short_slot_time_set = 0;
152 hapd->iface->num_sta_no_short_slot_time--;
153 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
154 && hapd->iface->num_sta_no_short_slot_time == 0)
158 if (sta->no_short_preamble_set) {
159 sta->no_short_preamble_set = 0;
160 hapd->iface->num_sta_no_short_preamble--;
161 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
162 && hapd->iface->num_sta_no_short_preamble == 0)
166 if (sta->no_ht_gf_set) {
167 sta->no_ht_gf_set = 0;
168 hapd->iface->num_sta_ht_no_gf--;
171 if (sta->no_ht_set) {
173 hapd->iface->num_sta_no_ht--;
176 if (sta->ht_20mhz_set) {
177 sta->ht_20mhz_set = 0;
178 hapd->iface->num_sta_ht_20mhz--;
182 if (sta->no_p2p_set) {
184 hapd->num_sta_no_p2p--;
185 if (hapd->num_sta_no_p2p == 0)
186 hostapd_p2p_non_p2p_sta_disconnected(hapd);
188 #endif /* CONFIG_P2P */
190 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
191 if (hostapd_ht_operation_update(hapd->iface) > 0)
193 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
196 ieee802_11_set_beacons(hapd->iface);
198 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
199 __func__, MAC2STR(sta->addr));
200 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
201 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
202 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
203 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
205 ieee802_1x_free_station(sta);
206 wpa_auth_sta_deinit(sta->wpa_sm);
207 rsn_preauth_free_station(hapd, sta);
208 #ifndef CONFIG_NO_RADIUS
209 radius_client_flush_auth(hapd->radius, sta->addr);
210 #endif /* CONFIG_NO_RADIUS */
212 os_free(sta->last_assoc_req);
213 os_free(sta->challenge);
215 #ifdef CONFIG_IEEE80211W
216 os_free(sta->sa_query_trans_id);
217 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
218 #endif /* CONFIG_IEEE80211W */
221 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
222 #endif /* CONFIG_P2P */
224 #ifdef CONFIG_INTERWORKING
225 if (sta->gas_dialog) {
227 for (i = 0; i < GAS_DIALOG_MAX; i++)
228 gas_serv_dialog_clear(&sta->gas_dialog[i]);
229 os_free(sta->gas_dialog);
231 #endif /* CONFIG_INTERWORKING */
233 wpabuf_free(sta->wps_ie);
234 wpabuf_free(sta->p2p_ie);
236 os_free(sta->ht_capabilities);
243 void hostapd_free_stas(struct hostapd_data *hapd)
245 struct sta_info *sta, *prev;
247 sta = hapd->sta_list;
251 if (sta->flags & WLAN_STA_AUTH) {
252 mlme_deauthenticate_indication(
253 hapd, sta, WLAN_REASON_UNSPECIFIED);
256 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
257 MAC2STR(prev->addr));
258 ap_free_sta(hapd, prev);
264 * ap_handle_timer - Per STA timer handler
265 * @eloop_ctx: struct hostapd_data *
266 * @timeout_ctx: struct sta_info *
268 * This function is called to check station activity and to remove inactive
271 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
273 struct hostapd_data *hapd = eloop_ctx;
274 struct sta_info *sta = timeout_ctx;
275 unsigned long next_time = 0;
277 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
278 __func__, MAC2STR(sta->addr), sta->flags,
280 if (sta->timeout_next == STA_REMOVE) {
281 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
282 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
283 "local deauth request");
284 ap_free_sta(hapd, sta);
288 if ((sta->flags & WLAN_STA_ASSOC) &&
289 (sta->timeout_next == STA_NULLFUNC ||
290 sta->timeout_next == STA_DISASSOC)) {
293 * Add random value to timeout so that we don't end up bouncing
294 * all stations at the same time if we have lots of associated
295 * stations that are idle (but keep re-associating).
297 int fuzz = os_random() % 20;
298 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
299 if (inactive_sec == -1) {
300 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
301 "Check inactivity: Could not "
302 "get station info from kernel driver for "
303 MACSTR, MAC2STR(sta->addr));
305 * The driver may not support this functionality.
306 * Anyway, try again after the next inactivity timeout,
307 * but do not disconnect the station now.
309 next_time = hapd->conf->ap_max_inactivity + fuzz;
310 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
311 sta->flags & WLAN_STA_ASSOC) {
312 /* station activity detected; reset timeout state */
313 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
314 "Station " MACSTR " has been active %is ago",
315 MAC2STR(sta->addr), inactive_sec);
316 sta->timeout_next = STA_NULLFUNC;
317 next_time = hapd->conf->ap_max_inactivity + fuzz -
320 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
321 "Station " MACSTR " has been "
322 "inactive too long: %d sec, max allowed: %d",
323 MAC2STR(sta->addr), inactive_sec,
324 hapd->conf->ap_max_inactivity);
326 if (hapd->conf->skip_inactivity_poll)
327 sta->timeout_next = STA_DISASSOC;
331 if ((sta->flags & WLAN_STA_ASSOC) &&
332 sta->timeout_next == STA_DISASSOC &&
333 !(sta->flags & WLAN_STA_PENDING_POLL) &&
334 !hapd->conf->skip_inactivity_poll) {
335 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
336 " has ACKed data poll", MAC2STR(sta->addr));
337 /* data nullfunc frame poll did not produce TX errors; assume
338 * station ACKed it */
339 sta->timeout_next = STA_NULLFUNC;
340 next_time = hapd->conf->ap_max_inactivity;
344 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
345 "for " MACSTR " (%lu seconds)",
346 __func__, MAC2STR(sta->addr), next_time);
347 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
352 if (sta->timeout_next == STA_NULLFUNC &&
353 (sta->flags & WLAN_STA_ASSOC)) {
354 wpa_printf(MSG_DEBUG, " Polling STA");
355 sta->flags |= WLAN_STA_PENDING_POLL;
356 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
357 sta->flags & WLAN_STA_WMM);
358 } else if (sta->timeout_next != STA_REMOVE) {
359 int deauth = sta->timeout_next == STA_DEAUTH;
361 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
362 "Timeout, sending %s info to STA " MACSTR,
363 deauth ? "deauthentication" : "disassociation",
367 hostapd_drv_sta_deauth(
369 WLAN_REASON_PREV_AUTH_NOT_VALID);
371 hostapd_drv_sta_disassoc(
373 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
377 switch (sta->timeout_next) {
379 sta->timeout_next = STA_DISASSOC;
380 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
381 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
382 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
383 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
387 ap_sta_set_authorized(hapd, sta, 0);
388 sta->flags &= ~WLAN_STA_ASSOC;
389 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
390 if (!sta->acct_terminate_cause)
391 sta->acct_terminate_cause =
392 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
393 accounting_sta_stop(hapd, sta);
394 ieee802_1x_free_station(sta);
395 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
396 HOSTAPD_LEVEL_INFO, "disassociated due to "
398 sta->timeout_next = STA_DEAUTH;
399 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
400 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
401 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
402 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
404 mlme_disassociate_indication(
405 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
409 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
410 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
411 "inactivity (timer DEAUTH/REMOVE)");
412 if (!sta->acct_terminate_cause)
413 sta->acct_terminate_cause =
414 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
415 mlme_deauthenticate_indication(
417 WLAN_REASON_PREV_AUTH_NOT_VALID);
418 ap_free_sta(hapd, sta);
424 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
426 struct hostapd_data *hapd = eloop_ctx;
427 struct sta_info *sta = timeout_ctx;
430 if (!(sta->flags & WLAN_STA_AUTH)) {
431 if (sta->flags & WLAN_STA_GAS) {
432 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
433 "entry " MACSTR, MAC2STR(sta->addr));
434 ap_free_sta(hapd, sta);
439 mlme_deauthenticate_indication(hapd, sta,
440 WLAN_REASON_PREV_AUTH_NOT_VALID);
441 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
442 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
444 sta->acct_terminate_cause =
445 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
446 os_memcpy(addr, sta->addr, ETH_ALEN);
447 ap_free_sta(hapd, sta);
448 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
452 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
455 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
456 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
457 "seconds", session_timeout);
458 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
459 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
464 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
466 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
470 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
472 struct sta_info *sta;
474 sta = ap_get_sta(hapd, addr);
478 wpa_printf(MSG_DEBUG, " New STA");
479 if (hapd->num_sta >= hapd->conf->max_num_sta) {
480 /* FIX: might try to remove some old STAs first? */
481 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
482 hapd->num_sta, hapd->conf->max_num_sta);
486 sta = os_zalloc(sizeof(struct sta_info));
488 wpa_printf(MSG_ERROR, "malloc failed");
491 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
493 /* initialize STA info data */
494 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
495 "for " MACSTR " (%d seconds - ap_max_inactivity)",
496 __func__, MAC2STR(addr),
497 hapd->conf->ap_max_inactivity);
498 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
499 ap_handle_timer, hapd, sta);
500 os_memcpy(sta->addr, addr, ETH_ALEN);
501 sta->next = hapd->sta_list;
502 hapd->sta_list = sta;
504 ap_sta_hash_add(hapd, sta);
505 sta->ssid = &hapd->conf->ssid;
506 ap_sta_remove_in_other_bss(hapd, sta);
512 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
514 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
516 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
518 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
519 sta->flags & WLAN_STA_ASSOC) {
520 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
521 " from kernel driver.", MAC2STR(sta->addr));
528 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
529 struct sta_info *sta)
531 struct hostapd_iface *iface = hapd->iface;
534 for (i = 0; i < iface->num_bss; i++) {
535 struct hostapd_data *bss = iface->bss[i];
536 struct sta_info *sta2;
537 /* bss should always be set during operation, but it may be
538 * NULL during reconfiguration. Assume the STA is not
539 * associated to another BSS in that case to avoid NULL pointer
541 if (bss == hapd || bss == NULL)
543 sta2 = ap_get_sta(bss, sta->addr);
547 ap_sta_disconnect(bss, sta2, sta2->addr,
548 WLAN_REASON_PREV_AUTH_NOT_VALID);
553 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
555 struct hostapd_data *hapd = eloop_ctx;
556 struct sta_info *sta = timeout_ctx;
558 ap_sta_remove(hapd, sta);
559 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
563 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
566 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
567 hapd->conf->iface, MAC2STR(sta->addr));
568 sta->flags &= ~WLAN_STA_ASSOC;
569 ap_sta_set_authorized(hapd, sta, 0);
570 sta->timeout_next = STA_DEAUTH;
571 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
572 "for " MACSTR " (%d seconds - "
573 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
574 __func__, MAC2STR(sta->addr),
575 AP_MAX_INACTIVITY_AFTER_DISASSOC);
576 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
577 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
578 ap_handle_timer, hapd, sta);
579 accounting_sta_stop(hapd, sta);
580 ieee802_1x_free_station(sta);
582 sta->disassoc_reason = reason;
583 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
584 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
585 eloop_register_timeout(hapd->iface->drv_flags &
586 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
587 ap_sta_disassoc_cb_timeout, hapd, sta);
591 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
593 struct hostapd_data *hapd = eloop_ctx;
594 struct sta_info *sta = timeout_ctx;
596 ap_sta_remove(hapd, sta);
597 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
601 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
604 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
605 hapd->conf->iface, MAC2STR(sta->addr));
606 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
607 ap_sta_set_authorized(hapd, sta, 0);
608 sta->timeout_next = STA_REMOVE;
609 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
610 "for " MACSTR " (%d seconds - "
611 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
612 __func__, MAC2STR(sta->addr),
613 AP_MAX_INACTIVITY_AFTER_DEAUTH);
614 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
615 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
616 ap_handle_timer, hapd, sta);
617 accounting_sta_stop(hapd, sta);
618 ieee802_1x_free_station(sta);
620 sta->deauth_reason = reason;
621 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
622 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
623 eloop_register_timeout(hapd->iface->drv_flags &
624 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
625 ap_sta_deauth_cb_timeout, hapd, sta);
630 int ap_sta_wps_cancel(struct hostapd_data *hapd,
631 struct sta_info *sta, void *ctx)
633 if (sta && (sta->flags & WLAN_STA_WPS)) {
634 ap_sta_deauthenticate(hapd, sta,
635 WLAN_REASON_PREV_AUTH_NOT_VALID);
636 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
637 __func__, MAC2STR(sta->addr));
643 #endif /* CONFIG_WPS */
646 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
649 #ifndef CONFIG_NO_VLAN
651 struct hostapd_vlan *vlan = NULL;
655 * Do not proceed furthur if the vlan id remains same. We do not want
656 * duplicate dynamic vlan entries.
658 if (sta->vlan_id == old_vlanid)
662 * During 1x reauth, if the vlan id changes, then remove the old id and
663 * proceed furthur to add the new one.
666 vlan_remove_dynamic(hapd, old_vlanid);
668 iface = hapd->conf->iface;
669 if (sta->ssid->vlan[0])
670 iface = sta->ssid->vlan;
672 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
674 else if (sta->vlan_id > 0) {
675 vlan = hapd->conf->vlan;
677 if (vlan->vlan_id == sta->vlan_id ||
678 vlan->vlan_id == VLAN_ID_WILDCARD) {
679 iface = vlan->ifname;
686 if (sta->vlan_id > 0 && vlan == NULL) {
687 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
688 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
689 "binding station to (vlan_id=%d)",
692 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
693 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
695 hostapd_logger(hapd, sta->addr,
696 HOSTAPD_MODULE_IEEE80211,
697 HOSTAPD_LEVEL_DEBUG, "could not add "
698 "dynamic VLAN interface for vlan_id=%d",
703 iface = vlan->ifname;
704 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
705 hostapd_logger(hapd, sta->addr,
706 HOSTAPD_MODULE_IEEE80211,
707 HOSTAPD_LEVEL_DEBUG, "could not "
708 "configure encryption for dynamic VLAN "
709 "interface for vlan_id=%d",
713 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
714 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
715 "interface '%s'", iface);
716 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
717 if (sta->vlan_id > 0) {
718 vlan->dynamic_vlan++;
719 hostapd_logger(hapd, sta->addr,
720 HOSTAPD_MODULE_IEEE80211,
721 HOSTAPD_LEVEL_DEBUG, "updated existing "
722 "dynamic VLAN interface '%s'", iface);
726 * Update encryption configuration for statically generated
727 * VLAN interface. This is only used for static WEP
728 * configuration for the case where hostapd did not yet know
729 * which keys are to be used when the interface was added.
731 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
732 hostapd_logger(hapd, sta->addr,
733 HOSTAPD_MODULE_IEEE80211,
734 HOSTAPD_LEVEL_DEBUG, "could not "
735 "configure encryption for VLAN "
736 "interface for vlan_id=%d",
741 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
742 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
745 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
746 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
748 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
750 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
751 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
752 "entry to vlan_id=%d", sta->vlan_id);
755 #else /* CONFIG_NO_VLAN */
757 #endif /* CONFIG_NO_VLAN */
761 #ifdef CONFIG_IEEE80211W
763 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
766 struct os_time now, passed;
768 os_time_sub(&now, &sta->sa_query_start, &passed);
769 tu = (passed.sec * 1000000 + passed.usec) / 1024;
770 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
771 hostapd_logger(hapd, sta->addr,
772 HOSTAPD_MODULE_IEEE80211,
774 "association SA Query timed out");
775 sta->sa_query_timed_out = 1;
776 os_free(sta->sa_query_trans_id);
777 sta->sa_query_trans_id = NULL;
778 sta->sa_query_count = 0;
779 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
787 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
789 struct hostapd_data *hapd = eloop_ctx;
790 struct sta_info *sta = timeout_ctx;
791 unsigned int timeout, sec, usec;
794 if (sta->sa_query_count > 0 &&
795 ap_check_sa_query_timeout(hapd, sta))
798 nbuf = os_realloc(sta->sa_query_trans_id,
799 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
802 if (sta->sa_query_count == 0) {
803 /* Starting a new SA Query procedure */
804 os_get_time(&sta->sa_query_start);
806 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
807 sta->sa_query_trans_id = nbuf;
808 sta->sa_query_count++;
810 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
812 timeout = hapd->conf->assoc_sa_query_retry_timeout;
813 sec = ((timeout / 1000) * 1024) / 1000;
814 usec = (timeout % 1000) * 1024;
815 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
817 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
819 "association SA Query attempt %d", sta->sa_query_count);
822 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
823 #endif /* NEED_AP_MLME */
827 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
829 ap_sa_query_timer(hapd, sta);
833 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
835 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
836 os_free(sta->sa_query_trans_id);
837 sta->sa_query_trans_id = NULL;
838 sta->sa_query_count = 0;
841 #endif /* CONFIG_IEEE80211W */
844 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
847 const u8 *dev_addr = NULL;
850 #endif /* CONFIG_P2P */
852 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
856 if (hapd->p2p_group == NULL) {
857 if (sta->p2p_ie != NULL &&
858 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
861 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
862 #endif /* CONFIG_P2P */
866 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
867 MACSTR " p2p_dev_addr=" MACSTR,
868 MAC2STR(sta->addr), MAC2STR(dev_addr));
870 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
871 MACSTR, MAC2STR(sta->addr));
872 if (hapd->msg_ctx_parent &&
873 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
874 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
875 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
877 MAC2STR(sta->addr), MAC2STR(dev_addr));
878 else if (hapd->msg_ctx_parent &&
879 hapd->msg_ctx_parent != hapd->msg_ctx)
880 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
881 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
883 sta->flags |= WLAN_STA_AUTHORIZED;
886 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
887 MACSTR " p2p_dev_addr=" MACSTR,
888 MAC2STR(sta->addr), MAC2STR(dev_addr));
890 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
891 MACSTR, MAC2STR(sta->addr));
892 if (hapd->msg_ctx_parent &&
893 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
894 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
895 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
896 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
897 else if (hapd->msg_ctx_parent &&
898 hapd->msg_ctx_parent != hapd->msg_ctx)
899 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
900 AP_STA_DISCONNECTED MACSTR,
902 sta->flags &= ~WLAN_STA_AUTHORIZED;
905 if (hapd->sta_authorized_cb)
906 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
907 sta->addr, authorized, dev_addr);
911 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
912 const u8 *addr, u16 reason)
915 if (sta == NULL && addr)
916 sta = ap_get_sta(hapd, addr);
919 hostapd_drv_sta_deauth(hapd, addr, reason);
923 ap_sta_set_authorized(hapd, sta, 0);
924 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
925 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
926 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
927 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
928 "for " MACSTR " (%d seconds - "
929 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
930 __func__, MAC2STR(sta->addr),
931 AP_MAX_INACTIVITY_AFTER_DEAUTH);
932 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
933 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
934 ap_handle_timer, hapd, sta);
935 sta->timeout_next = STA_REMOVE;
937 sta->deauth_reason = reason;
938 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
939 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
940 eloop_register_timeout(hapd->iface->drv_flags &
941 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
942 ap_sta_deauth_cb_timeout, hapd, sta);
946 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
948 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
949 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
952 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
953 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
954 ap_sta_deauth_cb_timeout(hapd, sta);
958 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
960 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
961 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
964 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
965 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
966 ap_sta_disassoc_cb_timeout(hapd, sta);