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"
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"
35 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
36 struct sta_info *sta);
37 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
38 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
39 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
40 #ifdef CONFIG_IEEE80211W
41 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
42 #endif /* CONFIG_IEEE80211W */
43 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
45 int ap_for_each_sta(struct hostapd_data *hapd,
46 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
52 for (sta = hapd->sta_list; sta; sta = sta->next) {
53 if (cb(hapd, sta, ctx))
61 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
65 s = hapd->sta_hash[STA_HASH(sta)];
66 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
72 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
76 if (hapd->sta_list == sta) {
77 hapd->sta_list = sta->next;
82 while (tmp != NULL && tmp->next != sta)
85 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
86 "list.", MAC2STR(sta->addr));
88 tmp->next = sta->next;
92 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
94 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
95 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
99 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
103 s = hapd->sta_hash[STA_HASH(sta->addr)];
104 if (s == NULL) return;
105 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
106 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
110 while (s->hnext != NULL &&
111 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
113 if (s->hnext != NULL)
114 s->hnext = s->hnext->hnext;
116 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
117 " from hash table", MAC2STR(sta->addr));
121 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
125 accounting_sta_stop(hapd, sta);
128 ap_sta_set_authorized(hapd, sta, 0);
130 if (sta->flags & WLAN_STA_WDS)
131 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
133 if (!(sta->flags & WLAN_STA_PREAUTH))
134 hostapd_drv_sta_remove(hapd, sta->addr);
136 ap_sta_hash_del(hapd, sta);
137 ap_sta_list_del(hapd, sta);
140 hapd->sta_aid[(sta->aid - 1) / 32] &=
141 ~BIT((sta->aid - 1) % 32);
144 if (sta->nonerp_set) {
146 hapd->iface->num_sta_non_erp--;
147 if (hapd->iface->num_sta_non_erp == 0)
151 if (sta->no_short_slot_time_set) {
152 sta->no_short_slot_time_set = 0;
153 hapd->iface->num_sta_no_short_slot_time--;
154 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
155 && hapd->iface->num_sta_no_short_slot_time == 0)
159 if (sta->no_short_preamble_set) {
160 sta->no_short_preamble_set = 0;
161 hapd->iface->num_sta_no_short_preamble--;
162 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
163 && hapd->iface->num_sta_no_short_preamble == 0)
167 if (sta->no_ht_gf_set) {
168 sta->no_ht_gf_set = 0;
169 hapd->iface->num_sta_ht_no_gf--;
172 if (sta->no_ht_set) {
174 hapd->iface->num_sta_no_ht--;
177 if (sta->ht_20mhz_set) {
178 sta->ht_20mhz_set = 0;
179 hapd->iface->num_sta_ht_20mhz--;
183 if (sta->no_p2p_set) {
185 hapd->num_sta_no_p2p--;
186 if (hapd->num_sta_no_p2p == 0)
187 hostapd_p2p_non_p2p_sta_disconnected(hapd);
189 #endif /* CONFIG_P2P */
191 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
192 if (hostapd_ht_operation_update(hapd->iface) > 0)
194 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
197 ieee802_11_set_beacons(hapd->iface);
199 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
200 __func__, MAC2STR(sta->addr));
201 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
202 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
203 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
204 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
206 ieee802_1x_free_station(sta);
207 wpa_auth_sta_deinit(sta->wpa_sm);
208 rsn_preauth_free_station(hapd, sta);
209 #ifndef CONFIG_NO_RADIUS
210 radius_client_flush_auth(hapd->radius, sta->addr);
211 #endif /* CONFIG_NO_RADIUS */
213 os_free(sta->last_assoc_req);
214 os_free(sta->challenge);
216 #ifdef CONFIG_IEEE80211W
217 os_free(sta->sa_query_trans_id);
218 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
219 #endif /* CONFIG_IEEE80211W */
222 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
223 #endif /* CONFIG_P2P */
225 #ifdef CONFIG_INTERWORKING
226 if (sta->gas_dialog) {
228 for (i = 0; i < GAS_DIALOG_MAX; i++)
229 gas_serv_dialog_clear(&sta->gas_dialog[i]);
230 os_free(sta->gas_dialog);
232 #endif /* CONFIG_INTERWORKING */
234 wpabuf_free(sta->wps_ie);
235 wpabuf_free(sta->p2p_ie);
236 wpabuf_free(sta->hs20_ie);
238 os_free(sta->ht_capabilities);
239 hostapd_free_psk_list(sta->psk);
240 os_free(sta->identity);
241 os_free(sta->radius_cui);
247 void hostapd_free_stas(struct hostapd_data *hapd)
249 struct sta_info *sta, *prev;
251 sta = hapd->sta_list;
255 if (sta->flags & WLAN_STA_AUTH) {
256 mlme_deauthenticate_indication(
257 hapd, sta, WLAN_REASON_UNSPECIFIED);
260 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
261 MAC2STR(prev->addr));
262 ap_free_sta(hapd, prev);
268 * ap_handle_timer - Per STA timer handler
269 * @eloop_ctx: struct hostapd_data *
270 * @timeout_ctx: struct sta_info *
272 * This function is called to check station activity and to remove inactive
275 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
277 struct hostapd_data *hapd = eloop_ctx;
278 struct sta_info *sta = timeout_ctx;
279 unsigned long next_time = 0;
281 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
282 __func__, MAC2STR(sta->addr), sta->flags,
284 if (sta->timeout_next == STA_REMOVE) {
285 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
286 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
287 "local deauth request");
288 ap_free_sta(hapd, sta);
292 if ((sta->flags & WLAN_STA_ASSOC) &&
293 (sta->timeout_next == STA_NULLFUNC ||
294 sta->timeout_next == STA_DISASSOC)) {
297 * Add random value to timeout so that we don't end up bouncing
298 * all stations at the same time if we have lots of associated
299 * stations that are idle (but keep re-associating).
301 int fuzz = os_random() % 20;
302 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
303 if (inactive_sec == -1) {
304 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
305 "Check inactivity: Could not "
306 "get station info from kernel driver for "
307 MACSTR, MAC2STR(sta->addr));
309 * The driver may not support this functionality.
310 * Anyway, try again after the next inactivity timeout,
311 * but do not disconnect the station now.
313 next_time = hapd->conf->ap_max_inactivity + fuzz;
314 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
315 sta->flags & WLAN_STA_ASSOC) {
316 /* station activity detected; reset timeout state */
317 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
318 "Station " MACSTR " has been active %is ago",
319 MAC2STR(sta->addr), inactive_sec);
320 sta->timeout_next = STA_NULLFUNC;
321 next_time = hapd->conf->ap_max_inactivity + fuzz -
324 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
325 "Station " MACSTR " has been "
326 "inactive too long: %d sec, max allowed: %d",
327 MAC2STR(sta->addr), inactive_sec,
328 hapd->conf->ap_max_inactivity);
330 if (hapd->conf->skip_inactivity_poll)
331 sta->timeout_next = STA_DISASSOC;
335 if ((sta->flags & WLAN_STA_ASSOC) &&
336 sta->timeout_next == STA_DISASSOC &&
337 !(sta->flags & WLAN_STA_PENDING_POLL) &&
338 !hapd->conf->skip_inactivity_poll) {
339 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
340 " has ACKed data poll", MAC2STR(sta->addr));
341 /* data nullfunc frame poll did not produce TX errors; assume
342 * station ACKed it */
343 sta->timeout_next = STA_NULLFUNC;
344 next_time = hapd->conf->ap_max_inactivity;
348 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
349 "for " MACSTR " (%lu seconds)",
350 __func__, MAC2STR(sta->addr), next_time);
351 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
356 if (sta->timeout_next == STA_NULLFUNC &&
357 (sta->flags & WLAN_STA_ASSOC)) {
358 wpa_printf(MSG_DEBUG, " Polling STA");
359 sta->flags |= WLAN_STA_PENDING_POLL;
360 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
361 sta->flags & WLAN_STA_WMM);
362 } else if (sta->timeout_next != STA_REMOVE) {
363 int deauth = sta->timeout_next == STA_DEAUTH;
365 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
366 "Timeout, sending %s info to STA " MACSTR,
367 deauth ? "deauthentication" : "disassociation",
371 hostapd_drv_sta_deauth(
373 WLAN_REASON_PREV_AUTH_NOT_VALID);
375 hostapd_drv_sta_disassoc(
377 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
381 switch (sta->timeout_next) {
383 sta->timeout_next = STA_DISASSOC;
384 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
385 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
386 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
387 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
391 ap_sta_set_authorized(hapd, sta, 0);
392 sta->flags &= ~WLAN_STA_ASSOC;
393 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
394 if (!sta->acct_terminate_cause)
395 sta->acct_terminate_cause =
396 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
397 accounting_sta_stop(hapd, sta);
398 ieee802_1x_free_station(sta);
399 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
400 HOSTAPD_LEVEL_INFO, "disassociated due to "
402 sta->timeout_next = STA_DEAUTH;
403 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
404 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
405 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
406 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
408 mlme_disassociate_indication(
409 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
413 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
414 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
415 "inactivity (timer DEAUTH/REMOVE)");
416 if (!sta->acct_terminate_cause)
417 sta->acct_terminate_cause =
418 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
419 mlme_deauthenticate_indication(
421 WLAN_REASON_PREV_AUTH_NOT_VALID);
422 ap_free_sta(hapd, sta);
428 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
430 struct hostapd_data *hapd = eloop_ctx;
431 struct sta_info *sta = timeout_ctx;
434 if (!(sta->flags & WLAN_STA_AUTH)) {
435 if (sta->flags & WLAN_STA_GAS) {
436 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
437 "entry " MACSTR, MAC2STR(sta->addr));
438 ap_free_sta(hapd, sta);
443 mlme_deauthenticate_indication(hapd, sta,
444 WLAN_REASON_PREV_AUTH_NOT_VALID);
445 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
446 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
448 sta->acct_terminate_cause =
449 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
450 os_memcpy(addr, sta->addr, ETH_ALEN);
451 ap_free_sta(hapd, sta);
452 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
456 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
459 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
460 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
461 "seconds", session_timeout);
462 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
463 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
468 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
470 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
474 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
476 struct sta_info *sta;
478 sta = ap_get_sta(hapd, addr);
482 wpa_printf(MSG_DEBUG, " New STA");
483 if (hapd->num_sta >= hapd->conf->max_num_sta) {
484 /* FIX: might try to remove some old STAs first? */
485 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
486 hapd->num_sta, hapd->conf->max_num_sta);
490 sta = os_zalloc(sizeof(struct sta_info));
492 wpa_printf(MSG_ERROR, "malloc failed");
495 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
496 accounting_sta_get_id(hapd, sta);
498 /* initialize STA info data */
499 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
500 "for " MACSTR " (%d seconds - ap_max_inactivity)",
501 __func__, MAC2STR(addr),
502 hapd->conf->ap_max_inactivity);
503 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
504 ap_handle_timer, hapd, sta);
505 os_memcpy(sta->addr, addr, ETH_ALEN);
506 sta->next = hapd->sta_list;
507 hapd->sta_list = sta;
509 ap_sta_hash_add(hapd, sta);
510 sta->ssid = &hapd->conf->ssid;
511 ap_sta_remove_in_other_bss(hapd, sta);
517 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
519 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
521 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
523 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
524 sta->flags & WLAN_STA_ASSOC) {
525 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
526 " from kernel driver.", MAC2STR(sta->addr));
533 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
534 struct sta_info *sta)
536 struct hostapd_iface *iface = hapd->iface;
539 for (i = 0; i < iface->num_bss; i++) {
540 struct hostapd_data *bss = iface->bss[i];
541 struct sta_info *sta2;
542 /* bss should always be set during operation, but it may be
543 * NULL during reconfiguration. Assume the STA is not
544 * associated to another BSS in that case to avoid NULL pointer
546 if (bss == hapd || bss == NULL)
548 sta2 = ap_get_sta(bss, sta->addr);
552 ap_sta_disconnect(bss, sta2, sta2->addr,
553 WLAN_REASON_PREV_AUTH_NOT_VALID);
558 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
560 struct hostapd_data *hapd = eloop_ctx;
561 struct sta_info *sta = timeout_ctx;
563 ap_sta_remove(hapd, sta);
564 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
568 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
571 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
572 hapd->conf->iface, MAC2STR(sta->addr));
573 sta->flags &= ~WLAN_STA_ASSOC;
574 ap_sta_set_authorized(hapd, sta, 0);
575 sta->timeout_next = STA_DEAUTH;
576 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
577 "for " MACSTR " (%d seconds - "
578 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
579 __func__, MAC2STR(sta->addr),
580 AP_MAX_INACTIVITY_AFTER_DISASSOC);
581 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
582 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
583 ap_handle_timer, hapd, sta);
584 accounting_sta_stop(hapd, sta);
585 ieee802_1x_free_station(sta);
587 sta->disassoc_reason = reason;
588 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
589 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
590 eloop_register_timeout(hapd->iface->drv_flags &
591 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
592 ap_sta_disassoc_cb_timeout, hapd, sta);
596 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
598 struct hostapd_data *hapd = eloop_ctx;
599 struct sta_info *sta = timeout_ctx;
601 ap_sta_remove(hapd, sta);
602 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
606 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
609 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
610 hapd->conf->iface, MAC2STR(sta->addr));
611 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
612 ap_sta_set_authorized(hapd, sta, 0);
613 sta->timeout_next = STA_REMOVE;
614 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
615 "for " MACSTR " (%d seconds - "
616 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
617 __func__, MAC2STR(sta->addr),
618 AP_MAX_INACTIVITY_AFTER_DEAUTH);
619 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
620 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
621 ap_handle_timer, hapd, sta);
622 accounting_sta_stop(hapd, sta);
623 ieee802_1x_free_station(sta);
625 sta->deauth_reason = reason;
626 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
627 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
628 eloop_register_timeout(hapd->iface->drv_flags &
629 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
630 ap_sta_deauth_cb_timeout, hapd, sta);
635 int ap_sta_wps_cancel(struct hostapd_data *hapd,
636 struct sta_info *sta, void *ctx)
638 if (sta && (sta->flags & WLAN_STA_WPS)) {
639 ap_sta_deauthenticate(hapd, sta,
640 WLAN_REASON_PREV_AUTH_NOT_VALID);
641 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
642 __func__, MAC2STR(sta->addr));
648 #endif /* CONFIG_WPS */
651 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
654 #ifndef CONFIG_NO_VLAN
656 struct hostapd_vlan *vlan = NULL;
660 * Do not proceed furthur if the vlan id remains same. We do not want
661 * duplicate dynamic vlan entries.
663 if (sta->vlan_id == old_vlanid)
667 * During 1x reauth, if the vlan id changes, then remove the old id and
668 * proceed furthur to add the new one.
671 vlan_remove_dynamic(hapd, old_vlanid);
673 iface = hapd->conf->iface;
674 if (sta->ssid->vlan[0])
675 iface = sta->ssid->vlan;
677 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
679 else if (sta->vlan_id > 0) {
680 vlan = hapd->conf->vlan;
682 if (vlan->vlan_id == sta->vlan_id ||
683 vlan->vlan_id == VLAN_ID_WILDCARD) {
684 iface = vlan->ifname;
691 if (sta->vlan_id > 0 && vlan == NULL) {
692 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
693 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
694 "binding station to (vlan_id=%d)",
697 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
698 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
700 hostapd_logger(hapd, sta->addr,
701 HOSTAPD_MODULE_IEEE80211,
702 HOSTAPD_LEVEL_DEBUG, "could not add "
703 "dynamic VLAN interface for vlan_id=%d",
708 iface = vlan->ifname;
709 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
710 hostapd_logger(hapd, sta->addr,
711 HOSTAPD_MODULE_IEEE80211,
712 HOSTAPD_LEVEL_DEBUG, "could not "
713 "configure encryption for dynamic VLAN "
714 "interface for vlan_id=%d",
718 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
719 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
720 "interface '%s'", iface);
721 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
722 if (sta->vlan_id > 0) {
723 vlan->dynamic_vlan++;
724 hostapd_logger(hapd, sta->addr,
725 HOSTAPD_MODULE_IEEE80211,
726 HOSTAPD_LEVEL_DEBUG, "updated existing "
727 "dynamic VLAN interface '%s'", iface);
731 * Update encryption configuration for statically generated
732 * VLAN interface. This is only used for static WEP
733 * configuration for the case where hostapd did not yet know
734 * which keys are to be used when the interface was added.
736 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
737 hostapd_logger(hapd, sta->addr,
738 HOSTAPD_MODULE_IEEE80211,
739 HOSTAPD_LEVEL_DEBUG, "could not "
740 "configure encryption for VLAN "
741 "interface for vlan_id=%d",
746 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
747 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
750 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
751 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
753 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
755 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
756 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
757 "entry to vlan_id=%d", sta->vlan_id);
760 #else /* CONFIG_NO_VLAN */
762 #endif /* CONFIG_NO_VLAN */
766 #ifdef CONFIG_IEEE80211W
768 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
771 struct os_time now, passed;
773 os_time_sub(&now, &sta->sa_query_start, &passed);
774 tu = (passed.sec * 1000000 + passed.usec) / 1024;
775 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
776 hostapd_logger(hapd, sta->addr,
777 HOSTAPD_MODULE_IEEE80211,
779 "association SA Query timed out");
780 sta->sa_query_timed_out = 1;
781 os_free(sta->sa_query_trans_id);
782 sta->sa_query_trans_id = NULL;
783 sta->sa_query_count = 0;
784 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
792 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
794 struct hostapd_data *hapd = eloop_ctx;
795 struct sta_info *sta = timeout_ctx;
796 unsigned int timeout, sec, usec;
799 if (sta->sa_query_count > 0 &&
800 ap_check_sa_query_timeout(hapd, sta))
803 nbuf = os_realloc_array(sta->sa_query_trans_id,
804 sta->sa_query_count + 1,
805 WLAN_SA_QUERY_TR_ID_LEN);
808 if (sta->sa_query_count == 0) {
809 /* Starting a new SA Query procedure */
810 os_get_time(&sta->sa_query_start);
812 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
813 sta->sa_query_trans_id = nbuf;
814 sta->sa_query_count++;
816 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
818 timeout = hapd->conf->assoc_sa_query_retry_timeout;
819 sec = ((timeout / 1000) * 1024) / 1000;
820 usec = (timeout % 1000) * 1024;
821 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
823 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
825 "association SA Query attempt %d", sta->sa_query_count);
827 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
831 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
833 ap_sa_query_timer(hapd, sta);
837 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
839 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
840 os_free(sta->sa_query_trans_id);
841 sta->sa_query_trans_id = NULL;
842 sta->sa_query_count = 0;
845 #endif /* CONFIG_IEEE80211W */
848 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
851 const u8 *dev_addr = NULL;
854 #endif /* CONFIG_P2P */
856 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
860 if (hapd->p2p_group == NULL) {
861 if (sta->p2p_ie != NULL &&
862 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
865 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
866 #endif /* CONFIG_P2P */
870 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
871 MACSTR " p2p_dev_addr=" MACSTR,
872 MAC2STR(sta->addr), MAC2STR(dev_addr));
874 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
875 MACSTR, MAC2STR(sta->addr));
876 if (hapd->msg_ctx_parent &&
877 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
878 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
879 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
881 MAC2STR(sta->addr), MAC2STR(dev_addr));
882 else if (hapd->msg_ctx_parent &&
883 hapd->msg_ctx_parent != hapd->msg_ctx)
884 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
885 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
887 sta->flags |= WLAN_STA_AUTHORIZED;
890 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
891 MACSTR " p2p_dev_addr=" MACSTR,
892 MAC2STR(sta->addr), MAC2STR(dev_addr));
894 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
895 MACSTR, MAC2STR(sta->addr));
896 if (hapd->msg_ctx_parent &&
897 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
898 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
899 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
900 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
901 else if (hapd->msg_ctx_parent &&
902 hapd->msg_ctx_parent != hapd->msg_ctx)
903 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
904 AP_STA_DISCONNECTED MACSTR,
906 sta->flags &= ~WLAN_STA_AUTHORIZED;
909 if (hapd->sta_authorized_cb)
910 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
911 sta->addr, authorized, dev_addr);
915 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
916 const u8 *addr, u16 reason)
919 if (sta == NULL && addr)
920 sta = ap_get_sta(hapd, addr);
923 hostapd_drv_sta_deauth(hapd, addr, reason);
927 ap_sta_set_authorized(hapd, sta, 0);
928 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
929 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
930 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
931 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
932 "for " MACSTR " (%d seconds - "
933 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
934 __func__, MAC2STR(sta->addr),
935 AP_MAX_INACTIVITY_AFTER_DEAUTH);
936 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
937 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
938 ap_handle_timer, hapd, sta);
939 sta->timeout_next = STA_REMOVE;
941 sta->deauth_reason = reason;
942 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
943 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
944 eloop_register_timeout(hapd->iface->drv_flags &
945 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
946 ap_sta_deauth_cb_timeout, hapd, sta);
950 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
952 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
953 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
956 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
957 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
958 ap_sta_deauth_cb_timeout(hapd, sta);
962 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
964 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
965 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
968 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
969 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
970 ap_sta_disassoc_cb_timeout(hapd, sta);