2 * hostapd / Station table
3 * Copyright (c) 2002-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
23 #include "radius/radius.h"
26 #include "radius/radius_client.h"
29 #include "hw_features.h"
31 #include "vlan_init.h"
33 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
34 struct sta_info *sta, u32 flags);
35 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
37 int ap_for_each_sta(struct hostapd_data *hapd,
38 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
44 for (sta = hapd->sta_list; sta; sta = sta->next) {
45 if (cb(hapd, sta, ctx))
53 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
57 s = hapd->sta_hash[STA_HASH(sta)];
58 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
64 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
68 if (hapd->sta_list == sta) {
69 hapd->sta_list = sta->next;
74 while (tmp != NULL && tmp->next != sta)
77 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
78 "list.", MAC2STR(sta->addr));
80 tmp->next = sta->next;
84 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
86 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
87 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
91 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
95 s = hapd->sta_hash[STA_HASH(sta->addr)];
96 if (s == NULL) return;
97 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
98 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
102 while (s->hnext != NULL &&
103 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
105 if (s->hnext != NULL)
106 s->hnext = s->hnext->hnext;
108 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
109 " from hash table", MAC2STR(sta->addr));
113 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
117 accounting_sta_stop(hapd, sta);
119 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
120 !(sta->flags & WLAN_STA_PREAUTH))
121 hostapd_sta_remove(hapd, sta->addr);
123 ap_sta_hash_del(hapd, sta);
124 ap_sta_list_del(hapd, sta);
127 hapd->sta_aid[sta->aid - 1] = NULL;
130 if (sta->nonerp_set) {
132 hapd->iface->num_sta_non_erp--;
133 if (hapd->iface->num_sta_non_erp == 0)
137 if (sta->no_short_slot_time_set) {
138 sta->no_short_slot_time_set = 0;
139 hapd->iface->num_sta_no_short_slot_time--;
140 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
141 && hapd->iface->num_sta_no_short_slot_time == 0)
145 if (sta->no_short_preamble_set) {
146 sta->no_short_preamble_set = 0;
147 hapd->iface->num_sta_no_short_preamble--;
148 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
149 && hapd->iface->num_sta_no_short_preamble == 0)
153 #ifdef CONFIG_IEEE80211N
154 if (sta->flags & WLAN_STA_HT) {
155 if ((sta->ht_capabilities.data.capabilities_info &
156 HT_CAP_INFO_GREEN_FIELD) == 0)
157 hapd->iface->num_sta_ht_no_gf--;
158 if ((sta->ht_capabilities.data.capabilities_info &
159 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0)
160 hapd->iface->num_sta_ht_20mhz--;
162 hapd->iface->num_sta_no_ht--;
164 if (hostapd_ht_operation_update(hapd->iface) > 0)
166 #endif /* CONFIG_IEEE80211N */
169 ieee802_11_set_beacons(hapd->iface);
171 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
172 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
174 ieee802_1x_free_station(sta);
175 wpa_auth_sta_deinit(sta->wpa_sm);
176 rsn_preauth_free_station(hapd, sta);
177 radius_client_flush_auth(hapd->radius, sta->addr);
179 os_free(sta->last_assoc_req);
180 os_free(sta->challenge);
185 void hostapd_free_stas(struct hostapd_data *hapd)
187 struct sta_info *sta, *prev;
189 sta = hapd->sta_list;
193 if (sta->flags & WLAN_STA_AUTH) {
194 mlme_deauthenticate_indication(
195 hapd, sta, WLAN_REASON_UNSPECIFIED);
198 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
199 MAC2STR(prev->addr));
200 ap_free_sta(hapd, prev);
205 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
207 struct hostapd_data *hapd = eloop_ctx;
208 struct sta_info *sta = timeout_ctx;
209 unsigned long next_time = 0;
211 if (sta->timeout_next == STA_REMOVE) {
212 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
213 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
214 "local deauth request");
215 ap_free_sta(hapd, sta);
219 if ((sta->flags & WLAN_STA_ASSOC) &&
220 (sta->timeout_next == STA_NULLFUNC ||
221 sta->timeout_next == STA_DISASSOC)) {
223 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
225 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
226 if (inactive_sec == -1) {
227 wpa_printf(MSG_DEBUG, "Could not get station info "
228 "from kernel driver for " MACSTR ".",
230 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
231 sta->flags & WLAN_STA_ASSOC) {
232 /* station activity detected; reset timeout state */
233 wpa_printf(MSG_DEBUG, " Station has been active");
234 sta->timeout_next = STA_NULLFUNC;
235 next_time = hapd->conf->ap_max_inactivity -
240 if ((sta->flags & WLAN_STA_ASSOC) &&
241 sta->timeout_next == STA_DISASSOC &&
242 !(sta->flags & WLAN_STA_PENDING_POLL)) {
243 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
244 /* data nullfunc frame poll did not produce TX errors; assume
245 * station ACKed it */
246 sta->timeout_next = STA_NULLFUNC;
247 next_time = hapd->conf->ap_max_inactivity;
251 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
256 if (sta->timeout_next == STA_NULLFUNC &&
257 (sta->flags & WLAN_STA_ASSOC)) {
258 /* send data frame to poll STA and check whether this frame
260 struct ieee80211_hdr hdr;
262 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
263 sta->flags |= WLAN_STA_PENDING_POLL;
265 #ifndef CONFIG_NATIVE_WINDOWS
266 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
267 * it is apparently not retried so TX Exc events are not
269 os_memset(&hdr, 0, sizeof(hdr));
271 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
272 hdr.frame_control |= host_to_le16(BIT(1));
273 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
274 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
275 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
277 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
279 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
280 perror("ap_handle_timer: send");
281 #endif /* CONFIG_NATIVE_WINDOWS */
282 } else if (sta->timeout_next != STA_REMOVE) {
283 int deauth = sta->timeout_next == STA_DEAUTH;
285 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
286 deauth ? "deauthentication" : "disassociation",
290 hostapd_sta_deauth(hapd, sta->addr,
291 WLAN_REASON_PREV_AUTH_NOT_VALID);
293 hostapd_sta_disassoc(
295 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
299 switch (sta->timeout_next) {
301 sta->timeout_next = STA_DISASSOC;
302 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
306 sta->flags &= ~WLAN_STA_ASSOC;
307 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
308 if (!sta->acct_terminate_cause)
309 sta->acct_terminate_cause =
310 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
311 accounting_sta_stop(hapd, sta);
312 ieee802_1x_free_station(sta);
313 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
314 HOSTAPD_LEVEL_INFO, "disassociated due to "
316 sta->timeout_next = STA_DEAUTH;
317 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
319 mlme_disassociate_indication(
320 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
324 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
325 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
327 if (!sta->acct_terminate_cause)
328 sta->acct_terminate_cause =
329 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
330 mlme_deauthenticate_indication(
332 WLAN_REASON_PREV_AUTH_NOT_VALID);
333 ap_free_sta(hapd, sta);
339 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
341 struct hostapd_data *hapd = eloop_ctx;
342 struct sta_info *sta = timeout_ctx;
345 if (!(sta->flags & WLAN_STA_AUTH))
348 mlme_deauthenticate_indication(hapd, sta,
349 WLAN_REASON_PREV_AUTH_NOT_VALID);
350 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
351 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
353 sta->acct_terminate_cause =
354 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
355 os_memcpy(addr, sta->addr, ETH_ALEN);
356 ap_free_sta(hapd, sta);
357 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
361 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
364 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
365 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
366 "seconds", session_timeout);
367 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
368 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
373 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
375 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
379 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
381 struct sta_info *sta;
383 sta = ap_get_sta(hapd, addr);
387 wpa_printf(MSG_DEBUG, " New STA");
388 if (hapd->num_sta >= hapd->conf->max_num_sta) {
389 /* FIX: might try to remove some old STAs first? */
390 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
391 hapd->num_sta, hapd->conf->max_num_sta);
395 sta = os_zalloc(sizeof(struct sta_info));
397 wpa_printf(MSG_ERROR, "malloc failed");
400 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
402 /* initialize STA info data */
403 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
404 ap_handle_timer, hapd, sta);
405 os_memcpy(sta->addr, addr, ETH_ALEN);
406 sta->next = hapd->sta_list;
407 hapd->sta_list = sta;
409 ap_sta_hash_add(hapd, sta);
410 sta->ssid = &hapd->conf->ssid;
416 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
418 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
420 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
422 if (hostapd_sta_remove(hapd, sta->addr) &&
423 sta->flags & WLAN_STA_ASSOC) {
424 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
425 " from kernel driver.", MAC2STR(sta->addr));
432 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
433 struct sta_info *sta, u32 flags)
435 struct hostapd_iface *iface = hapd->iface;
438 for (i = 0; i < iface->num_bss; i++) {
439 struct hostapd_data *bss = iface->bss[i];
440 struct sta_info *sta2;
441 /* bss should always be set during operation, but it may be
442 * NULL during reconfiguration. Assume the STA is not
443 * associated to another BSS in that case to avoid NULL pointer
445 if (bss == hapd || bss == NULL)
447 sta2 = ap_get_sta(bss, sta->addr);
448 if (sta2 && ((sta2->flags & flags) == flags))
456 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
459 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
460 hapd->conf->iface, MAC2STR(sta->addr));
461 sta->flags &= ~WLAN_STA_ASSOC;
462 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
463 ap_sta_remove(hapd, sta);
464 sta->timeout_next = STA_DEAUTH;
465 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
466 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
467 ap_handle_timer, hapd, sta);
468 accounting_sta_stop(hapd, sta);
469 ieee802_1x_free_station(sta);
471 mlme_disassociate_indication(hapd, sta, reason);
475 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
478 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
479 hapd->conf->iface, MAC2STR(sta->addr));
480 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
481 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
482 ap_sta_remove(hapd, sta);
483 sta->timeout_next = STA_REMOVE;
484 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
485 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
486 ap_handle_timer, hapd, sta);
487 accounting_sta_stop(hapd, sta);
488 ieee802_1x_free_station(sta);
490 mlme_deauthenticate_indication(hapd, sta, reason);
494 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
498 struct hostapd_vlan *vlan = NULL;
501 * Do not proceed furthur if the vlan id remains same. We do not want
502 * duplicate dynamic vlan entries.
504 if (sta->vlan_id == old_vlanid)
508 * During 1x reauth, if the vlan id changes, then remove the old id and
509 * proceed furthur to add the new one.
512 vlan_remove_dynamic(hapd, old_vlanid);
514 iface = hapd->conf->iface;
515 if (sta->ssid->vlan[0])
516 iface = sta->ssid->vlan;
518 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
520 else if (sta->vlan_id > 0) {
521 vlan = hapd->conf->vlan;
523 if (vlan->vlan_id == sta->vlan_id ||
524 vlan->vlan_id == VLAN_ID_WILDCARD) {
525 iface = vlan->ifname;
532 if (sta->vlan_id > 0 && vlan == NULL) {
533 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
534 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
535 "binding station to (vlan_id=%d)",
538 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
539 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
541 hostapd_logger(hapd, sta->addr,
542 HOSTAPD_MODULE_IEEE80211,
543 HOSTAPD_LEVEL_DEBUG, "could not add "
544 "dynamic VLAN interface for vlan_id=%d",
549 iface = vlan->ifname;
550 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
551 hostapd_logger(hapd, sta->addr,
552 HOSTAPD_MODULE_IEEE80211,
553 HOSTAPD_LEVEL_DEBUG, "could not "
554 "configure encryption for dynamic VLAN "
555 "interface for vlan_id=%d",
559 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
560 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
561 "interface '%s'", iface);
562 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
563 if (sta->vlan_id > 0) {
564 vlan->dynamic_vlan++;
565 hostapd_logger(hapd, sta->addr,
566 HOSTAPD_MODULE_IEEE80211,
567 HOSTAPD_LEVEL_DEBUG, "updated existing "
568 "dynamic VLAN interface '%s'", iface);
572 * Update encryption configuration for statically generated
573 * VLAN interface. This is only used for static WEP
574 * configuration for the case where hostapd did not yet know
575 * which keys are to be used when the interface was added.
577 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
578 hostapd_logger(hapd, sta->addr,
579 HOSTAPD_MODULE_IEEE80211,
580 HOSTAPD_LEVEL_DEBUG, "could not "
581 "configure encryption for VLAN "
582 "interface for vlan_id=%d",
587 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
588 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
591 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
592 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
594 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);