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] = NULL;
134 if (sta->nonerp_set) {
136 hapd->iface->num_sta_non_erp--;
137 if (hapd->iface->num_sta_non_erp == 0)
141 if (sta->no_short_slot_time_set) {
142 sta->no_short_slot_time_set = 0;
143 hapd->iface->num_sta_no_short_slot_time--;
144 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
145 && hapd->iface->num_sta_no_short_slot_time == 0)
149 if (sta->no_short_preamble_set) {
150 sta->no_short_preamble_set = 0;
151 hapd->iface->num_sta_no_short_preamble--;
152 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
153 && hapd->iface->num_sta_no_short_preamble == 0)
157 #ifdef CONFIG_IEEE80211N
158 if (sta->no_ht_gf_set) {
159 sta->no_ht_gf_set = 0;
160 hapd->iface->num_sta_ht_no_gf--;
163 if (sta->no_ht_set) {
165 hapd->iface->num_sta_no_ht--;
168 if (sta->ht_20mhz_set) {
169 sta->ht_20mhz_set = 0;
170 hapd->iface->num_sta_ht_20mhz--;
173 if (hostapd_ht_operation_update(hapd->iface) > 0)
175 #endif /* CONFIG_IEEE80211N */
178 ieee802_11_set_beacons(hapd->iface);
180 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
181 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
183 ieee802_1x_free_station(sta);
184 wpa_auth_sta_deinit(sta->wpa_sm);
185 rsn_preauth_free_station(hapd, sta);
186 radius_client_flush_auth(hapd->radius, sta->addr);
188 os_free(sta->last_assoc_req);
189 os_free(sta->challenge);
191 #ifdef CONFIG_IEEE80211W
192 os_free(sta->sa_query_trans_id);
193 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
194 #endif /* CONFIG_IEEE80211W */
196 wpabuf_free(sta->wps_ie);
202 void hostapd_free_stas(struct hostapd_data *hapd)
204 struct sta_info *sta, *prev;
206 sta = hapd->sta_list;
210 if (sta->flags & WLAN_STA_AUTH) {
211 mlme_deauthenticate_indication(
212 hapd, sta, WLAN_REASON_UNSPECIFIED);
215 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
216 MAC2STR(prev->addr));
217 ap_free_sta(hapd, prev);
222 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
224 struct hostapd_data *hapd = eloop_ctx;
225 struct sta_info *sta = timeout_ctx;
226 unsigned long next_time = 0;
228 if (sta->timeout_next == STA_REMOVE) {
229 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
230 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
231 "local deauth request");
232 ap_free_sta(hapd, sta);
236 if ((sta->flags & WLAN_STA_ASSOC) &&
237 (sta->timeout_next == STA_NULLFUNC ||
238 sta->timeout_next == STA_DISASSOC)) {
240 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
242 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
243 if (inactive_sec == -1) {
244 wpa_printf(MSG_DEBUG, "Could not get station info "
245 "from kernel driver for " MACSTR ".",
247 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
248 sta->flags & WLAN_STA_ASSOC) {
249 /* station activity detected; reset timeout state */
250 wpa_printf(MSG_DEBUG, " Station has been active");
251 sta->timeout_next = STA_NULLFUNC;
252 next_time = hapd->conf->ap_max_inactivity -
257 if ((sta->flags & WLAN_STA_ASSOC) &&
258 sta->timeout_next == STA_DISASSOC &&
259 !(sta->flags & WLAN_STA_PENDING_POLL)) {
260 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
261 /* data nullfunc frame poll did not produce TX errors; assume
262 * station ACKed it */
263 sta->timeout_next = STA_NULLFUNC;
264 next_time = hapd->conf->ap_max_inactivity;
268 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
273 if (sta->timeout_next == STA_NULLFUNC &&
274 (sta->flags & WLAN_STA_ASSOC)) {
275 /* send data frame to poll STA and check whether this frame
277 struct ieee80211_hdr hdr;
279 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
280 sta->flags |= WLAN_STA_PENDING_POLL;
282 #ifndef CONFIG_NATIVE_WINDOWS
283 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
284 * it is apparently not retried so TX Exc events are not
286 os_memset(&hdr, 0, sizeof(hdr));
288 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
289 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
290 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
291 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
293 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
295 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
296 perror("ap_handle_timer: send");
297 #endif /* CONFIG_NATIVE_WINDOWS */
298 } else if (sta->timeout_next != STA_REMOVE) {
299 int deauth = sta->timeout_next == STA_DEAUTH;
301 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
302 deauth ? "deauthentication" : "disassociation",
306 hostapd_sta_deauth(hapd, sta->addr,
307 WLAN_REASON_PREV_AUTH_NOT_VALID);
309 hostapd_sta_disassoc(
311 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
315 switch (sta->timeout_next) {
317 sta->timeout_next = STA_DISASSOC;
318 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
322 sta->flags &= ~WLAN_STA_ASSOC;
323 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
324 if (!sta->acct_terminate_cause)
325 sta->acct_terminate_cause =
326 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
327 accounting_sta_stop(hapd, sta);
328 ieee802_1x_free_station(sta);
329 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
330 HOSTAPD_LEVEL_INFO, "disassociated due to "
332 sta->timeout_next = STA_DEAUTH;
333 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
335 mlme_disassociate_indication(
336 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
340 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
341 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
343 if (!sta->acct_terminate_cause)
344 sta->acct_terminate_cause =
345 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
346 mlme_deauthenticate_indication(
348 WLAN_REASON_PREV_AUTH_NOT_VALID);
349 ap_free_sta(hapd, sta);
355 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
357 struct hostapd_data *hapd = eloop_ctx;
358 struct sta_info *sta = timeout_ctx;
361 if (!(sta->flags & WLAN_STA_AUTH))
364 mlme_deauthenticate_indication(hapd, sta,
365 WLAN_REASON_PREV_AUTH_NOT_VALID);
366 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
367 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
369 sta->acct_terminate_cause =
370 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
371 os_memcpy(addr, sta->addr, ETH_ALEN);
372 ap_free_sta(hapd, sta);
373 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
377 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
380 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
381 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
382 "seconds", session_timeout);
383 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
384 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
389 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
391 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
395 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
397 struct sta_info *sta;
399 sta = ap_get_sta(hapd, addr);
403 wpa_printf(MSG_DEBUG, " New STA");
404 if (hapd->num_sta >= hapd->conf->max_num_sta) {
405 /* FIX: might try to remove some old STAs first? */
406 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
407 hapd->num_sta, hapd->conf->max_num_sta);
411 sta = os_zalloc(sizeof(struct sta_info));
413 wpa_printf(MSG_ERROR, "malloc failed");
416 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
418 /* initialize STA info data */
419 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
420 ap_handle_timer, hapd, sta);
421 os_memcpy(sta->addr, addr, ETH_ALEN);
422 sta->next = hapd->sta_list;
423 hapd->sta_list = sta;
425 ap_sta_hash_add(hapd, sta);
426 sta->ssid = &hapd->conf->ssid;
432 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
434 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
436 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
438 if (hostapd_sta_remove(hapd, sta->addr) &&
439 sta->flags & WLAN_STA_ASSOC) {
440 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
441 " from kernel driver.", MAC2STR(sta->addr));
448 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
449 struct sta_info *sta, u32 flags)
451 struct hostapd_iface *iface = hapd->iface;
454 for (i = 0; i < iface->num_bss; i++) {
455 struct hostapd_data *bss = iface->bss[i];
456 struct sta_info *sta2;
457 /* bss should always be set during operation, but it may be
458 * NULL during reconfiguration. Assume the STA is not
459 * associated to another BSS in that case to avoid NULL pointer
461 if (bss == hapd || bss == NULL)
463 sta2 = ap_get_sta(bss, sta->addr);
464 if (sta2 && ((sta2->flags & flags) == flags))
472 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
475 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
476 hapd->conf->iface, MAC2STR(sta->addr));
477 sta->flags &= ~WLAN_STA_ASSOC;
478 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
479 ap_sta_remove(hapd, sta);
480 sta->timeout_next = STA_DEAUTH;
481 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
482 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
483 ap_handle_timer, hapd, sta);
484 accounting_sta_stop(hapd, sta);
485 ieee802_1x_free_station(sta);
487 mlme_disassociate_indication(hapd, sta, reason);
491 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
494 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
495 hapd->conf->iface, MAC2STR(sta->addr));
496 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
497 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
498 ap_sta_remove(hapd, sta);
499 sta->timeout_next = STA_REMOVE;
500 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
501 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
502 ap_handle_timer, hapd, sta);
503 accounting_sta_stop(hapd, sta);
504 ieee802_1x_free_station(sta);
506 mlme_deauthenticate_indication(hapd, sta, reason);
510 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
514 struct hostapd_vlan *vlan = NULL;
517 * Do not proceed furthur if the vlan id remains same. We do not want
518 * duplicate dynamic vlan entries.
520 if (sta->vlan_id == old_vlanid)
524 * During 1x reauth, if the vlan id changes, then remove the old id and
525 * proceed furthur to add the new one.
528 vlan_remove_dynamic(hapd, old_vlanid);
530 iface = hapd->conf->iface;
531 if (sta->ssid->vlan[0])
532 iface = sta->ssid->vlan;
534 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
536 else if (sta->vlan_id > 0) {
537 vlan = hapd->conf->vlan;
539 if (vlan->vlan_id == sta->vlan_id ||
540 vlan->vlan_id == VLAN_ID_WILDCARD) {
541 iface = vlan->ifname;
548 if (sta->vlan_id > 0 && vlan == NULL) {
549 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
550 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
551 "binding station to (vlan_id=%d)",
554 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
555 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
557 hostapd_logger(hapd, sta->addr,
558 HOSTAPD_MODULE_IEEE80211,
559 HOSTAPD_LEVEL_DEBUG, "could not add "
560 "dynamic VLAN interface for vlan_id=%d",
565 iface = vlan->ifname;
566 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
567 hostapd_logger(hapd, sta->addr,
568 HOSTAPD_MODULE_IEEE80211,
569 HOSTAPD_LEVEL_DEBUG, "could not "
570 "configure encryption for dynamic VLAN "
571 "interface for vlan_id=%d",
575 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
576 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
577 "interface '%s'", iface);
578 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
579 if (sta->vlan_id > 0) {
580 vlan->dynamic_vlan++;
581 hostapd_logger(hapd, sta->addr,
582 HOSTAPD_MODULE_IEEE80211,
583 HOSTAPD_LEVEL_DEBUG, "updated existing "
584 "dynamic VLAN interface '%s'", iface);
588 * Update encryption configuration for statically generated
589 * VLAN interface. This is only used for static WEP
590 * configuration for the case where hostapd did not yet know
591 * which keys are to be used when the interface was added.
593 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
594 hostapd_logger(hapd, sta->addr,
595 HOSTAPD_MODULE_IEEE80211,
596 HOSTAPD_LEVEL_DEBUG, "could not "
597 "configure encryption for VLAN "
598 "interface for vlan_id=%d",
603 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
604 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
607 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
608 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
610 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
614 #ifdef CONFIG_IEEE80211W
616 /* MLME-SAQuery.request */
617 static void ieee802_11_send_sa_query_req(struct hostapd_data *hapd,
618 const u8 *addr, const u8 *trans_id)
620 struct ieee80211_mgmt mgmt;
623 os_memset(&mgmt, 0, sizeof(mgmt));
624 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
625 WLAN_FC_STYPE_ACTION);
626 os_memcpy(mgmt.da, addr, ETH_ALEN);
627 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
628 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
629 mgmt.u.action.category = WLAN_ACTION_SA_QUERY;
630 mgmt.u.action.u.sa_query_req.action = WLAN_SA_QUERY_REQUEST;
631 os_memcpy(mgmt.u.action.u.sa_query_req.trans_id, trans_id,
632 WLAN_SA_QUERY_TR_ID_LEN);
633 end = mgmt.u.action.u.sa_query_req.trans_id + WLAN_SA_QUERY_TR_ID_LEN;
634 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
635 end - (u8 *) &mgmt, 0) < 0)
636 perror("ieee802_11_send_sa_query_req: send");
640 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
642 struct hostapd_data *hapd = eloop_ctx;
643 struct sta_info *sta = timeout_ctx;
644 unsigned int timeout, sec, usec;
647 if (sta->sa_query_count >= hapd->conf->assoc_ping_attempts) {
648 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
650 "association SA Query timed out");
651 sta->sa_query_timed_out = 1;
652 os_free(sta->sa_query_trans_id);
653 sta->sa_query_trans_id = NULL;
654 sta->sa_query_count = 0;
658 nbuf = os_realloc(sta->sa_query_trans_id,
659 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
662 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
663 sta->sa_query_trans_id = nbuf;
664 sta->sa_query_count++;
666 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
668 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
670 "association SA Query attempt %d", sta->sa_query_count);
672 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
674 timeout = hapd->conf->assoc_ping_timeout;
675 sec = ((timeout / 1000) * 1024) / 1000;
676 usec = (timeout % 1000) * 1024;
677 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
681 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
683 ap_sa_query_timer(hapd, sta);
687 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
689 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
690 os_free(sta->sa_query_trans_id);
691 sta->sa_query_trans_id = NULL;
692 sta->sa_query_count = 0;
695 #endif /* CONFIG_IEEE80211W */