HAMMER 60I/Many: Mirroring
[dragonfly.git] / contrib / hostapd-0.5.8 / sta_info.c
blobdbb7f6cef7198ddb621e6ec126b7c001f2f1e346
1 /*
2 * hostapd / Station table
3 * Copyright (c) 2002-2006, 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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "hostapd.h"
18 #include "sta_info.h"
19 #include "eloop.h"
20 #include "accounting.h"
21 #include "ieee802_1x.h"
22 #include "ieee802_11.h"
23 #include "radius.h"
24 #include "eapol_sm.h"
25 #include "wpa.h"
26 #include "preauth.h"
27 #include "radius_client.h"
28 #include "driver.h"
29 #include "beacon.h"
30 #include "hw_features.h"
31 #include "mlme.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,
40 void *ctx),
41 void *ctx)
43 struct sta_info *sta;
45 for (sta = hapd->sta_list; sta; sta = sta->next) {
46 if (cb(hapd, sta, ctx))
47 return 1;
50 return 0;
54 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
56 struct sta_info *s;
58 s = hapd->sta_hash[STA_HASH(sta)];
59 while (s != NULL && memcmp(s->addr, sta, 6) != 0)
60 s = s->hnext;
61 return s;
65 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
67 struct sta_info *tmp;
69 if (hapd->sta_list == sta) {
70 hapd->sta_list = sta->next;
71 return;
74 tmp = hapd->sta_list;
75 while (tmp != NULL && tmp->next != sta)
76 tmp = tmp->next;
77 if (tmp == NULL) {
78 printf("Could not remove STA " MACSTR " from list.\n",
79 MAC2STR(sta->addr));
80 } else
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)
94 struct sta_info *s;
96 s = hapd->sta_hash[STA_HASH(sta->addr)];
97 if (s == NULL) return;
98 if (memcmp(s->addr, sta->addr, 6) == 0) {
99 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
100 return;
103 while (s->hnext != NULL && memcmp(s->hnext->addr, sta->addr, 6) != 0)
104 s = s->hnext;
105 if (s->hnext != NULL)
106 s->hnext = s->hnext->hnext;
107 else
108 printf("AP: could not remove STA " MACSTR " from hash table\n",
109 MAC2STR(sta->addr));
113 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
115 int set_beacon = 0;
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);
126 if (sta->aid > 0)
127 hapd->sta_aid[sta->aid - 1] = NULL;
129 hapd->num_sta--;
130 if (sta->nonerp_set) {
131 sta->nonerp_set = 0;
132 hapd->iface->num_sta_non_erp--;
133 if (hapd->iface->num_sta_non_erp == 0)
134 set_beacon++;
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)
142 set_beacon++;
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)
150 set_beacon++;
153 if (set_beacon)
154 ieee802_11_set_beacons(hapd->iface);
156 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
157 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
159 ieee802_1x_free_station(sta);
160 wpa_auth_sta_deinit(sta->wpa_sm);
161 rsn_preauth_free_station(hapd, sta);
162 radius_client_flush_auth(hapd->radius, sta->addr);
164 if (sta->last_assoc_req)
165 free(sta->last_assoc_req);
167 free(sta->challenge);
169 free(sta);
173 void hostapd_free_stas(struct hostapd_data *hapd)
175 struct sta_info *sta, *prev;
177 sta = hapd->sta_list;
179 while (sta) {
180 prev = sta;
181 if (sta->flags & WLAN_STA_AUTH) {
182 mlme_deauthenticate_indication(
183 hapd, sta, WLAN_REASON_UNSPECIFIED);
185 sta = sta->next;
186 printf("Removing station " MACSTR "\n", MAC2STR(prev->addr));
187 ap_free_sta(hapd, prev);
192 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
194 struct hostapd_data *hapd = eloop_ctx;
195 struct sta_info *sta = timeout_ctx;
196 unsigned long next_time = 0;
198 if (sta->timeout_next == STA_REMOVE) {
199 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
200 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
201 "local deauth request");
202 ap_free_sta(hapd, sta);
203 return;
206 if ((sta->flags & WLAN_STA_ASSOC) &&
207 (sta->timeout_next == STA_NULLFUNC ||
208 sta->timeout_next == STA_DISASSOC)) {
209 int inactive_sec;
210 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
211 "Checking STA " MACSTR " inactivity:\n",
212 MAC2STR(sta->addr));
213 inactive_sec = hostapd_get_inact_sec(hapd, sta->addr);
214 if (inactive_sec == -1) {
215 printf(" Could not get station info from kernel "
216 "driver for " MACSTR ".\n",
217 MAC2STR(sta->addr));
218 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
219 sta->flags & WLAN_STA_ASSOC) {
220 /* station activity detected; reset timeout state */
221 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
222 " Station has been active\n");
223 sta->timeout_next = STA_NULLFUNC;
224 next_time = hapd->conf->ap_max_inactivity -
225 inactive_sec;
229 if ((sta->flags & WLAN_STA_ASSOC) &&
230 sta->timeout_next == STA_DISASSOC &&
231 !(sta->flags & WLAN_STA_PENDING_POLL)) {
232 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
233 " Station has ACKed data poll\n");
234 /* data nullfunc frame poll did not produce TX errors; assume
235 * station ACKed it */
236 sta->timeout_next = STA_NULLFUNC;
237 next_time = hapd->conf->ap_max_inactivity;
240 if (next_time) {
241 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
242 sta);
243 return;
246 if (sta->timeout_next == STA_NULLFUNC &&
247 (sta->flags & WLAN_STA_ASSOC)) {
248 /* send data frame to poll STA and check whether this frame
249 * is ACKed */
250 struct ieee80211_hdr hdr;
252 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL,
253 " Polling STA with data frame\n");
254 sta->flags |= WLAN_STA_PENDING_POLL;
256 #ifndef CONFIG_NATIVE_WINDOWS
257 /* FIX: WLAN_FC_STYPE_NULLFUNC would be more appropriate, but
258 * it is apparently not retried so TX Exc events are not
259 * received for it */
260 memset(&hdr, 0, sizeof(hdr));
261 hdr.frame_control =
262 IEEE80211_FC(WLAN_FC_TYPE_DATA, WLAN_FC_STYPE_DATA);
263 hdr.frame_control |= host_to_le16(BIT(1));
264 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
265 memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
266 memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr, ETH_ALEN);
267 memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
269 if (hostapd_send_mgmt_frame(hapd, &hdr, sizeof(hdr), 0) < 0)
270 perror("ap_handle_timer: send");
271 #endif /* CONFIG_NATIVE_WINDOWS */
272 } else if (sta->timeout_next != STA_REMOVE) {
273 int deauth = sta->timeout_next == STA_DEAUTH;
275 printf(" Sending %s info to STA " MACSTR "\n",
276 deauth ? "deauthentication" : "disassociation",
277 MAC2STR(sta->addr));
279 if (deauth) {
280 hostapd_sta_deauth(hapd, sta->addr,
281 WLAN_REASON_PREV_AUTH_NOT_VALID);
282 } else {
283 hostapd_sta_disassoc(
284 hapd, sta->addr,
285 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
289 switch (sta->timeout_next) {
290 case STA_NULLFUNC:
291 sta->timeout_next = STA_DISASSOC;
292 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
293 hapd, sta);
294 break;
295 case STA_DISASSOC:
296 sta->flags &= ~WLAN_STA_ASSOC;
297 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
298 if (!sta->acct_terminate_cause)
299 sta->acct_terminate_cause =
300 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
301 accounting_sta_stop(hapd, sta);
302 ieee802_1x_free_station(sta);
303 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
304 HOSTAPD_LEVEL_INFO, "disassociated due to "
305 "inactivity");
306 sta->timeout_next = STA_DEAUTH;
307 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
308 hapd, sta);
309 mlme_disassociate_indication(
310 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
311 break;
312 case STA_DEAUTH:
313 case STA_REMOVE:
314 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
315 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
316 "inactivity");
317 if (!sta->acct_terminate_cause)
318 sta->acct_terminate_cause =
319 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
320 mlme_deauthenticate_indication(
321 hapd, sta,
322 WLAN_REASON_PREV_AUTH_NOT_VALID);
323 ap_free_sta(hapd, sta);
324 break;
329 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
331 struct hostapd_data *hapd = eloop_ctx;
332 struct sta_info *sta = timeout_ctx;
333 u8 addr[ETH_ALEN];
335 if (!(sta->flags & WLAN_STA_AUTH))
336 return;
338 mlme_deauthenticate_indication(hapd, sta,
339 WLAN_REASON_PREV_AUTH_NOT_VALID);
340 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
341 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
342 "session timeout");
343 sta->acct_terminate_cause =
344 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
345 memcpy(addr, sta->addr, ETH_ALEN);
346 ap_free_sta(hapd, sta);
347 hostapd_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
351 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
352 u32 session_timeout)
354 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
355 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
356 "seconds", session_timeout);
357 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
358 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
359 hapd, sta);
363 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
365 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
369 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
371 struct sta_info *sta;
373 sta = ap_get_sta(hapd, addr);
374 if (sta)
375 return sta;
377 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, " New STA\n");
378 if (hapd->num_sta >= hapd->conf->max_num_sta) {
379 /* FIX: might try to remove some old STAs first? */
380 printf(" no more room for new STAs (%d/%d)\n",
381 hapd->num_sta, hapd->conf->max_num_sta);
382 return NULL;
385 sta = wpa_zalloc(sizeof(struct sta_info));
386 if (sta == NULL) {
387 printf(" malloc failed\n");
388 return NULL;
390 sta->acct_interim_interval = hapd->conf->radius->acct_interim_interval;
392 /* initialize STA info data */
393 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
394 ap_handle_timer, hapd, sta);
395 memcpy(sta->addr, addr, ETH_ALEN);
396 sta->next = hapd->sta_list;
397 hapd->sta_list = sta;
398 hapd->num_sta++;
399 ap_sta_hash_add(hapd, sta);
400 sta->ssid = &hapd->conf->ssid;
402 return sta;
406 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
408 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
410 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "Removing STA " MACSTR
411 " from kernel driver\n", MAC2STR(sta->addr));
412 if (hostapd_sta_remove(hapd, sta->addr) &&
413 sta->flags & WLAN_STA_ASSOC) {
414 printf("Could not remove station " MACSTR " from kernel "
415 "driver.\n", MAC2STR(sta->addr));
416 return -1;
418 return 0;
422 static int ap_sta_in_other_bss(struct hostapd_data *hapd,
423 struct sta_info *sta, u32 flags)
425 struct hostapd_iface *iface = hapd->iface;
426 size_t i;
428 for (i = 0; i < iface->num_bss; i++) {
429 struct hostapd_data *bss = iface->bss[i];
430 struct sta_info *sta2;
431 /* bss should always be set during operation, but it may be
432 * NULL during reconfiguration. Assume the STA is not
433 * associated to another BSS in that case to avoid NULL pointer
434 * dereferences. */
435 if (bss == hapd || bss == NULL)
436 continue;
437 sta2 = ap_get_sta(bss, sta->addr);
438 if (sta2 && ((sta2->flags & flags) == flags))
439 return 1;
442 return 0;
446 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
447 u16 reason)
449 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: disassociate STA " MACSTR
450 "\n", hapd->conf->iface, MAC2STR(sta->addr));
451 sta->flags &= ~WLAN_STA_ASSOC;
452 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
453 ap_sta_remove(hapd, sta);
454 sta->timeout_next = STA_DEAUTH;
455 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
456 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
457 ap_handle_timer, hapd, sta);
458 accounting_sta_stop(hapd, sta);
459 ieee802_1x_free_station(sta);
461 mlme_disassociate_indication(hapd, sta, reason);
465 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
466 u16 reason)
468 HOSTAPD_DEBUG(HOSTAPD_DEBUG_MINIMAL, "%s: deauthenticate STA " MACSTR
469 "\n", hapd->conf->iface, MAC2STR(sta->addr));
470 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
471 if (!ap_sta_in_other_bss(hapd, sta, WLAN_STA_ASSOC))
472 ap_sta_remove(hapd, sta);
473 sta->timeout_next = STA_REMOVE;
474 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
475 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
476 ap_handle_timer, hapd, sta);
477 accounting_sta_stop(hapd, sta);
478 ieee802_1x_free_station(sta);
480 mlme_deauthenticate_indication(hapd, sta, reason);
484 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
485 int old_vlanid)
487 const char *iface;
488 struct hostapd_vlan *vlan = NULL;
491 * Do not proceed furthur if the vlan id remains same. We do not want
492 * duplicate dynamic vlan entries.
494 if (sta->vlan_id == old_vlanid)
495 return 0;
498 * During 1x reauth, if the vlan id changes, then remove the old id and
499 * proceed furthur to add the new one.
501 if (old_vlanid > 0)
502 vlan_remove_dynamic(hapd, old_vlanid);
504 iface = hapd->conf->iface;
505 if (sta->ssid->vlan[0])
506 iface = sta->ssid->vlan;
508 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
509 sta->vlan_id = 0;
510 else if (sta->vlan_id > 0) {
511 vlan = hapd->conf->vlan;
512 while (vlan) {
513 if (vlan->vlan_id == sta->vlan_id ||
514 vlan->vlan_id == VLAN_ID_WILDCARD) {
515 iface = vlan->ifname;
516 break;
518 vlan = vlan->next;
522 if (sta->vlan_id > 0 && vlan == NULL) {
523 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
524 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
525 "binding station to (vlan_id=%d)",
526 sta->vlan_id);
527 return -1;
528 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
529 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
530 if (vlan == NULL) {
531 hostapd_logger(hapd, sta->addr,
532 HOSTAPD_MODULE_IEEE80211,
533 HOSTAPD_LEVEL_DEBUG, "could not add "
534 "dynamic VLAN interface for vlan_id=%d",
535 sta->vlan_id);
536 return -1;
539 iface = vlan->ifname;
540 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
541 hostapd_logger(hapd, sta->addr,
542 HOSTAPD_MODULE_IEEE80211,
543 HOSTAPD_LEVEL_DEBUG, "could not "
544 "configure encryption for dynamic VLAN "
545 "interface for vlan_id=%d",
546 sta->vlan_id);
549 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
550 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
551 "interface '%s'", iface);
552 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
553 if (sta->vlan_id > 0) {
554 vlan->dynamic_vlan++;
555 hostapd_logger(hapd, sta->addr,
556 HOSTAPD_MODULE_IEEE80211,
557 HOSTAPD_LEVEL_DEBUG, "updated existing "
558 "dynamic VLAN interface '%s'", iface);
562 * Update encryption configuration for statically generated
563 * VLAN interface. This is only used for static WEP
564 * configuration for the case where hostapd did not yet know
565 * which keys are to be used when the interface was added.
567 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
568 hostapd_logger(hapd, sta->addr,
569 HOSTAPD_MODULE_IEEE80211,
570 HOSTAPD_LEVEL_DEBUG, "could not "
571 "configure encryption for VLAN "
572 "interface for vlan_id=%d",
573 sta->vlan_id);
577 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
578 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
579 "'%s'", iface);
581 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
582 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
584 return hostapd_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);