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--;
174 if (hostapd_ht_operation_update(hapd->iface) > 0)
176 #endif /* CONFIG_IEEE80211N */
179 ieee802_11_set_beacons(hapd->iface);
181 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
182 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
184 ieee802_1x_free_station(sta);
185 wpa_auth_sta_deinit(sta->wpa_sm);
186 rsn_preauth_free_station(hapd, sta);
187 radius_client_flush_auth(hapd->radius, sta->addr);
189 os_free(sta->last_assoc_req);
190 os_free(sta->challenge);
192 #ifdef CONFIG_IEEE80211W
193 os_free(sta->sa_query_trans_id);
194 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
195 #endif /* CONFIG_IEEE80211W */
197 wpabuf_free(sta->wps_ie);
203 void hostapd_free_stas(struct hostapd_data *hapd)
205 struct sta_info *sta, *prev;
207 sta = hapd->sta_list;
211 if (sta->flags & WLAN_STA_AUTH) {
212 mlme_deauthenticate_indication(
213 hapd, sta, WLAN_REASON_UNSPECIFIED);
216 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
217 MAC2STR(prev->addr));
218 ap_free_sta(hapd, prev);
224 * ap_handle_timer - Per STA timer handler
225 * @eloop_ctx: struct hostapd_data *
226 * @timeout_ctx: struct sta_info *
228 * This function is called to check station activity and to remove inactive
231 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
233 struct hostapd_data *hapd = eloop_ctx;
234 struct sta_info *sta = timeout_ctx;
235 unsigned long next_time = 0;
237 if (sta->timeout_next == STA_REMOVE) {
238 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
239 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
240 "local deauth request");
241 ap_free_sta(hapd, sta);
245 if ((sta->flags & WLAN_STA_ASSOC) &&
246 (sta->timeout_next == STA_NULLFUNC ||
247 sta->timeout_next == STA_DISASSOC)) {
249 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
251 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
252 if (inactive_sec == -1) {
253 wpa_printf(MSG_DEBUG, "Could not get station info "
254 "from kernel driver for " MACSTR ".",
256 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
257 sta->flags & WLAN_STA_ASSOC) {
258 /* station activity detected; reset timeout state */
259 wpa_printf(MSG_DEBUG, " Station has been active");
260 sta->timeout_next = STA_NULLFUNC;
261 next_time = hapd->conf->ap_max_inactivity -
266 if ((sta->flags & WLAN_STA_ASSOC) &&
267 sta->timeout_next == STA_DISASSOC &&
268 !(sta->flags & WLAN_STA_PENDING_POLL)) {
269 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
270 /* data nullfunc frame poll did not produce TX errors; assume
271 * station ACKed it */
272 sta->timeout_next = STA_NULLFUNC;
273 next_time = hapd->conf->ap_max_inactivity;
277 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
282 if (sta->timeout_next == STA_NULLFUNC &&
283 (sta->flags & WLAN_STA_ASSOC)) {
284 /* send data frame to poll STA and check whether this frame
286 struct ieee80211_hdr hdr;
288 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
289 sta->flags |= WLAN_STA_PENDING_POLL;
291 #ifndef CONFIG_NATIVE_WINDOWS
292 os_memset(&hdr, 0, sizeof(hdr));
294 os_strcmp(hapd->driver->name, "hostap") == 0) {
296 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
297 * but it is apparently not retried so TX Exc events
298 * are not received for it.
301 IEEE80211_FC(WLAN_FC_TYPE_DATA,
305 IEEE80211_FC(WLAN_FC_TYPE_DATA,
306 WLAN_FC_STYPE_NULLFUNC);
309 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
310 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
311 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
313 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
315 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
316 perror("ap_handle_timer: send");
317 #endif /* CONFIG_NATIVE_WINDOWS */
318 } else if (sta->timeout_next != STA_REMOVE) {
319 int deauth = sta->timeout_next == STA_DEAUTH;
321 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
322 deauth ? "deauthentication" : "disassociation",
326 hostapd_sta_deauth(hapd, sta->addr,
327 WLAN_REASON_PREV_AUTH_NOT_VALID);
329 hostapd_sta_disassoc(
331 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
335 switch (sta->timeout_next) {
337 sta->timeout_next = STA_DISASSOC;
338 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
342 sta->flags &= ~WLAN_STA_ASSOC;
343 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
344 if (!sta->acct_terminate_cause)
345 sta->acct_terminate_cause =
346 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
347 accounting_sta_stop(hapd, sta);
348 ieee802_1x_free_station(sta);
349 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
350 HOSTAPD_LEVEL_INFO, "disassociated due to "
352 sta->timeout_next = STA_DEAUTH;
353 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
355 mlme_disassociate_indication(
356 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
360 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
361 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
363 if (!sta->acct_terminate_cause)
364 sta->acct_terminate_cause =
365 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
366 mlme_deauthenticate_indication(
368 WLAN_REASON_PREV_AUTH_NOT_VALID);
369 ap_free_sta(hapd, sta);
375 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
377 struct hostapd_data *hapd = eloop_ctx;
378 struct sta_info *sta = timeout_ctx;
381 if (!(sta->flags & WLAN_STA_AUTH))
384 mlme_deauthenticate_indication(hapd, sta,
385 WLAN_REASON_PREV_AUTH_NOT_VALID);
386 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
387 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
389 sta->acct_terminate_cause =
390 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
391 os_memcpy(addr, sta->addr, ETH_ALEN);
392 ap_free_sta(hapd, sta);
393 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
397 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
400 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
401 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
402 "seconds", session_timeout);
403 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
404 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
409 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
411 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
415 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
417 struct sta_info *sta;
419 sta = ap_get_sta(hapd, addr);
423 wpa_printf(MSG_DEBUG, " New STA");
424 if (hapd->num_sta >= hapd->conf->max_num_sta) {
425 /* FIX: might try to remove some old STAs first? */
426 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
427 hapd->num_sta, hapd->conf->max_num_sta);
431 sta = os_zalloc(sizeof(struct sta_info));
433 wpa_printf(MSG_ERROR, "malloc failed");
436 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
438 /* initialize STA info data */
439 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
440 ap_handle_timer, hapd, sta);
441 os_memcpy(sta->addr, addr, ETH_ALEN);
442 sta->next = hapd->sta_list;
443 hapd->sta_list = sta;
445 ap_sta_hash_add(hapd, sta);
446 sta->ssid = &hapd->conf->ssid;
452 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
454 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
456 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
458 if (hostapd_sta_remove(hapd, sta->addr) &&
459 sta->flags & WLAN_STA_ASSOC) {
460 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
461 " from kernel driver.", MAC2STR(sta->addr));
468 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
469 struct sta_info *sta, u32 flags)
471 struct hostapd_iface *iface = hapd->iface;
474 for (i = 0; i < iface->num_bss; i++) {
475 struct hostapd_data *bss = iface->bss[i];
476 struct sta_info *sta2;
477 /* bss should always be set during operation, but it may be
478 * NULL during reconfiguration. Assume the STA is not
479 * associated to another BSS in that case to avoid NULL pointer
481 if (bss == hapd || bss == NULL)
483 sta2 = ap_get_sta(bss, sta->addr);
484 if (sta2 && ((sta2->flags & flags) == flags))
492 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
495 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
496 hapd->conf->iface, MAC2STR(sta->addr));
497 sta->flags &= ~WLAN_STA_ASSOC;
498 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
499 ap_sta_remove(hapd, sta);
500 sta->timeout_next = STA_DEAUTH;
501 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
502 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
503 ap_handle_timer, hapd, sta);
504 accounting_sta_stop(hapd, sta);
505 ieee802_1x_free_station(sta);
507 mlme_disassociate_indication(hapd, sta, reason);
511 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
514 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
515 hapd->conf->iface, MAC2STR(sta->addr));
516 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
517 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
518 ap_sta_remove(hapd, sta);
519 sta->timeout_next = STA_REMOVE;
520 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
521 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
522 ap_handle_timer, hapd, sta);
523 accounting_sta_stop(hapd, sta);
524 ieee802_1x_free_station(sta);
526 mlme_deauthenticate_indication(hapd, sta, reason);
530 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
533 #ifndef CONFIG_NO_VLAN
535 struct hostapd_vlan *vlan = NULL;
538 * Do not proceed furthur if the vlan id remains same. We do not want
539 * duplicate dynamic vlan entries.
541 if (sta->vlan_id == old_vlanid)
545 * During 1x reauth, if the vlan id changes, then remove the old id and
546 * proceed furthur to add the new one.
549 vlan_remove_dynamic(hapd, old_vlanid);
551 iface = hapd->conf->iface;
552 if (sta->ssid->vlan[0])
553 iface = sta->ssid->vlan;
555 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
557 else if (sta->vlan_id > 0) {
558 vlan = hapd->conf->vlan;
560 if (vlan->vlan_id == sta->vlan_id ||
561 vlan->vlan_id == VLAN_ID_WILDCARD) {
562 iface = vlan->ifname;
569 if (sta->vlan_id > 0 && vlan == NULL) {
570 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
571 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
572 "binding station to (vlan_id=%d)",
575 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
576 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
578 hostapd_logger(hapd, sta->addr,
579 HOSTAPD_MODULE_IEEE80211,
580 HOSTAPD_LEVEL_DEBUG, "could not add "
581 "dynamic VLAN interface for vlan_id=%d",
586 iface = vlan->ifname;
587 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
588 hostapd_logger(hapd, sta->addr,
589 HOSTAPD_MODULE_IEEE80211,
590 HOSTAPD_LEVEL_DEBUG, "could not "
591 "configure encryption for dynamic VLAN "
592 "interface for vlan_id=%d",
596 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
597 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
598 "interface '%s'", iface);
599 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
600 if (sta->vlan_id > 0) {
601 vlan->dynamic_vlan++;
602 hostapd_logger(hapd, sta->addr,
603 HOSTAPD_MODULE_IEEE80211,
604 HOSTAPD_LEVEL_DEBUG, "updated existing "
605 "dynamic VLAN interface '%s'", iface);
609 * Update encryption configuration for statically generated
610 * VLAN interface. This is only used for static WEP
611 * configuration for the case where hostapd did not yet know
612 * which keys are to be used when the interface was added.
614 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
615 hostapd_logger(hapd, sta->addr,
616 HOSTAPD_MODULE_IEEE80211,
617 HOSTAPD_LEVEL_DEBUG, "could not "
618 "configure encryption for VLAN "
619 "interface for vlan_id=%d",
624 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
625 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
628 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
629 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
631 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
632 #else /* CONFIG_NO_VLAN */
634 #endif /* CONFIG_NO_VLAN */
638 #ifdef CONFIG_IEEE80211W
640 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
643 struct os_time now, passed;
645 os_time_sub(&now, &sta->sa_query_start, &passed);
646 tu = (passed.sec * 1000000 + passed.usec) / 1024;
647 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
648 hostapd_logger(hapd, sta->addr,
649 HOSTAPD_MODULE_IEEE80211,
651 "association SA Query timed out");
652 sta->sa_query_timed_out = 1;
653 os_free(sta->sa_query_trans_id);
654 sta->sa_query_trans_id = NULL;
655 sta->sa_query_count = 0;
656 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
664 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
666 struct hostapd_data *hapd = eloop_ctx;
667 struct sta_info *sta = timeout_ctx;
668 unsigned int timeout, sec, usec;
671 if (sta->sa_query_count > 0 &&
672 ap_check_sa_query_timeout(hapd, sta))
675 nbuf = os_realloc(sta->sa_query_trans_id,
676 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
679 if (sta->sa_query_count == 0) {
680 /* Starting a new SA Query procedure */
681 os_get_time(&sta->sa_query_start);
683 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
684 sta->sa_query_trans_id = nbuf;
685 sta->sa_query_count++;
687 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
689 timeout = hapd->conf->assoc_sa_query_retry_timeout;
690 sec = ((timeout / 1000) * 1024) / 1000;
691 usec = (timeout % 1000) * 1024;
692 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
694 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
696 "association SA Query attempt %d", sta->sa_query_count);
698 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
702 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
704 ap_sa_query_timer(hapd, sta);
708 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
710 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
711 os_free(sta->sa_query_trans_id);
712 sta->sa_query_trans_id = NULL;
713 sta->sa_query_count = 0;
716 #endif /* CONFIG_IEEE80211W */