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_ping_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->flags & WLAN_STA_HT) {
159 if ((sta->ht_capabilities.data.capabilities_info &
160 HT_CAP_INFO_GREEN_FIELD) == 0)
161 hapd->iface->num_sta_ht_no_gf--;
162 if ((sta->ht_capabilities.data.capabilities_info &
163 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET) == 0)
164 hapd->iface->num_sta_ht_20mhz--;
166 hapd->iface->num_sta_no_ht--;
168 if (hostapd_ht_operation_update(hapd->iface) > 0)
170 #endif /* CONFIG_IEEE80211N */
173 ieee802_11_set_beacons(hapd->iface);
175 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
176 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
178 ieee802_1x_free_station(sta);
179 wpa_auth_sta_deinit(sta->wpa_sm);
180 rsn_preauth_free_station(hapd, sta);
181 radius_client_flush_auth(hapd->radius, sta->addr);
183 os_free(sta->last_assoc_req);
184 os_free(sta->challenge);
186 #ifdef CONFIG_IEEE80211W
187 os_free(sta->ping_trans_id);
188 eloop_cancel_timeout(ap_ping_timer, hapd, sta);
189 #endif /* CONFIG_IEEE80211W */
195 void hostapd_free_stas(struct hostapd_data *hapd)
197 struct sta_info *sta, *prev;
199 sta = hapd->sta_list;
203 if (sta->flags & WLAN_STA_AUTH) {
204 mlme_deauthenticate_indication(
205 hapd, sta, WLAN_REASON_UNSPECIFIED);
208 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
209 MAC2STR(prev->addr));
210 ap_free_sta(hapd, prev);
215 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
217 struct hostapd_data *hapd = eloop_ctx;
218 struct sta_info *sta = timeout_ctx;
219 unsigned long next_time = 0;
221 if (sta->timeout_next == STA_REMOVE) {
222 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
223 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
224 "local deauth request");
225 ap_free_sta(hapd, sta);
229 if ((sta->flags & WLAN_STA_ASSOC) &&
230 (sta->timeout_next == STA_NULLFUNC ||
231 sta->timeout_next == STA_DISASSOC)) {
233 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
235 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
236 if (inactive_sec == -1) {
237 wpa_printf(MSG_DEBUG, "Could not get station info "
238 "from kernel driver for " MACSTR ".",
240 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
241 sta->flags & WLAN_STA_ASSOC) {
242 /* station activity detected; reset timeout state */
243 wpa_printf(MSG_DEBUG, " Station has been active");
244 sta->timeout_next = STA_NULLFUNC;
245 next_time = hapd->conf->ap_max_inactivity -
250 if ((sta->flags & WLAN_STA_ASSOC) &&
251 sta->timeout_next == STA_DISASSOC &&
252 !(sta->flags & WLAN_STA_PENDING_POLL)) {
253 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
254 /* data nullfunc frame poll did not produce TX errors; assume
255 * station ACKed it */
256 sta->timeout_next = STA_NULLFUNC;
257 next_time = hapd->conf->ap_max_inactivity;
261 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
266 if (sta->timeout_next == STA_NULLFUNC &&
267 (sta->flags & WLAN_STA_ASSOC)) {
268 /* send data frame to poll STA and check whether this frame
270 struct ieee80211_hdr hdr;
272 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
273 sta->flags |= WLAN_STA_PENDING_POLL;
275 #ifndef CONFIG_NATIVE_WINDOWS
276 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
277 * it is apparently not retried so TX Exc events are not
279 os_memset(&hdr, 0, sizeof(hdr));
281 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
282 hdr.frame_control |= host_to_le16(BIT(1));
283 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
284 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
285 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
287 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
289 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
290 perror("ap_handle_timer: send");
291 #endif /* CONFIG_NATIVE_WINDOWS */
292 } else if (sta->timeout_next != STA_REMOVE) {
293 int deauth = sta->timeout_next == STA_DEAUTH;
295 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
296 deauth ? "deauthentication" : "disassociation",
300 hostapd_sta_deauth(hapd, sta->addr,
301 WLAN_REASON_PREV_AUTH_NOT_VALID);
303 hostapd_sta_disassoc(
305 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
309 switch (sta->timeout_next) {
311 sta->timeout_next = STA_DISASSOC;
312 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
316 sta->flags &= ~WLAN_STA_ASSOC;
317 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
318 if (!sta->acct_terminate_cause)
319 sta->acct_terminate_cause =
320 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
321 accounting_sta_stop(hapd, sta);
322 ieee802_1x_free_station(sta);
323 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
324 HOSTAPD_LEVEL_INFO, "disassociated due to "
326 sta->timeout_next = STA_DEAUTH;
327 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
329 mlme_disassociate_indication(
330 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
334 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
335 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
337 if (!sta->acct_terminate_cause)
338 sta->acct_terminate_cause =
339 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
340 mlme_deauthenticate_indication(
342 WLAN_REASON_PREV_AUTH_NOT_VALID);
343 ap_free_sta(hapd, sta);
349 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
351 struct hostapd_data *hapd = eloop_ctx;
352 struct sta_info *sta = timeout_ctx;
355 if (!(sta->flags & WLAN_STA_AUTH))
358 mlme_deauthenticate_indication(hapd, sta,
359 WLAN_REASON_PREV_AUTH_NOT_VALID);
360 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
361 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
363 sta->acct_terminate_cause =
364 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
365 os_memcpy(addr, sta->addr, ETH_ALEN);
366 ap_free_sta(hapd, sta);
367 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
371 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
374 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
375 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
376 "seconds", session_timeout);
377 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
378 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
383 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
385 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
389 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
391 struct sta_info *sta;
393 sta = ap_get_sta(hapd, addr);
397 wpa_printf(MSG_DEBUG, " New STA");
398 if (hapd->num_sta >= hapd->conf->max_num_sta) {
399 /* FIX: might try to remove some old STAs first? */
400 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
401 hapd->num_sta, hapd->conf->max_num_sta);
405 sta = os_zalloc(sizeof(struct sta_info));
407 wpa_printf(MSG_ERROR, "malloc failed");
410 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
412 /* initialize STA info data */
413 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
414 ap_handle_timer, hapd, sta);
415 os_memcpy(sta->addr, addr, ETH_ALEN);
416 sta->next = hapd->sta_list;
417 hapd->sta_list = sta;
419 ap_sta_hash_add(hapd, sta);
420 sta->ssid = &hapd->conf->ssid;
426 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
428 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
430 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
432 if (hostapd_sta_remove(hapd, sta->addr) &&
433 sta->flags & WLAN_STA_ASSOC) {
434 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
435 " from kernel driver.", MAC2STR(sta->addr));
442 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
443 struct sta_info *sta, u32 flags)
445 struct hostapd_iface *iface = hapd->iface;
448 for (i = 0; i < iface->num_bss; i++) {
449 struct hostapd_data *bss = iface->bss[i];
450 struct sta_info *sta2;
451 /* bss should always be set during operation, but it may be
452 * NULL during reconfiguration. Assume the STA is not
453 * associated to another BSS in that case to avoid NULL pointer
455 if (bss == hapd || bss == NULL)
457 sta2 = ap_get_sta(bss, sta->addr);
458 if (sta2 && ((sta2->flags & flags) == flags))
466 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
469 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
470 hapd->conf->iface, MAC2STR(sta->addr));
471 sta->flags &= ~WLAN_STA_ASSOC;
472 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
473 ap_sta_remove(hapd, sta);
474 sta->timeout_next = STA_DEAUTH;
475 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
476 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
477 ap_handle_timer, hapd, sta);
478 accounting_sta_stop(hapd, sta);
479 ieee802_1x_free_station(sta);
481 mlme_disassociate_indication(hapd, sta, reason);
485 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
488 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
489 hapd->conf->iface, MAC2STR(sta->addr));
490 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
491 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
492 ap_sta_remove(hapd, sta);
493 sta->timeout_next = STA_REMOVE;
494 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
495 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
496 ap_handle_timer, hapd, sta);
497 accounting_sta_stop(hapd, sta);
498 ieee802_1x_free_station(sta);
500 mlme_deauthenticate_indication(hapd, sta, reason);
504 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
508 struct hostapd_vlan *vlan = NULL;
511 * Do not proceed furthur if the vlan id remains same. We do not want
512 * duplicate dynamic vlan entries.
514 if (sta->vlan_id == old_vlanid)
518 * During 1x reauth, if the vlan id changes, then remove the old id and
519 * proceed furthur to add the new one.
522 vlan_remove_dynamic(hapd, old_vlanid);
524 iface = hapd->conf->iface;
525 if (sta->ssid->vlan[0])
526 iface = sta->ssid->vlan;
528 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
530 else if (sta->vlan_id > 0) {
531 vlan = hapd->conf->vlan;
533 if (vlan->vlan_id == sta->vlan_id ||
534 vlan->vlan_id == VLAN_ID_WILDCARD) {
535 iface = vlan->ifname;
542 if (sta->vlan_id > 0 && vlan == NULL) {
543 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
544 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
545 "binding station to (vlan_id=%d)",
548 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
549 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
551 hostapd_logger(hapd, sta->addr,
552 HOSTAPD_MODULE_IEEE80211,
553 HOSTAPD_LEVEL_DEBUG, "could not add "
554 "dynamic VLAN interface for vlan_id=%d",
559 iface = vlan->ifname;
560 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
561 hostapd_logger(hapd, sta->addr,
562 HOSTAPD_MODULE_IEEE80211,
563 HOSTAPD_LEVEL_DEBUG, "could not "
564 "configure encryption for dynamic VLAN "
565 "interface for vlan_id=%d",
569 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
570 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
571 "interface '%s'", iface);
572 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
573 if (sta->vlan_id > 0) {
574 vlan->dynamic_vlan++;
575 hostapd_logger(hapd, sta->addr,
576 HOSTAPD_MODULE_IEEE80211,
577 HOSTAPD_LEVEL_DEBUG, "updated existing "
578 "dynamic VLAN interface '%s'", iface);
582 * Update encryption configuration for statically generated
583 * VLAN interface. This is only used for static WEP
584 * configuration for the case where hostapd did not yet know
585 * which keys are to be used when the interface was added.
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 VLAN "
592 "interface for vlan_id=%d",
597 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
598 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
601 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
602 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
604 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
608 #ifdef CONFIG_IEEE80211W
610 /* MLME-PING.request */
611 static void ieee802_11_send_ping_req(struct hostapd_data *hapd, const u8 *addr,
614 struct ieee80211_mgmt mgmt;
617 os_memset(&mgmt, 0, sizeof(mgmt));
618 mgmt.frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
619 WLAN_FC_STYPE_ACTION);
620 os_memcpy(mgmt.da, addr, ETH_ALEN);
621 os_memcpy(mgmt.sa, hapd->own_addr, ETH_ALEN);
622 os_memcpy(mgmt.bssid, hapd->own_addr, ETH_ALEN);
623 mgmt.u.action.category = WLAN_ACTION_PING;
624 mgmt.u.action.u.ping_req.action = WLAN_PING_REQUEST;
625 os_memcpy(mgmt.u.action.u.ping_req.trans_id, trans_id,
626 WLAN_PING_TRANS_ID_LEN);
627 end = mgmt.u.action.u.ping_req.trans_id + WLAN_PING_TRANS_ID_LEN;
628 if (hostapd_send_mgmt_frame(hapd, &mgmt, IEEE80211_HDRLEN +
629 end - (u8 *) &mgmt, 0) < 0)
630 perror("ieee802_11_send_ping_req: send");
634 static void ap_ping_timer(void *eloop_ctx, void *timeout_ctx)
636 struct hostapd_data *hapd = eloop_ctx;
637 struct sta_info *sta = timeout_ctx;
638 unsigned int timeout, sec, usec;
641 if (sta->ping_count >= hapd->conf->assoc_ping_attempts) {
642 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
644 "association ping timed out");
645 sta->ping_timed_out = 1;
646 os_free(sta->ping_trans_id);
647 sta->ping_trans_id = NULL;
652 nbuf = os_realloc(sta->ping_trans_id,
653 (sta->ping_count + 1) * WLAN_PING_TRANS_ID_LEN);
656 trans_id = nbuf + sta->ping_count * WLAN_PING_TRANS_ID_LEN;
657 sta->ping_trans_id = nbuf;
660 os_get_random(trans_id, WLAN_PING_TRANS_ID_LEN);
662 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
664 "association ping attempt %d", sta->ping_count);
666 ieee802_11_send_ping_req(hapd, sta->addr, trans_id);
668 timeout = hapd->conf->assoc_ping_timeout;
669 sec = ((timeout / 1000) * 1024) / 1000;
670 usec = (timeout % 1000) * 1024;
671 eloop_register_timeout(sec, usec, ap_ping_timer, hapd, sta);
675 void ap_sta_start_ping(struct hostapd_data *hapd, struct sta_info *sta)
677 ap_ping_timer(hapd, sta);
681 void ap_sta_stop_ping(struct hostapd_data *hapd, struct sta_info *sta)
683 eloop_cancel_timeout(ap_ping_timer, hapd, sta);
684 os_free(sta->ping_trans_id);
685 sta->ping_trans_id = NULL;
689 #endif /* CONFIG_IEEE80211W */