2 * hostapd / Station table
3 * Copyright (c) 2002-2007, 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);
38 int ap_for_each_sta(struct hostapd_data *hapd,
39 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
45 for (sta = hapd->sta_list; sta; sta = sta->next) {
46 if (cb(hapd, sta, ctx))
54 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
58 s = hapd->sta_hash[STA_HASH(sta)];
59 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
65 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
69 if (hapd->sta_list == sta) {
70 hapd->sta_list = sta->next;
75 while (tmp != NULL && tmp->next != sta)
78 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
79 "list.", MAC2STR(sta->addr));
81 tmp->next = sta->next;
85 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
87 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
88 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
92 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
96 s = hapd->sta_hash[STA_HASH(sta->addr)];
97 if (s == NULL) return;
98 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
99 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
103 while (s->hnext != NULL &&
104 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
106 if (s->hnext != NULL)
107 s->hnext = s->hnext->hnext;
109 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
110 " from hash table", MAC2STR(sta->addr));
114 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
118 accounting_sta_stop(hapd, sta);
120 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC) &&
121 !(sta->flags & WLAN_STA_PREAUTH))
122 hostapd_sta_remove(hapd, sta->addr);
124 ap_sta_hash_del(hapd, sta);
125 ap_sta_list_del(hapd, sta);
128 hapd->sta_aid[sta->aid - 1] = NULL;
131 if (sta->nonerp_set) {
133 hapd->iface->num_sta_non_erp--;
134 if (hapd->iface->num_sta_non_erp == 0)
138 if (sta->no_short_slot_time_set) {
139 sta->no_short_slot_time_set = 0;
140 hapd->iface->num_sta_no_short_slot_time--;
141 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
142 && hapd->iface->num_sta_no_short_slot_time == 0)
146 if (sta->no_short_preamble_set) {
147 sta->no_short_preamble_set = 0;
148 hapd->iface->num_sta_no_short_preamble--;
149 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
150 && hapd->iface->num_sta_no_short_preamble == 0)
154 #ifdef CONFIG_IEEE80211N
155 if (sta->flags & WLAN_STA_HT) {
156 if ((sta->ht_capabilities.data.capabilities_info &
157 HT_CAP_INFO_GREEN_FIELD) == 0)
158 hapd->iface->num_sta_ht_no_gf--;
159 if ((sta->ht_capabilities.data.capabilities_info &
160 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0)
161 hapd->iface->num_sta_ht_20mhz--;
163 hapd->iface->num_sta_no_ht--;
165 if (hostapd_ht_operation_update(hapd->iface) > 0)
167 #endif /* CONFIG_IEEE80211N */
170 ieee802_11_set_beacons(hapd->iface);
172 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
173 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
175 ieee802_1x_free_station(sta);
176 wpa_auth_sta_deinit(sta->wpa_sm);
177 rsn_preauth_free_station(hapd, sta);
178 radius_client_flush_auth(hapd->radius, sta->addr);
180 os_free(sta->last_assoc_req);
181 os_free(sta->challenge);
186 void hostapd_free_stas(struct hostapd_data *hapd)
188 struct sta_info *sta, *prev;
190 sta = hapd->sta_list;
194 if (sta->flags & WLAN_STA_AUTH) {
195 mlme_deauthenticate_indication(
196 hapd, sta, WLAN_REASON_UNSPECIFIED);
199 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
200 MAC2STR(prev->addr));
201 ap_free_sta(hapd, prev);
206 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
208 struct hostapd_data *hapd = eloop_ctx;
209 struct sta_info *sta = timeout_ctx;
210 unsigned long next_time = 0;
212 if (sta->timeout_next == STA_REMOVE) {
213 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
214 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
215 "local deauth request");
216 ap_free_sta(hapd, sta);
220 if ((sta->flags & WLAN_STA_ASSOC) &&
221 (sta->timeout_next == STA_NULLFUNC ||
222 sta->timeout_next == STA_DISASSOC)) {
224 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
226 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
227 if (inactive_sec == -1) {
228 wpa_printf(MSG_DEBUG, "Could not get station info "
229 "from kernel driver for " MACSTR ".",
231 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
232 sta->flags & WLAN_STA_ASSOC) {
233 /* station activity detected; reset timeout state */
234 wpa_printf(MSG_DEBUG, " Station has been active");
235 sta->timeout_next = STA_NULLFUNC;
236 next_time = hapd->conf->ap_max_inactivity -
241 if ((sta->flags & WLAN_STA_ASSOC) &&
242 sta->timeout_next == STA_DISASSOC &&
243 !(sta->flags & WLAN_STA_PENDING_POLL)) {
244 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
245 /* data nullfunc frame poll did not produce TX errors; assume
246 * station ACKed it */
247 sta->timeout_next = STA_NULLFUNC;
248 next_time = hapd->conf->ap_max_inactivity;
252 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
257 if (sta->timeout_next == STA_NULLFUNC &&
258 (sta->flags & WLAN_STA_ASSOC)) {
259 /* send data frame to poll STA and check whether this frame
261 struct ieee80211_hdr hdr;
263 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
264 sta->flags |= WLAN_STA_PENDING_POLL;
266 #ifndef CONFIG_NATIVE_WINDOWS
267 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
268 * it is apparently not retried so TX Exc events are not
270 os_memset(&hdr, 0, sizeof(hdr));
272 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
273 hdr.frame_control |= host_to_le16(BIT(1));
274 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
275 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
276 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
278 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
280 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
281 perror("ap_handle_timer: send");
282 #endif /* CONFIG_NATIVE_WINDOWS */
283 } else if (sta->timeout_next != STA_REMOVE) {
284 int deauth = sta->timeout_next == STA_DEAUTH;
286 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
287 deauth ? "deauthentication" : "disassociation",
291 hostapd_sta_deauth(hapd, sta->addr,
292 WLAN_REASON_PREV_AUTH_NOT_VALID);
294 hostapd_sta_disassoc(
296 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
300 switch (sta->timeout_next) {
302 sta->timeout_next = STA_DISASSOC;
303 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
307 sta->flags &= ~WLAN_STA_ASSOC;
308 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
309 if (!sta->acct_terminate_cause)
310 sta->acct_terminate_cause =
311 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
312 accounting_sta_stop(hapd, sta);
313 ieee802_1x_free_station(sta);
314 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
315 HOSTAPD_LEVEL_INFO, "disassociated due to "
317 sta->timeout_next = STA_DEAUTH;
318 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
320 mlme_disassociate_indication(
321 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
325 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
326 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
328 if (!sta->acct_terminate_cause)
329 sta->acct_terminate_cause =
330 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
331 mlme_deauthenticate_indication(
333 WLAN_REASON_PREV_AUTH_NOT_VALID);
334 ap_free_sta(hapd, sta);
340 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
342 struct hostapd_data *hapd = eloop_ctx;
343 struct sta_info *sta = timeout_ctx;
346 if (!(sta->flags & WLAN_STA_AUTH))
349 mlme_deauthenticate_indication(hapd, sta,
350 WLAN_REASON_PREV_AUTH_NOT_VALID);
351 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
352 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
354 sta->acct_terminate_cause =
355 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
356 os_memcpy(addr, sta->addr, ETH_ALEN);
357 ap_free_sta(hapd, sta);
358 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
362 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
365 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
366 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
367 "seconds", session_timeout);
368 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
369 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
374 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
376 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
380 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
382 struct sta_info *sta;
384 sta = ap_get_sta(hapd, addr);
388 wpa_printf(MSG_DEBUG, " New STA");
389 if (hapd->num_sta >= hapd->conf->max_num_sta) {
390 /* FIX: might try to remove some old STAs first? */
391 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
392 hapd->num_sta, hapd->conf->max_num_sta);
396 sta = os_zalloc(sizeof(struct sta_info));
398 wpa_printf(MSG_ERROR, "malloc failed");
401 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
403 /* initialize STA info data */
404 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
405 ap_handle_timer, hapd, sta);
406 os_memcpy(sta->addr, addr, ETH_ALEN);
407 sta->next = hapd->sta_list;
408 hapd->sta_list = sta;
410 ap_sta_hash_add(hapd, sta);
411 sta->ssid = &hapd->conf->ssid;
417 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
419 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
421 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
423 if (hostapd_sta_remove(hapd, sta->addr) &&
424 sta->flags & WLAN_STA_ASSOC) {
425 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
426 " from kernel driver.", MAC2STR(sta->addr));
433 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
434 struct sta_info *sta, u32 flags)
436 struct hostapd_iface *iface = hapd->iface;
439 for (i = 0; i < iface->num_bss; i++) {
440 struct hostapd_data *bss = iface->bss[i];
441 struct sta_info *sta2;
442 /* bss should always be set during operation, but it may be
443 * NULL during reconfiguration. Assume the STA is not
444 * associated to another BSS in that case to avoid NULL pointer
446 if (bss == hapd || bss == NULL)
448 sta2 = ap_get_sta(bss, sta->addr);
449 if (sta2 && ((sta2->flags & flags) == flags))
457 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
460 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
461 hapd->conf->iface, MAC2STR(sta->addr));
462 sta->flags &= ~WLAN_STA_ASSOC;
463 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
464 ap_sta_remove(hapd, sta);
465 sta->timeout_next = STA_DEAUTH;
466 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
467 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
468 ap_handle_timer, hapd, sta);
469 accounting_sta_stop(hapd, sta);
470 ieee802_1x_free_station(sta);
472 mlme_disassociate_indication(hapd, sta, reason);
476 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
479 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
480 hapd->conf->iface, MAC2STR(sta->addr));
481 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
482 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
483 ap_sta_remove(hapd, sta);
484 sta->timeout_next = STA_REMOVE;
485 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
486 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
487 ap_handle_timer, hapd, sta);
488 accounting_sta_stop(hapd, sta);
489 ieee802_1x_free_station(sta);
491 mlme_deauthenticate_indication(hapd, sta, reason);
495 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
499 struct hostapd_vlan *vlan = NULL;
502 * Do not proceed furthur if the vlan id remains same. We do not want
503 * duplicate dynamic vlan entries.
505 if (sta->vlan_id == old_vlanid)
509 * During 1x reauth, if the vlan id changes, then remove the old id and
510 * proceed furthur to add the new one.
513 vlan_remove_dynamic(hapd, old_vlanid);
515 iface = hapd->conf->iface;
516 if (sta->ssid->vlan[0])
517 iface = sta->ssid->vlan;
519 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
521 else if (sta->vlan_id > 0) {
522 vlan = hapd->conf->vlan;
524 if (vlan->vlan_id == sta->vlan_id ||
525 vlan->vlan_id == VLAN_ID_WILDCARD) {
526 iface = vlan->ifname;
533 if (sta->vlan_id > 0 && vlan == NULL) {
534 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
535 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
536 "binding station to (vlan_id=%d)",
539 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
540 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
542 hostapd_logger(hapd, sta->addr,
543 HOSTAPD_MODULE_IEEE80211,
544 HOSTAPD_LEVEL_DEBUG, "could not add "
545 "dynamic VLAN interface for vlan_id=%d",
550 iface = vlan->ifname;
551 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
552 hostapd_logger(hapd, sta->addr,
553 HOSTAPD_MODULE_IEEE80211,
554 HOSTAPD_LEVEL_DEBUG, "could not "
555 "configure encryption for dynamic VLAN "
556 "interface for vlan_id=%d",
560 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
561 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
562 "interface '%s'", iface);
563 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
564 if (sta->vlan_id > 0) {
565 vlan->dynamic_vlan++;
566 hostapd_logger(hapd, sta->addr,
567 HOSTAPD_MODULE_IEEE80211,
568 HOSTAPD_LEVEL_DEBUG, "updated existing "
569 "dynamic VLAN interface '%s'", iface);
573 * Update encryption configuration for statically generated
574 * VLAN interface. This is only used for static WEP
575 * configuration for the case where hostapd did not yet know
576 * which keys are to be used when the interface was added.
578 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
579 hostapd_logger(hapd, sta->addr,
580 HOSTAPD_MODULE_IEEE80211,
581 HOSTAPD_LEVEL_DEBUG, "could not "
582 "configure encryption for VLAN "
583 "interface for vlan_id=%d",
588 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
589 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
592 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
593 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
595 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);