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) / 32] &=
132 ~BIT((sta->aid - 1) % 32);
135 if (sta->nonerp_set) {
137 hapd->iface->num_sta_non_erp--;
138 if (hapd->iface->num_sta_non_erp == 0)
142 if (sta->no_short_slot_time_set) {
143 sta->no_short_slot_time_set = 0;
144 hapd->iface->num_sta_no_short_slot_time--;
145 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
146 && hapd->iface->num_sta_no_short_slot_time == 0)
150 if (sta->no_short_preamble_set) {
151 sta->no_short_preamble_set = 0;
152 hapd->iface->num_sta_no_short_preamble--;
153 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
154 && hapd->iface->num_sta_no_short_preamble == 0)
158 #ifdef CONFIG_IEEE80211N
159 if (sta->no_ht_gf_set) {
160 sta->no_ht_gf_set = 0;
161 hapd->iface->num_sta_ht_no_gf--;
164 if (sta->no_ht_set) {
166 hapd->iface->num_sta_no_ht--;
169 if (sta->ht_20mhz_set) {
170 sta->ht_20mhz_set = 0;
171 hapd->iface->num_sta_ht_20mhz--;
175 if (hostapd_ht_operation_update(hapd->iface) > 0)
177 #endif /* NEED_MLME */
178 #endif /* CONFIG_IEEE80211N */
181 ieee802_11_set_beacons(hapd->iface);
183 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
184 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
186 ieee802_1x_free_station(sta);
187 wpa_auth_sta_deinit(sta->wpa_sm);
188 rsn_preauth_free_station(hapd, sta);
189 radius_client_flush_auth(hapd->radius, sta->addr);
191 os_free(sta->last_assoc_req);
192 os_free(sta->challenge);
194 #ifdef CONFIG_IEEE80211W
195 os_free(sta->sa_query_trans_id);
196 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
197 #endif /* CONFIG_IEEE80211W */
199 wpabuf_free(sta->wps_ie);
205 void hostapd_free_stas(struct hostapd_data *hapd)
207 struct sta_info *sta, *prev;
209 sta = hapd->sta_list;
213 if (sta->flags & WLAN_STA_AUTH) {
214 mlme_deauthenticate_indication(
215 hapd, sta, WLAN_REASON_UNSPECIFIED);
218 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
219 MAC2STR(prev->addr));
220 ap_free_sta(hapd, prev);
226 * ap_handle_timer - Per STA timer handler
227 * @eloop_ctx: struct hostapd_data *
228 * @timeout_ctx: struct sta_info *
230 * This function is called to check station activity and to remove inactive
233 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
235 struct hostapd_data *hapd = eloop_ctx;
236 struct sta_info *sta = timeout_ctx;
237 unsigned long next_time = 0;
239 if (sta->timeout_next == STA_REMOVE) {
240 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
241 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
242 "local deauth request");
243 ap_free_sta(hapd, sta);
247 if ((sta->flags & WLAN_STA_ASSOC) &&
248 (sta->timeout_next == STA_NULLFUNC ||
249 sta->timeout_next == STA_DISASSOC)) {
251 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
253 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
254 if (inactive_sec == -1) {
255 wpa_printf(MSG_DEBUG, "Could not get station info "
256 "from kernel driver for " MACSTR ".",
258 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
259 sta->flags & WLAN_STA_ASSOC) {
260 /* station activity detected; reset timeout state */
261 wpa_printf(MSG_DEBUG, " Station has been active");
262 sta->timeout_next = STA_NULLFUNC;
263 next_time = hapd->conf->ap_max_inactivity -
268 if ((sta->flags & WLAN_STA_ASSOC) &&
269 sta->timeout_next == STA_DISASSOC &&
270 !(sta->flags & WLAN_STA_PENDING_POLL)) {
271 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
272 /* data nullfunc frame poll did not produce TX errors; assume
273 * station ACKed it */
274 sta->timeout_next = STA_NULLFUNC;
275 next_time = hapd->conf->ap_max_inactivity;
279 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
284 if (sta->timeout_next == STA_NULLFUNC &&
285 (sta->flags & WLAN_STA_ASSOC)) {
286 /* send data frame to poll STA and check whether this frame
288 struct ieee80211_hdr hdr;
290 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
291 sta->flags |= WLAN_STA_PENDING_POLL;
293 #ifndef CONFIG_NATIVE_WINDOWS
294 os_memset(&hdr, 0, sizeof(hdr));
296 os_strcmp(hapd->driver->name, "hostap") == 0) {
298 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
299 * but it is apparently not retried so TX Exc events
300 * are not received for it.
303 IEEE80211_FC(WLAN_FC_TYPE_DATA,
307 IEEE80211_FC(WLAN_FC_TYPE_DATA,
308 WLAN_FC_STYPE_NULLFUNC);
311 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
312 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
313 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
315 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
317 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr)) < 0)
318 perror("ap_handle_timer: send");
319 #endif /* CONFIG_NATIVE_WINDOWS */
320 } else if (sta->timeout_next != STA_REMOVE) {
321 int deauth = sta->timeout_next == STA_DEAUTH;
323 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
324 deauth ? "deauthentication" : "disassociation",
328 hostapd_sta_deauth(hapd, sta->addr,
329 WLAN_REASON_PREV_AUTH_NOT_VALID);
331 hostapd_sta_disassoc(
333 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
337 switch (sta->timeout_next) {
339 sta->timeout_next = STA_DISASSOC;
340 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
344 sta->flags &= ~WLAN_STA_ASSOC;
345 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
346 if (!sta->acct_terminate_cause)
347 sta->acct_terminate_cause =
348 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
349 accounting_sta_stop(hapd, sta);
350 ieee802_1x_free_station(sta);
351 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
352 HOSTAPD_LEVEL_INFO, "disassociated due to "
354 sta->timeout_next = STA_DEAUTH;
355 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
357 mlme_disassociate_indication(
358 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
362 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
363 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
365 if (!sta->acct_terminate_cause)
366 sta->acct_terminate_cause =
367 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
368 mlme_deauthenticate_indication(
370 WLAN_REASON_PREV_AUTH_NOT_VALID);
371 ap_free_sta(hapd, sta);
377 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
379 struct hostapd_data *hapd = eloop_ctx;
380 struct sta_info *sta = timeout_ctx;
383 if (!(sta->flags & WLAN_STA_AUTH))
386 mlme_deauthenticate_indication(hapd, sta,
387 WLAN_REASON_PREV_AUTH_NOT_VALID);
388 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
389 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
391 sta->acct_terminate_cause =
392 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
393 os_memcpy(addr, sta->addr, ETH_ALEN);
394 ap_free_sta(hapd, sta);
395 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
399 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
402 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
403 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
404 "seconds", session_timeout);
405 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
406 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
411 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
413 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
417 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
419 struct sta_info *sta;
421 sta = ap_get_sta(hapd, addr);
425 wpa_printf(MSG_DEBUG, " New STA");
426 if (hapd->num_sta >= hapd->conf->max_num_sta) {
427 /* FIX: might try to remove some old STAs first? */
428 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
429 hapd->num_sta, hapd->conf->max_num_sta);
433 sta = os_zalloc(sizeof(struct sta_info));
435 wpa_printf(MSG_ERROR, "malloc failed");
438 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
440 /* initialize STA info data */
441 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
442 ap_handle_timer, hapd, sta);
443 os_memcpy(sta->addr, addr, ETH_ALEN);
444 sta->next = hapd->sta_list;
445 hapd->sta_list = sta;
447 ap_sta_hash_add(hapd, sta);
448 sta->ssid = &hapd->conf->ssid;
454 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
456 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
458 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
460 if (hostapd_sta_remove(hapd, sta->addr) &&
461 sta->flags & WLAN_STA_ASSOC) {
462 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
463 " from kernel driver.", MAC2STR(sta->addr));
470 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
471 struct sta_info *sta, u32 flags)
473 struct hostapd_iface *iface = hapd->iface;
476 for (i = 0; i < iface->num_bss; i++) {
477 struct hostapd_data *bss = iface->bss[i];
478 struct sta_info *sta2;
479 /* bss should always be set during operation, but it may be
480 * NULL during reconfiguration. Assume the STA is not
481 * associated to another BSS in that case to avoid NULL pointer
483 if (bss == hapd || bss == NULL)
485 sta2 = ap_get_sta(bss, sta->addr);
486 if (sta2 && ((sta2->flags & flags) == flags))
494 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
497 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
498 hapd->conf->iface, MAC2STR(sta->addr));
499 sta->flags &= ~WLAN_STA_ASSOC;
500 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
501 ap_sta_remove(hapd, sta);
502 sta->timeout_next = STA_DEAUTH;
503 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
504 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
505 ap_handle_timer, hapd, sta);
506 accounting_sta_stop(hapd, sta);
507 ieee802_1x_free_station(sta);
509 mlme_disassociate_indication(hapd, sta, reason);
513 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
516 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
517 hapd->conf->iface, MAC2STR(sta->addr));
518 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
519 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
520 ap_sta_remove(hapd, sta);
521 sta->timeout_next = STA_REMOVE;
522 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
523 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
524 ap_handle_timer, hapd, sta);
525 accounting_sta_stop(hapd, sta);
526 ieee802_1x_free_station(sta);
528 mlme_deauthenticate_indication(hapd, sta, reason);
532 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
535 #ifndef CONFIG_NO_VLAN
537 struct hostapd_vlan *vlan = NULL;
540 * Do not proceed furthur if the vlan id remains same. We do not want
541 * duplicate dynamic vlan entries.
543 if (sta->vlan_id == old_vlanid)
547 * During 1x reauth, if the vlan id changes, then remove the old id and
548 * proceed furthur to add the new one.
551 vlan_remove_dynamic(hapd, old_vlanid);
553 iface = hapd->conf->iface;
554 if (sta->ssid->vlan[0])
555 iface = sta->ssid->vlan;
557 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
559 else if (sta->vlan_id > 0) {
560 vlan = hapd->conf->vlan;
562 if (vlan->vlan_id == sta->vlan_id ||
563 vlan->vlan_id == VLAN_ID_WILDCARD) {
564 iface = vlan->ifname;
571 if (sta->vlan_id > 0 && vlan == NULL) {
572 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
573 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
574 "binding station to (vlan_id=%d)",
577 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
578 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
580 hostapd_logger(hapd, sta->addr,
581 HOSTAPD_MODULE_IEEE80211,
582 HOSTAPD_LEVEL_DEBUG, "could not add "
583 "dynamic VLAN interface for vlan_id=%d",
588 iface = vlan->ifname;
589 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
590 hostapd_logger(hapd, sta->addr,
591 HOSTAPD_MODULE_IEEE80211,
592 HOSTAPD_LEVEL_DEBUG, "could not "
593 "configure encryption for dynamic VLAN "
594 "interface for vlan_id=%d",
598 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
599 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
600 "interface '%s'", iface);
601 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
602 if (sta->vlan_id > 0) {
603 vlan->dynamic_vlan++;
604 hostapd_logger(hapd, sta->addr,
605 HOSTAPD_MODULE_IEEE80211,
606 HOSTAPD_LEVEL_DEBUG, "updated existing "
607 "dynamic VLAN interface '%s'", iface);
611 * Update encryption configuration for statically generated
612 * VLAN interface. This is only used for static WEP
613 * configuration for the case where hostapd did not yet know
614 * which keys are to be used when the interface was added.
616 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
617 hostapd_logger(hapd, sta->addr,
618 HOSTAPD_MODULE_IEEE80211,
619 HOSTAPD_LEVEL_DEBUG, "could not "
620 "configure encryption for VLAN "
621 "interface for vlan_id=%d",
626 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
627 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
630 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
631 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
633 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
634 #else /* CONFIG_NO_VLAN */
636 #endif /* CONFIG_NO_VLAN */
640 #ifdef CONFIG_IEEE80211W
642 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
645 struct os_time now, passed;
647 os_time_sub(&now, &sta->sa_query_start, &passed);
648 tu = (passed.sec * 1000000 + passed.usec) / 1024;
649 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
650 hostapd_logger(hapd, sta->addr,
651 HOSTAPD_MODULE_IEEE80211,
653 "association SA Query timed out");
654 sta->sa_query_timed_out = 1;
655 os_free(sta->sa_query_trans_id);
656 sta->sa_query_trans_id = NULL;
657 sta->sa_query_count = 0;
658 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
666 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
668 struct hostapd_data *hapd = eloop_ctx;
669 struct sta_info *sta = timeout_ctx;
670 unsigned int timeout, sec, usec;
673 if (sta->sa_query_count > 0 &&
674 ap_check_sa_query_timeout(hapd, sta))
677 nbuf = os_realloc(sta->sa_query_trans_id,
678 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
681 if (sta->sa_query_count == 0) {
682 /* Starting a new SA Query procedure */
683 os_get_time(&sta->sa_query_start);
685 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
686 sta->sa_query_trans_id = nbuf;
687 sta->sa_query_count++;
689 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
691 timeout = hapd->conf->assoc_sa_query_retry_timeout;
692 sec = ((timeout / 1000) * 1024) / 1000;
693 usec = (timeout % 1000) * 1024;
694 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
696 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
698 "association SA Query attempt %d", sta->sa_query_count);
701 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
702 #endif /* NEED_MLME */
706 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
708 ap_sa_query_timer(hapd, sta);
712 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
714 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
715 os_free(sta->sa_query_trans_id);
716 sta->sa_query_trans_id = NULL;
717 sta->sa_query_count = 0;
720 #endif /* CONFIG_IEEE80211W */