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.
20 #include "sta_flags.h"
23 #include "accounting.h"
24 #include "ieee802_1x.h"
25 #include "ieee802_11.h"
26 #include "radius/radius.h"
29 #include "radius/radius_client.h"
32 #include "hw_features.h"
34 #include "vlan_init.h"
36 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
37 struct sta_info *sta, u32 flags);
38 static void ap_handle_session_timer(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 */
43 int ap_for_each_sta(struct hostapd_data *hapd,
44 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
50 for (sta = hapd->sta_list; sta; sta = sta->next) {
51 if (cb(hapd, sta, ctx))
59 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
63 s = hapd->sta_hash[STA_HASH(sta)];
64 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
70 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
74 if (hapd->sta_list == sta) {
75 hapd->sta_list = sta->next;
80 while (tmp != NULL && tmp->next != sta)
83 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
84 "list.", MAC2STR(sta->addr));
86 tmp->next = sta->next;
90 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
92 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
93 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
97 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
101 s = hapd->sta_hash[STA_HASH(sta->addr)];
102 if (s == NULL) return;
103 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
104 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
108 while (s->hnext != NULL &&
109 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
111 if (s->hnext != NULL)
112 s->hnext = s->hnext->hnext;
114 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
115 " from hash table", MAC2STR(sta->addr));
119 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
123 accounting_sta_stop(hapd, sta);
125 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
126 !(sta->flags & WLAN_STA_PREAUTH))
127 hostapd_sta_remove(hapd, sta->addr);
129 ap_sta_hash_del(hapd, sta);
130 ap_sta_list_del(hapd, sta);
133 hapd->sta_aid[(sta->aid - 1) / 32] &=
134 ~BIT((sta->aid - 1) % 32);
137 if (sta->nonerp_set) {
139 hapd->iface->num_sta_non_erp--;
140 if (hapd->iface->num_sta_non_erp == 0)
144 if (sta->no_short_slot_time_set) {
145 sta->no_short_slot_time_set = 0;
146 hapd->iface->num_sta_no_short_slot_time--;
147 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
148 && hapd->iface->num_sta_no_short_slot_time == 0)
152 if (sta->no_short_preamble_set) {
153 sta->no_short_preamble_set = 0;
154 hapd->iface->num_sta_no_short_preamble--;
155 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
156 && hapd->iface->num_sta_no_short_preamble == 0)
160 #ifdef CONFIG_IEEE80211N
161 if (sta->no_ht_gf_set) {
162 sta->no_ht_gf_set = 0;
163 hapd->iface->num_sta_ht_no_gf--;
166 if (sta->no_ht_set) {
168 hapd->iface->num_sta_no_ht--;
171 if (sta->ht_20mhz_set) {
172 sta->ht_20mhz_set = 0;
173 hapd->iface->num_sta_ht_20mhz--;
177 if (hostapd_ht_operation_update(hapd->iface) > 0)
179 #endif /* NEED_AP_MLME */
180 #endif /* CONFIG_IEEE80211N */
183 ieee802_11_set_beacons(hapd->iface);
185 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
186 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
188 ieee802_1x_free_station(sta);
189 wpa_auth_sta_deinit(sta->wpa_sm);
190 rsn_preauth_free_station(hapd, sta);
191 #ifndef CONFIG_NO_RADIUS
192 radius_client_flush_auth(hapd->radius, sta->addr);
193 #endif /* CONFIG_NO_RADIUS */
195 os_free(sta->last_assoc_req);
196 os_free(sta->challenge);
198 #ifdef CONFIG_IEEE80211W
199 os_free(sta->sa_query_trans_id);
200 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
201 #endif /* CONFIG_IEEE80211W */
203 wpabuf_free(sta->wps_ie);
205 os_free(sta->ht_capabilities);
211 void hostapd_free_stas(struct hostapd_data *hapd)
213 struct sta_info *sta, *prev;
215 sta = hapd->sta_list;
219 if (sta->flags & WLAN_STA_AUTH) {
220 mlme_deauthenticate_indication(
221 hapd, sta, WLAN_REASON_UNSPECIFIED);
224 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
225 MAC2STR(prev->addr));
226 ap_free_sta(hapd, prev);
232 * ap_handle_timer - Per STA timer handler
233 * @eloop_ctx: struct hostapd_data *
234 * @timeout_ctx: struct sta_info *
236 * This function is called to check station activity and to remove inactive
239 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
241 struct hostapd_data *hapd = eloop_ctx;
242 struct sta_info *sta = timeout_ctx;
243 unsigned long next_time = 0;
245 if (sta->timeout_next == STA_REMOVE) {
246 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
247 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
248 "local deauth request");
249 ap_free_sta(hapd, sta);
253 if ((sta->flags & WLAN_STA_ASSOC) &&
254 (sta->timeout_next == STA_NULLFUNC ||
255 sta->timeout_next == STA_DISASSOC)) {
257 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
259 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
260 if (inactive_sec == -1) {
261 wpa_printf(MSG_DEBUG, "Could not get station info "
262 "from kernel driver for " MACSTR ".",
264 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
265 sta->flags & WLAN_STA_ASSOC) {
266 /* station activity detected; reset timeout state */
267 wpa_printf(MSG_DEBUG, " Station has been active");
268 sta->timeout_next = STA_NULLFUNC;
269 next_time = hapd->conf->ap_max_inactivity -
274 if ((sta->flags & WLAN_STA_ASSOC) &&
275 sta->timeout_next == STA_DISASSOC &&
276 !(sta->flags & WLAN_STA_PENDING_POLL)) {
277 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
278 /* data nullfunc frame poll did not produce TX errors; assume
279 * station ACKed it */
280 sta->timeout_next = STA_NULLFUNC;
281 next_time = hapd->conf->ap_max_inactivity;
285 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
290 if (sta->timeout_next == STA_NULLFUNC &&
291 (sta->flags & WLAN_STA_ASSOC)) {
292 /* send data frame to poll STA and check whether this frame
294 struct ieee80211_hdr hdr;
296 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
297 sta->flags |= WLAN_STA_PENDING_POLL;
299 #ifndef CONFIG_NATIVE_WINDOWS
300 os_memset(&hdr, 0, sizeof(hdr));
302 os_strcmp(hapd->driver->name, "hostap") == 0) {
304 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
305 * but it is apparently not retried so TX Exc events
306 * are not received for it.
309 IEEE80211_FC(WLAN_FC_TYPE_DATA,
313 IEEE80211_FC(WLAN_FC_TYPE_DATA,
314 WLAN_FC_STYPE_NULLFUNC);
317 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
318 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
319 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
321 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
323 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr)) < 0)
324 perror("ap_handle_timer: send");
325 #endif /* CONFIG_NATIVE_WINDOWS */
326 } else if (sta->timeout_next != STA_REMOVE) {
327 int deauth = sta->timeout_next == STA_DEAUTH;
329 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
330 deauth ? "deauthentication" : "disassociation",
334 hostapd_sta_deauth(hapd, sta->addr,
335 WLAN_REASON_PREV_AUTH_NOT_VALID);
337 hostapd_sta_disassoc(
339 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
343 switch (sta->timeout_next) {
345 sta->timeout_next = STA_DISASSOC;
346 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
350 sta->flags &= ~WLAN_STA_ASSOC;
351 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
352 if (!sta->acct_terminate_cause)
353 sta->acct_terminate_cause =
354 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
355 accounting_sta_stop(hapd, sta);
356 ieee802_1x_free_station(sta);
357 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
358 HOSTAPD_LEVEL_INFO, "disassociated due to "
360 sta->timeout_next = STA_DEAUTH;
361 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
363 mlme_disassociate_indication(
364 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
368 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
369 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
371 if (!sta->acct_terminate_cause)
372 sta->acct_terminate_cause =
373 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
374 mlme_deauthenticate_indication(
376 WLAN_REASON_PREV_AUTH_NOT_VALID);
377 ap_free_sta(hapd, sta);
383 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
385 struct hostapd_data *hapd = eloop_ctx;
386 struct sta_info *sta = timeout_ctx;
389 if (!(sta->flags & WLAN_STA_AUTH))
392 mlme_deauthenticate_indication(hapd, sta,
393 WLAN_REASON_PREV_AUTH_NOT_VALID);
394 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
395 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
397 sta->acct_terminate_cause =
398 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
399 os_memcpy(addr, sta->addr, ETH_ALEN);
400 ap_free_sta(hapd, sta);
401 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
405 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
408 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
409 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
410 "seconds", session_timeout);
411 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
412 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
417 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
419 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
423 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
425 struct sta_info *sta;
427 sta = ap_get_sta(hapd, addr);
431 wpa_printf(MSG_DEBUG, " New STA");
432 if (hapd->num_sta >= hapd->conf->max_num_sta) {
433 /* FIX: might try to remove some old STAs first? */
434 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
435 hapd->num_sta, hapd->conf->max_num_sta);
439 sta = os_zalloc(sizeof(struct sta_info));
441 wpa_printf(MSG_ERROR, "malloc failed");
444 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
446 /* initialize STA info data */
447 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
448 ap_handle_timer, hapd, sta);
449 os_memcpy(sta->addr, addr, ETH_ALEN);
450 sta->next = hapd->sta_list;
451 hapd->sta_list = sta;
453 ap_sta_hash_add(hapd, sta);
454 sta->ssid = &hapd->conf->ssid;
460 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
462 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
464 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
466 if (hostapd_sta_remove(hapd, sta->addr) &&
467 sta->flags & WLAN_STA_ASSOC) {
468 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
469 " from kernel driver.", MAC2STR(sta->addr));
476 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
477 struct sta_info *sta, u32 flags)
479 struct hostapd_iface *iface = hapd->iface;
482 for (i = 0; i < iface->num_bss; i++) {
483 struct hostapd_data *bss = iface->bss[i];
484 struct sta_info *sta2;
485 /* bss should always be set during operation, but it may be
486 * NULL during reconfiguration. Assume the STA is not
487 * associated to another BSS in that case to avoid NULL pointer
489 if (bss == hapd || bss == NULL)
491 sta2 = ap_get_sta(bss, sta->addr);
492 if (sta2 && ((sta2->flags & flags) == flags))
500 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
503 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
504 hapd->conf->iface, MAC2STR(sta->addr));
505 sta->flags &= ~WLAN_STA_ASSOC;
506 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
507 ap_sta_remove(hapd, sta);
508 sta->timeout_next = STA_DEAUTH;
509 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
510 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
511 ap_handle_timer, hapd, sta);
512 accounting_sta_stop(hapd, sta);
513 ieee802_1x_free_station(sta);
515 mlme_disassociate_indication(hapd, sta, reason);
519 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
522 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
523 hapd->conf->iface, MAC2STR(sta->addr));
524 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
525 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
526 ap_sta_remove(hapd, sta);
527 sta->timeout_next = STA_REMOVE;
528 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
529 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
530 ap_handle_timer, hapd, sta);
531 accounting_sta_stop(hapd, sta);
532 ieee802_1x_free_station(sta);
534 mlme_deauthenticate_indication(hapd, sta, reason);
538 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
541 #ifndef CONFIG_NO_VLAN
543 struct hostapd_vlan *vlan = NULL;
546 * Do not proceed furthur if the vlan id remains same. We do not want
547 * duplicate dynamic vlan entries.
549 if (sta->vlan_id == old_vlanid)
553 * During 1x reauth, if the vlan id changes, then remove the old id and
554 * proceed furthur to add the new one.
557 vlan_remove_dynamic(hapd, old_vlanid);
559 iface = hapd->conf->iface;
560 if (sta->ssid->vlan[0])
561 iface = sta->ssid->vlan;
563 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
565 else if (sta->vlan_id > 0) {
566 vlan = hapd->conf->vlan;
568 if (vlan->vlan_id == sta->vlan_id ||
569 vlan->vlan_id == VLAN_ID_WILDCARD) {
570 iface = vlan->ifname;
577 if (sta->vlan_id > 0 && vlan == NULL) {
578 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
579 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
580 "binding station to (vlan_id=%d)",
583 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
584 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
586 hostapd_logger(hapd, sta->addr,
587 HOSTAPD_MODULE_IEEE80211,
588 HOSTAPD_LEVEL_DEBUG, "could not add "
589 "dynamic VLAN interface for vlan_id=%d",
594 iface = vlan->ifname;
595 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
596 hostapd_logger(hapd, sta->addr,
597 HOSTAPD_MODULE_IEEE80211,
598 HOSTAPD_LEVEL_DEBUG, "could not "
599 "configure encryption for dynamic VLAN "
600 "interface for vlan_id=%d",
604 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
605 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
606 "interface '%s'", iface);
607 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
608 if (sta->vlan_id > 0) {
609 vlan->dynamic_vlan++;
610 hostapd_logger(hapd, sta->addr,
611 HOSTAPD_MODULE_IEEE80211,
612 HOSTAPD_LEVEL_DEBUG, "updated existing "
613 "dynamic VLAN interface '%s'", iface);
617 * Update encryption configuration for statically generated
618 * VLAN interface. This is only used for static WEP
619 * configuration for the case where hostapd did not yet know
620 * which keys are to be used when the interface was added.
622 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
623 hostapd_logger(hapd, sta->addr,
624 HOSTAPD_MODULE_IEEE80211,
625 HOSTAPD_LEVEL_DEBUG, "could not "
626 "configure encryption for VLAN "
627 "interface for vlan_id=%d",
632 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
633 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
636 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
637 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
639 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
640 #else /* CONFIG_NO_VLAN */
642 #endif /* CONFIG_NO_VLAN */
646 #ifdef CONFIG_IEEE80211W
648 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
651 struct os_time now, passed;
653 os_time_sub(&now, &sta->sa_query_start, &passed);
654 tu = (passed.sec * 1000000 + passed.usec) / 1024;
655 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
656 hostapd_logger(hapd, sta->addr,
657 HOSTAPD_MODULE_IEEE80211,
659 "association SA Query timed out");
660 sta->sa_query_timed_out = 1;
661 os_free(sta->sa_query_trans_id);
662 sta->sa_query_trans_id = NULL;
663 sta->sa_query_count = 0;
664 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
672 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
674 struct hostapd_data *hapd = eloop_ctx;
675 struct sta_info *sta = timeout_ctx;
676 unsigned int timeout, sec, usec;
679 if (sta->sa_query_count > 0 &&
680 ap_check_sa_query_timeout(hapd, sta))
683 nbuf = os_realloc(sta->sa_query_trans_id,
684 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
687 if (sta->sa_query_count == 0) {
688 /* Starting a new SA Query procedure */
689 os_get_time(&sta->sa_query_start);
691 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
692 sta->sa_query_trans_id = nbuf;
693 sta->sa_query_count++;
695 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
697 timeout = hapd->conf->assoc_sa_query_retry_timeout;
698 sec = ((timeout / 1000) * 1024) / 1000;
699 usec = (timeout % 1000) * 1024;
700 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
702 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
704 "association SA Query attempt %d", sta->sa_query_count);
707 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
708 #endif /* NEED_AP_MLME */
712 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
714 ap_sa_query_timer(hapd, sta);
718 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
720 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
721 os_free(sta->sa_query_trans_id);
722 sta->sa_query_trans_id = NULL;
723 sta->sa_query_count = 0;
726 #endif /* CONFIG_IEEE80211W */