2 * hostapd / Station table
3 * Copyright (c) 2002-2008, Jouni Malinen <j@w1.fi>
4 * Copyright (c) 2007-2008, Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
10 * Alternatively, this software may be distributed under the terms of BSD
13 * See README and COPYING for more details.
21 #include "accounting.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "radius/radius.h"
27 #include "radius/radius_client.h"
30 #include "hw_features.h"
32 #include "vlan_init.h"
34 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
35 struct sta_info *sta, u32 flags);
36 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
37 #ifdef CONFIG_IEEE80211W
38 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
39 #endif /* CONFIG_IEEE80211W */
41 int ap_for_each_sta(struct hostapd_data *hapd,
42 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
48 for (sta = hapd->sta_list; sta; sta = sta->next) {
49 if (cb(hapd, sta, ctx))
57 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
61 s = hapd->sta_hash[STA_HASH(sta)];
62 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
68 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
72 if (hapd->sta_list == sta) {
73 hapd->sta_list = sta->next;
78 while (tmp != NULL && tmp->next != sta)
81 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
82 "list.", MAC2STR(sta->addr));
84 tmp->next = sta->next;
88 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
90 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
91 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
95 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
99 s = hapd->sta_hash[STA_HASH(sta->addr)];
100 if (s == NULL) return;
101 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
102 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
106 while (s->hnext != NULL &&
107 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
109 if (s->hnext != NULL)
110 s->hnext = s->hnext->hnext;
112 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
113 " from hash table", MAC2STR(sta->addr));
117 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
121 accounting_sta_stop(hapd, sta);
123 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
124 !(sta->flags & WLAN_STA_PREAUTH))
125 hostapd_sta_remove(hapd, sta->addr);
127 ap_sta_hash_del(hapd, sta);
128 ap_sta_list_del(hapd, sta);
131 hapd->sta_aid[sta->aid - 1] = NULL;
134 if (sta->nonerp_set) {
136 hapd->iface->num_sta_non_erp--;
137 if (hapd->iface->num_sta_non_erp == 0)
141 if (sta->no_short_slot_time_set) {
142 sta->no_short_slot_time_set = 0;
143 hapd->iface->num_sta_no_short_slot_time--;
144 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
145 && hapd->iface->num_sta_no_short_slot_time == 0)
149 if (sta->no_short_preamble_set) {
150 sta->no_short_preamble_set = 0;
151 hapd->iface->num_sta_no_short_preamble--;
152 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
153 && hapd->iface->num_sta_no_short_preamble == 0)
157 #ifdef CONFIG_IEEE80211N
158 if (sta->no_ht_gf_set) {
159 sta->no_ht_gf_set = 0;
160 hapd->iface->num_sta_ht_no_gf--;
163 if (sta->no_ht_set) {
165 hapd->iface->num_sta_no_ht--;
168 if (sta->ht_20mhz_set) {
169 sta->ht_20mhz_set = 0;
170 hapd->iface->num_sta_ht_20mhz--;
173 if (hostapd_ht_operation_update(hapd->iface) > 0)
175 #endif /* CONFIG_IEEE80211N */
178 ieee802_11_set_beacons(hapd->iface);
180 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
181 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
183 ieee802_1x_free_station(sta);
184 wpa_auth_sta_deinit(sta->wpa_sm);
185 rsn_preauth_free_station(hapd, sta);
186 radius_client_flush_auth(hapd->radius, sta->addr);
188 os_free(sta->last_assoc_req);
189 os_free(sta->challenge);
191 #ifdef CONFIG_IEEE80211W
192 os_free(sta->sa_query_trans_id);
193 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
194 #endif /* CONFIG_IEEE80211W */
196 wpabuf_free(sta->wps_ie);
202 void hostapd_free_stas(struct hostapd_data *hapd)
204 struct sta_info *sta, *prev;
206 sta = hapd->sta_list;
210 if (sta->flags & WLAN_STA_AUTH) {
211 mlme_deauthenticate_indication(
212 hapd, sta, WLAN_REASON_UNSPECIFIED);
215 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
216 MAC2STR(prev->addr));
217 ap_free_sta(hapd, prev);
223 * ap_handle_timer - Per STA timer handler
224 * @eloop_ctx: struct hostapd_data *
225 * @timeout_ctx: struct sta_info *
227 * This function is called to check station activity and to remove inactive
230 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
232 struct hostapd_data *hapd = eloop_ctx;
233 struct sta_info *sta = timeout_ctx;
234 unsigned long next_time = 0;
236 if (sta->timeout_next == STA_REMOVE) {
237 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
238 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
239 "local deauth request");
240 ap_free_sta(hapd, sta);
244 if ((sta->flags & WLAN_STA_ASSOC) &&
245 (sta->timeout_next == STA_NULLFUNC ||
246 sta->timeout_next == STA_DISASSOC)) {
248 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
250 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
251 if (inactive_sec == -1) {
252 wpa_printf(MSG_DEBUG, "Could not get station info "
253 "from kernel driver for " MACSTR ".",
255 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
256 sta->flags & WLAN_STA_ASSOC) {
257 /* station activity detected; reset timeout state */
258 wpa_printf(MSG_DEBUG, " Station has been active");
259 sta->timeout_next = STA_NULLFUNC;
260 next_time = hapd->conf->ap_max_inactivity -
265 if ((sta->flags & WLAN_STA_ASSOC) &&
266 sta->timeout_next == STA_DISASSOC &&
267 !(sta->flags & WLAN_STA_PENDING_POLL)) {
268 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
269 /* data nullfunc frame poll did not produce TX errors; assume
270 * station ACKed it */
271 sta->timeout_next = STA_NULLFUNC;
272 next_time = hapd->conf->ap_max_inactivity;
276 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
281 if (sta->timeout_next == STA_NULLFUNC &&
282 (sta->flags & WLAN_STA_ASSOC)) {
283 /* send data frame to poll STA and check whether this frame
285 struct ieee80211_hdr hdr;
287 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
288 sta->flags |= WLAN_STA_PENDING_POLL;
290 #ifndef CONFIG_NATIVE_WINDOWS
291 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
292 * it is apparently not retried so TX Exc events are not
294 os_memset(&hdr, 0, sizeof(hdr));
296 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
297 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
298 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
299 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
301 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
303 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
304 perror("ap_handle_timer: send");
305 #endif /* CONFIG_NATIVE_WINDOWS */
306 } else if (sta->timeout_next != STA_REMOVE) {
307 int deauth = sta->timeout_next == STA_DEAUTH;
309 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
310 deauth ? "deauthentication" : "disassociation",
314 hostapd_sta_deauth(hapd, sta->addr,
315 WLAN_REASON_PREV_AUTH_NOT_VALID);
317 hostapd_sta_disassoc(
319 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
323 switch (sta->timeout_next) {
325 sta->timeout_next = STA_DISASSOC;
326 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
330 sta->flags &= ~WLAN_STA_ASSOC;
331 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
332 if (!sta->acct_terminate_cause)
333 sta->acct_terminate_cause =
334 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
335 accounting_sta_stop(hapd, sta);
336 ieee802_1x_free_station(sta);
337 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
338 HOSTAPD_LEVEL_INFO, "disassociated due to "
340 sta->timeout_next = STA_DEAUTH;
341 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
343 mlme_disassociate_indication(
344 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
348 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
349 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
351 if (!sta->acct_terminate_cause)
352 sta->acct_terminate_cause =
353 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
354 mlme_deauthenticate_indication(
356 WLAN_REASON_PREV_AUTH_NOT_VALID);
357 ap_free_sta(hapd, sta);
363 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
365 struct hostapd_data *hapd = eloop_ctx;
366 struct sta_info *sta = timeout_ctx;
369 if (!(sta->flags & WLAN_STA_AUTH))
372 mlme_deauthenticate_indication(hapd, sta,
373 WLAN_REASON_PREV_AUTH_NOT_VALID);
374 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
375 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
377 sta->acct_terminate_cause =
378 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
379 os_memcpy(addr, sta->addr, ETH_ALEN);
380 ap_free_sta(hapd, sta);
381 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
385 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
388 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
389 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
390 "seconds", session_timeout);
391 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
392 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
397 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
399 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
403 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
405 struct sta_info *sta;
407 sta = ap_get_sta(hapd, addr);
411 wpa_printf(MSG_DEBUG, " New STA");
412 if (hapd->num_sta >= hapd->conf->max_num_sta) {
413 /* FIX: might try to remove some old STAs first? */
414 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
415 hapd->num_sta, hapd->conf->max_num_sta);
419 sta = os_zalloc(sizeof(struct sta_info));
421 wpa_printf(MSG_ERROR, "malloc failed");
424 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
426 /* initialize STA info data */
427 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
428 ap_handle_timer, hapd, sta);
429 os_memcpy(sta->addr, addr, ETH_ALEN);
430 sta->next = hapd->sta_list;
431 hapd->sta_list = sta;
433 ap_sta_hash_add(hapd, sta);
434 sta->ssid = &hapd->conf->ssid;
440 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
442 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
444 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
446 if (hostapd_sta_remove(hapd, sta->addr) &&
447 sta->flags & WLAN_STA_ASSOC) {
448 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
449 " from kernel driver.", MAC2STR(sta->addr));
456 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
457 struct sta_info *sta, u32 flags)
459 struct hostapd_iface *iface = hapd->iface;
462 for (i = 0; i < iface->num_bss; i++) {
463 struct hostapd_data *bss = iface->bss[i];
464 struct sta_info *sta2;
465 /* bss should always be set during operation, but it may be
466 * NULL during reconfiguration. Assume the STA is not
467 * associated to another BSS in that case to avoid NULL pointer
469 if (bss == hapd || bss == NULL)
471 sta2 = ap_get_sta(bss, sta->addr);
472 if (sta2 && ((sta2->flags & flags) == flags))
480 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
483 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
484 hapd->conf->iface, MAC2STR(sta->addr));
485 sta->flags &= ~WLAN_STA_ASSOC;
486 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
487 ap_sta_remove(hapd, sta);
488 sta->timeout_next = STA_DEAUTH;
489 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
490 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
491 ap_handle_timer, hapd, sta);
492 accounting_sta_stop(hapd, sta);
493 ieee802_1x_free_station(sta);
495 mlme_disassociate_indication(hapd, sta, reason);
499 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
502 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
503 hapd->conf->iface, MAC2STR(sta->addr));
504 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
505 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
506 ap_sta_remove(hapd, sta);
507 sta->timeout_next = STA_REMOVE;
508 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
509 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
510 ap_handle_timer, hapd, sta);
511 accounting_sta_stop(hapd, sta);
512 ieee802_1x_free_station(sta);
514 mlme_deauthenticate_indication(hapd, sta, reason);
518 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
521 #ifndef CONFIG_NO_VLAN
523 struct hostapd_vlan *vlan = NULL;
526 * Do not proceed furthur if the vlan id remains same. We do not want
527 * duplicate dynamic vlan entries.
529 if (sta->vlan_id == old_vlanid)
533 * During 1x reauth, if the vlan id changes, then remove the old id and
534 * proceed furthur to add the new one.
537 vlan_remove_dynamic(hapd, old_vlanid);
539 iface = hapd->conf->iface;
540 if (sta->ssid->vlan[0])
541 iface = sta->ssid->vlan;
543 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
545 else if (sta->vlan_id > 0) {
546 vlan = hapd->conf->vlan;
548 if (vlan->vlan_id == sta->vlan_id ||
549 vlan->vlan_id == VLAN_ID_WILDCARD) {
550 iface = vlan->ifname;
557 if (sta->vlan_id > 0 && vlan == NULL) {
558 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
559 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
560 "binding station to (vlan_id=%d)",
563 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
564 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
566 hostapd_logger(hapd, sta->addr,
567 HOSTAPD_MODULE_IEEE80211,
568 HOSTAPD_LEVEL_DEBUG, "could not add "
569 "dynamic VLAN interface for vlan_id=%d",
574 iface = vlan->ifname;
575 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
576 hostapd_logger(hapd, sta->addr,
577 HOSTAPD_MODULE_IEEE80211,
578 HOSTAPD_LEVEL_DEBUG, "could not "
579 "configure encryption for dynamic VLAN "
580 "interface for vlan_id=%d",
584 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
585 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
586 "interface '%s'", iface);
587 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
588 if (sta->vlan_id > 0) {
589 vlan->dynamic_vlan++;
590 hostapd_logger(hapd, sta->addr,
591 HOSTAPD_MODULE_IEEE80211,
592 HOSTAPD_LEVEL_DEBUG, "updated existing "
593 "dynamic VLAN interface '%s'", iface);
597 * Update encryption configuration for statically generated
598 * VLAN interface. This is only used for static WEP
599 * configuration for the case where hostapd did not yet know
600 * which keys are to be used when the interface was added.
602 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
603 hostapd_logger(hapd, sta->addr,
604 HOSTAPD_MODULE_IEEE80211,
605 HOSTAPD_LEVEL_DEBUG, "could not "
606 "configure encryption for VLAN "
607 "interface for vlan_id=%d",
612 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
613 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
616 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
617 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
619 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
620 #else /* CONFIG_NO_VLAN */
622 #endif /* CONFIG_NO_VLAN */
626 #ifdef CONFIG_IEEE80211W
628 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
631 struct os_time now, passed;
633 os_time_sub(&now, &sta->sa_query_start, &passed);
634 tu = (passed.sec * 1000000 + passed.usec) / 1024;
635 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
636 hostapd_logger(hapd, sta->addr,
637 HOSTAPD_MODULE_IEEE80211,
639 "association SA Query timed out");
640 sta->sa_query_timed_out = 1;
641 os_free(sta->sa_query_trans_id);
642 sta->sa_query_trans_id = NULL;
643 sta->sa_query_count = 0;
644 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
652 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
654 struct hostapd_data *hapd = eloop_ctx;
655 struct sta_info *sta = timeout_ctx;
656 unsigned int timeout, sec, usec;
659 if (sta->sa_query_count > 0 &&
660 ap_check_sa_query_timeout(hapd, sta))
663 nbuf = os_realloc(sta->sa_query_trans_id,
664 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
667 if (sta->sa_query_count == 0) {
668 /* Starting a new SA Query procedure */
669 os_get_time(&sta->sa_query_start);
671 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
672 sta->sa_query_trans_id = nbuf;
673 sta->sa_query_count++;
675 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
677 timeout = hapd->conf->assoc_sa_query_retry_timeout;
678 sec = ((timeout / 1000) * 1024) / 1000;
679 usec = (timeout % 1000) * 1024;
680 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
682 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
684 "association SA Query attempt %d", sta->sa_query_count);
686 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
690 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
692 ap_sa_query_timer(hapd, sta);
696 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
698 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
699 os_free(sta->sa_query_trans_id);
700 sta->sa_query_trans_id = NULL;
701 sta->sa_query_count = 0;
704 #endif /* CONFIG_IEEE80211W */