blob: 95b701cc6e10c71b85c767bd79223d2062a97eed [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Station table
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070015#include "radius/radius.h"
16#include "radius/radius_client.h"
17#include "drivers/driver.h"
18#include "p2p/p2p.h"
19#include "hostapd.h"
20#include "accounting.h"
21#include "ieee802_1x.h"
22#include "ieee802_11.h"
23#include "wpa_auth.h"
24#include "preauth_auth.h"
25#include "ap_config.h"
26#include "beacon.h"
27#include "ap_mlme.h"
28#include "vlan_init.h"
29#include "p2p_hostapd.h"
30#include "ap_drv_ops.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070031#include "gas_serv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032#include "sta_info.h"
33
34static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
35 struct sta_info *sta);
36static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080037static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
38static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039#ifdef CONFIG_IEEE80211W
40static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
41#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070043
44int ap_for_each_sta(struct hostapd_data *hapd,
45 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
46 void *ctx),
47 void *ctx)
48{
49 struct sta_info *sta;
50
51 for (sta = hapd->sta_list; sta; sta = sta->next) {
52 if (cb(hapd, sta, ctx))
53 return 1;
54 }
55
56 return 0;
57}
58
59
60struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
61{
62 struct sta_info *s;
63
64 s = hapd->sta_hash[STA_HASH(sta)];
65 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
66 s = s->hnext;
67 return s;
68}
69
70
71static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
72{
73 struct sta_info *tmp;
74
75 if (hapd->sta_list == sta) {
76 hapd->sta_list = sta->next;
77 return;
78 }
79
80 tmp = hapd->sta_list;
81 while (tmp != NULL && tmp->next != sta)
82 tmp = tmp->next;
83 if (tmp == NULL) {
84 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
85 "list.", MAC2STR(sta->addr));
86 } else
87 tmp->next = sta->next;
88}
89
90
91void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
92{
93 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
94 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
95}
96
97
98static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
99{
100 struct sta_info *s;
101
102 s = hapd->sta_hash[STA_HASH(sta->addr)];
103 if (s == NULL) return;
104 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
105 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
106 return;
107 }
108
109 while (s->hnext != NULL &&
110 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
111 s = s->hnext;
112 if (s->hnext != NULL)
113 s->hnext = s->hnext->hnext;
114 else
115 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
116 " from hash table", MAC2STR(sta->addr));
117}
118
119
120void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
121{
122 int set_beacon = 0;
123
124 accounting_sta_stop(hapd, sta);
125
126 /* just in case */
127 ap_sta_set_authorized(hapd, sta, 0);
128
129 if (sta->flags & WLAN_STA_WDS)
130 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
131
132 if (!(sta->flags & WLAN_STA_PREAUTH))
133 hostapd_drv_sta_remove(hapd, sta->addr);
134
135 ap_sta_hash_del(hapd, sta);
136 ap_sta_list_del(hapd, sta);
137
138 if (sta->aid > 0)
139 hapd->sta_aid[(sta->aid - 1) / 32] &=
140 ~BIT((sta->aid - 1) % 32);
141
142 hapd->num_sta--;
143 if (sta->nonerp_set) {
144 sta->nonerp_set = 0;
145 hapd->iface->num_sta_non_erp--;
146 if (hapd->iface->num_sta_non_erp == 0)
147 set_beacon++;
148 }
149
150 if (sta->no_short_slot_time_set) {
151 sta->no_short_slot_time_set = 0;
152 hapd->iface->num_sta_no_short_slot_time--;
153 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
154 && hapd->iface->num_sta_no_short_slot_time == 0)
155 set_beacon++;
156 }
157
158 if (sta->no_short_preamble_set) {
159 sta->no_short_preamble_set = 0;
160 hapd->iface->num_sta_no_short_preamble--;
161 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
162 && hapd->iface->num_sta_no_short_preamble == 0)
163 set_beacon++;
164 }
165
166 if (sta->no_ht_gf_set) {
167 sta->no_ht_gf_set = 0;
168 hapd->iface->num_sta_ht_no_gf--;
169 }
170
171 if (sta->no_ht_set) {
172 sta->no_ht_set = 0;
173 hapd->iface->num_sta_no_ht--;
174 }
175
176 if (sta->ht_20mhz_set) {
177 sta->ht_20mhz_set = 0;
178 hapd->iface->num_sta_ht_20mhz--;
179 }
180
181#ifdef CONFIG_P2P
182 if (sta->no_p2p_set) {
183 sta->no_p2p_set = 0;
184 hapd->num_sta_no_p2p--;
185 if (hapd->num_sta_no_p2p == 0)
186 hostapd_p2p_non_p2p_sta_disconnected(hapd);
187 }
188#endif /* CONFIG_P2P */
189
190#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
191 if (hostapd_ht_operation_update(hapd->iface) > 0)
192 set_beacon++;
193#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
194
195 if (set_beacon)
196 ieee802_11_set_beacons(hapd->iface);
197
Dmitry Shmidt04949592012-07-19 12:16:46 -0700198 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
199 __func__, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700200 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
201 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800202 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
203 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204
205 ieee802_1x_free_station(sta);
206 wpa_auth_sta_deinit(sta->wpa_sm);
207 rsn_preauth_free_station(hapd, sta);
208#ifndef CONFIG_NO_RADIUS
209 radius_client_flush_auth(hapd->radius, sta->addr);
210#endif /* CONFIG_NO_RADIUS */
211
212 os_free(sta->last_assoc_req);
213 os_free(sta->challenge);
214
215#ifdef CONFIG_IEEE80211W
216 os_free(sta->sa_query_trans_id);
217 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
218#endif /* CONFIG_IEEE80211W */
219
220#ifdef CONFIG_P2P
221 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
222#endif /* CONFIG_P2P */
223
Dmitry Shmidt04949592012-07-19 12:16:46 -0700224#ifdef CONFIG_INTERWORKING
225 if (sta->gas_dialog) {
226 int i;
227 for (i = 0; i < GAS_DIALOG_MAX; i++)
228 gas_serv_dialog_clear(&sta->gas_dialog[i]);
229 os_free(sta->gas_dialog);
230 }
231#endif /* CONFIG_INTERWORKING */
232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700233 wpabuf_free(sta->wps_ie);
234 wpabuf_free(sta->p2p_ie);
235
236 os_free(sta->ht_capabilities);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800237 os_free(sta->psk);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700238
239 os_free(sta);
240}
241
242
243void hostapd_free_stas(struct hostapd_data *hapd)
244{
245 struct sta_info *sta, *prev;
246
247 sta = hapd->sta_list;
248
249 while (sta) {
250 prev = sta;
251 if (sta->flags & WLAN_STA_AUTH) {
252 mlme_deauthenticate_indication(
253 hapd, sta, WLAN_REASON_UNSPECIFIED);
254 }
255 sta = sta->next;
256 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
257 MAC2STR(prev->addr));
258 ap_free_sta(hapd, prev);
259 }
260}
261
262
263/**
264 * ap_handle_timer - Per STA timer handler
265 * @eloop_ctx: struct hostapd_data *
266 * @timeout_ctx: struct sta_info *
267 *
268 * This function is called to check station activity and to remove inactive
269 * stations.
270 */
271void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
272{
273 struct hostapd_data *hapd = eloop_ctx;
274 struct sta_info *sta = timeout_ctx;
275 unsigned long next_time = 0;
276
Dmitry Shmidt04949592012-07-19 12:16:46 -0700277 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
278 __func__, MAC2STR(sta->addr), sta->flags,
279 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700280 if (sta->timeout_next == STA_REMOVE) {
281 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
282 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
283 "local deauth request");
284 ap_free_sta(hapd, sta);
285 return;
286 }
287
288 if ((sta->flags & WLAN_STA_ASSOC) &&
289 (sta->timeout_next == STA_NULLFUNC ||
290 sta->timeout_next == STA_DISASSOC)) {
291 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700292 /*
293 * Add random value to timeout so that we don't end up bouncing
294 * all stations at the same time if we have lots of associated
295 * stations that are idle (but keep re-associating).
296 */
297 int fuzz = os_random() % 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
299 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800300 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
301 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800302 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700303 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800304 /*
305 * The driver may not support this functionality.
306 * Anyway, try again after the next inactivity timeout,
307 * but do not disconnect the station now.
308 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700309 next_time = hapd->conf->ap_max_inactivity + fuzz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700310 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
311 sta->flags & WLAN_STA_ASSOC) {
312 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
314 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700315 MAC2STR(sta->addr), inactive_sec);
316 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700317 next_time = hapd->conf->ap_max_inactivity + fuzz -
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 inactive_sec;
319 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800320 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
321 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 "inactive too long: %d sec, max allowed: %d",
323 MAC2STR(sta->addr), inactive_sec,
324 hapd->conf->ap_max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800325
326 if (hapd->conf->skip_inactivity_poll)
327 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700328 }
329 }
330
331 if ((sta->flags & WLAN_STA_ASSOC) &&
332 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800333 !(sta->flags & WLAN_STA_PENDING_POLL) &&
334 !hapd->conf->skip_inactivity_poll) {
335 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
336 " has ACKed data poll", MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337 /* data nullfunc frame poll did not produce TX errors; assume
338 * station ACKed it */
339 sta->timeout_next = STA_NULLFUNC;
340 next_time = hapd->conf->ap_max_inactivity;
341 }
342
343 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700344 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
345 "for " MACSTR " (%lu seconds)",
346 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
348 sta);
349 return;
350 }
351
352 if (sta->timeout_next == STA_NULLFUNC &&
353 (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800354 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700355 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800356 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
357 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 } else if (sta->timeout_next != STA_REMOVE) {
359 int deauth = sta->timeout_next == STA_DEAUTH;
360
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800361 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
362 "Timeout, sending %s info to STA " MACSTR,
363 deauth ? "deauthentication" : "disassociation",
364 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700365
366 if (deauth) {
367 hostapd_drv_sta_deauth(
368 hapd, sta->addr,
369 WLAN_REASON_PREV_AUTH_NOT_VALID);
370 } else {
371 hostapd_drv_sta_disassoc(
372 hapd, sta->addr,
373 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
374 }
375 }
376
377 switch (sta->timeout_next) {
378 case STA_NULLFUNC:
379 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700380 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
381 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
382 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
384 hapd, sta);
385 break;
386 case STA_DISASSOC:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800387 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700388 sta->flags &= ~WLAN_STA_ASSOC;
389 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
390 if (!sta->acct_terminate_cause)
391 sta->acct_terminate_cause =
392 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
393 accounting_sta_stop(hapd, sta);
394 ieee802_1x_free_station(sta);
395 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
396 HOSTAPD_LEVEL_INFO, "disassociated due to "
397 "inactivity");
398 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700399 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
400 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
401 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700402 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
403 hapd, sta);
404 mlme_disassociate_indication(
405 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
406 break;
407 case STA_DEAUTH:
408 case STA_REMOVE:
409 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
410 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800411 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 if (!sta->acct_terminate_cause)
413 sta->acct_terminate_cause =
414 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
415 mlme_deauthenticate_indication(
416 hapd, sta,
417 WLAN_REASON_PREV_AUTH_NOT_VALID);
418 ap_free_sta(hapd, sta);
419 break;
420 }
421}
422
423
424static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
425{
426 struct hostapd_data *hapd = eloop_ctx;
427 struct sta_info *sta = timeout_ctx;
428 u8 addr[ETH_ALEN];
429
Dmitry Shmidt04949592012-07-19 12:16:46 -0700430 if (!(sta->flags & WLAN_STA_AUTH)) {
431 if (sta->flags & WLAN_STA_GAS) {
432 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
433 "entry " MACSTR, MAC2STR(sta->addr));
434 ap_free_sta(hapd, sta);
435 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700436 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700437 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700438
439 mlme_deauthenticate_indication(hapd, sta,
440 WLAN_REASON_PREV_AUTH_NOT_VALID);
441 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
442 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
443 "session timeout");
444 sta->acct_terminate_cause =
445 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
446 os_memcpy(addr, sta->addr, ETH_ALEN);
447 ap_free_sta(hapd, sta);
448 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
449}
450
451
452void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
453 u32 session_timeout)
454{
455 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
456 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
457 "seconds", session_timeout);
458 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
459 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
460 hapd, sta);
461}
462
463
464void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
465{
466 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
467}
468
469
470struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
471{
472 struct sta_info *sta;
473
474 sta = ap_get_sta(hapd, addr);
475 if (sta)
476 return sta;
477
478 wpa_printf(MSG_DEBUG, " New STA");
479 if (hapd->num_sta >= hapd->conf->max_num_sta) {
480 /* FIX: might try to remove some old STAs first? */
481 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
482 hapd->num_sta, hapd->conf->max_num_sta);
483 return NULL;
484 }
485
486 sta = os_zalloc(sizeof(struct sta_info));
487 if (sta == NULL) {
488 wpa_printf(MSG_ERROR, "malloc failed");
489 return NULL;
490 }
491 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
492
493 /* initialize STA info data */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700494 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
495 "for " MACSTR " (%d seconds - ap_max_inactivity)",
496 __func__, MAC2STR(addr),
497 hapd->conf->ap_max_inactivity);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700498 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
499 ap_handle_timer, hapd, sta);
500 os_memcpy(sta->addr, addr, ETH_ALEN);
501 sta->next = hapd->sta_list;
502 hapd->sta_list = sta;
503 hapd->num_sta++;
504 ap_sta_hash_add(hapd, sta);
505 sta->ssid = &hapd->conf->ssid;
506 ap_sta_remove_in_other_bss(hapd, sta);
507
508 return sta;
509}
510
511
512static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
513{
514 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
515
516 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
517 MAC2STR(sta->addr));
518 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
519 sta->flags & WLAN_STA_ASSOC) {
520 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
521 " from kernel driver.", MAC2STR(sta->addr));
522 return -1;
523 }
524 return 0;
525}
526
527
528static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
529 struct sta_info *sta)
530{
531 struct hostapd_iface *iface = hapd->iface;
532 size_t i;
533
534 for (i = 0; i < iface->num_bss; i++) {
535 struct hostapd_data *bss = iface->bss[i];
536 struct sta_info *sta2;
537 /* bss should always be set during operation, but it may be
538 * NULL during reconfiguration. Assume the STA is not
539 * associated to another BSS in that case to avoid NULL pointer
540 * dereferences. */
541 if (bss == hapd || bss == NULL)
542 continue;
543 sta2 = ap_get_sta(bss, sta->addr);
544 if (!sta2)
545 continue;
546
547 ap_sta_disconnect(bss, sta2, sta2->addr,
548 WLAN_REASON_PREV_AUTH_NOT_VALID);
549 }
550}
551
552
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800553static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
554{
555 struct hostapd_data *hapd = eloop_ctx;
556 struct sta_info *sta = timeout_ctx;
557
558 ap_sta_remove(hapd, sta);
559 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
560}
561
562
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700563void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
564 u16 reason)
565{
566 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
567 hapd->conf->iface, MAC2STR(sta->addr));
568 sta->flags &= ~WLAN_STA_ASSOC;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800569 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700571 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
572 "for " MACSTR " (%d seconds - "
573 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
574 __func__, MAC2STR(sta->addr),
575 AP_MAX_INACTIVITY_AFTER_DISASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
577 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
578 ap_handle_timer, hapd, sta);
579 accounting_sta_stop(hapd, sta);
580 ieee802_1x_free_station(sta);
581
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800582 sta->disassoc_reason = reason;
583 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
584 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
585 eloop_register_timeout(hapd->iface->drv_flags &
586 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
587 ap_sta_disassoc_cb_timeout, hapd, sta);
588}
589
590
591static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
592{
593 struct hostapd_data *hapd = eloop_ctx;
594 struct sta_info *sta = timeout_ctx;
595
596 ap_sta_remove(hapd, sta);
597 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700598}
599
600
601void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
602 u16 reason)
603{
604 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
605 hapd->conf->iface, MAC2STR(sta->addr));
606 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800607 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700608 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700609 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
610 "for " MACSTR " (%d seconds - "
611 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
612 __func__, MAC2STR(sta->addr),
613 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700614 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
615 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
616 ap_handle_timer, hapd, sta);
617 accounting_sta_stop(hapd, sta);
618 ieee802_1x_free_station(sta);
619
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800620 sta->deauth_reason = reason;
621 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
622 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
623 eloop_register_timeout(hapd->iface->drv_flags &
624 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
625 ap_sta_deauth_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700626}
627
628
Dmitry Shmidt04949592012-07-19 12:16:46 -0700629#ifdef CONFIG_WPS
630int ap_sta_wps_cancel(struct hostapd_data *hapd,
631 struct sta_info *sta, void *ctx)
632{
633 if (sta && (sta->flags & WLAN_STA_WPS)) {
634 ap_sta_deauthenticate(hapd, sta,
635 WLAN_REASON_PREV_AUTH_NOT_VALID);
636 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
637 __func__, MAC2STR(sta->addr));
638 return 1;
639 }
640
641 return 0;
642}
643#endif /* CONFIG_WPS */
644
645
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
647 int old_vlanid)
648{
649#ifndef CONFIG_NO_VLAN
650 const char *iface;
651 struct hostapd_vlan *vlan = NULL;
652 int ret;
653
654 /*
655 * Do not proceed furthur if the vlan id remains same. We do not want
656 * duplicate dynamic vlan entries.
657 */
658 if (sta->vlan_id == old_vlanid)
659 return 0;
660
661 /*
662 * During 1x reauth, if the vlan id changes, then remove the old id and
663 * proceed furthur to add the new one.
664 */
665 if (old_vlanid > 0)
666 vlan_remove_dynamic(hapd, old_vlanid);
667
668 iface = hapd->conf->iface;
669 if (sta->ssid->vlan[0])
670 iface = sta->ssid->vlan;
671
672 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
673 sta->vlan_id = 0;
674 else if (sta->vlan_id > 0) {
675 vlan = hapd->conf->vlan;
676 while (vlan) {
677 if (vlan->vlan_id == sta->vlan_id ||
678 vlan->vlan_id == VLAN_ID_WILDCARD) {
679 iface = vlan->ifname;
680 break;
681 }
682 vlan = vlan->next;
683 }
684 }
685
686 if (sta->vlan_id > 0 && vlan == NULL) {
687 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
688 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
689 "binding station to (vlan_id=%d)",
690 sta->vlan_id);
691 return -1;
692 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
693 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
694 if (vlan == NULL) {
695 hostapd_logger(hapd, sta->addr,
696 HOSTAPD_MODULE_IEEE80211,
697 HOSTAPD_LEVEL_DEBUG, "could not add "
698 "dynamic VLAN interface for vlan_id=%d",
699 sta->vlan_id);
700 return -1;
701 }
702
703 iface = vlan->ifname;
704 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
705 hostapd_logger(hapd, sta->addr,
706 HOSTAPD_MODULE_IEEE80211,
707 HOSTAPD_LEVEL_DEBUG, "could not "
708 "configure encryption for dynamic VLAN "
709 "interface for vlan_id=%d",
710 sta->vlan_id);
711 }
712
713 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
714 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
715 "interface '%s'", iface);
716 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
717 if (sta->vlan_id > 0) {
718 vlan->dynamic_vlan++;
719 hostapd_logger(hapd, sta->addr,
720 HOSTAPD_MODULE_IEEE80211,
721 HOSTAPD_LEVEL_DEBUG, "updated existing "
722 "dynamic VLAN interface '%s'", iface);
723 }
724
725 /*
726 * Update encryption configuration for statically generated
727 * VLAN interface. This is only used for static WEP
728 * configuration for the case where hostapd did not yet know
729 * which keys are to be used when the interface was added.
730 */
731 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
732 hostapd_logger(hapd, sta->addr,
733 HOSTAPD_MODULE_IEEE80211,
734 HOSTAPD_LEVEL_DEBUG, "could not "
735 "configure encryption for VLAN "
736 "interface for vlan_id=%d",
737 sta->vlan_id);
738 }
739 }
740
741 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
742 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
743 "'%s'", iface);
744
745 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
746 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
747
748 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
749 if (ret < 0) {
750 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
751 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
752 "entry to vlan_id=%d", sta->vlan_id);
753 }
754 return ret;
755#else /* CONFIG_NO_VLAN */
756 return 0;
757#endif /* CONFIG_NO_VLAN */
758}
759
760
761#ifdef CONFIG_IEEE80211W
762
763int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
764{
765 u32 tu;
766 struct os_time now, passed;
767 os_get_time(&now);
768 os_time_sub(&now, &sta->sa_query_start, &passed);
769 tu = (passed.sec * 1000000 + passed.usec) / 1024;
770 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
771 hostapd_logger(hapd, sta->addr,
772 HOSTAPD_MODULE_IEEE80211,
773 HOSTAPD_LEVEL_DEBUG,
774 "association SA Query timed out");
775 sta->sa_query_timed_out = 1;
776 os_free(sta->sa_query_trans_id);
777 sta->sa_query_trans_id = NULL;
778 sta->sa_query_count = 0;
779 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
780 return 1;
781 }
782
783 return 0;
784}
785
786
787static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
788{
789 struct hostapd_data *hapd = eloop_ctx;
790 struct sta_info *sta = timeout_ctx;
791 unsigned int timeout, sec, usec;
792 u8 *trans_id, *nbuf;
793
794 if (sta->sa_query_count > 0 &&
795 ap_check_sa_query_timeout(hapd, sta))
796 return;
797
798 nbuf = os_realloc(sta->sa_query_trans_id,
799 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
800 if (nbuf == NULL)
801 return;
802 if (sta->sa_query_count == 0) {
803 /* Starting a new SA Query procedure */
804 os_get_time(&sta->sa_query_start);
805 }
806 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
807 sta->sa_query_trans_id = nbuf;
808 sta->sa_query_count++;
809
810 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
811
812 timeout = hapd->conf->assoc_sa_query_retry_timeout;
813 sec = ((timeout / 1000) * 1024) / 1000;
814 usec = (timeout % 1000) * 1024;
815 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
816
817 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
818 HOSTAPD_LEVEL_DEBUG,
819 "association SA Query attempt %d", sta->sa_query_count);
820
821#ifdef NEED_AP_MLME
822 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
823#endif /* NEED_AP_MLME */
824}
825
826
827void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
828{
829 ap_sa_query_timer(hapd, sta);
830}
831
832
833void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
834{
835 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
836 os_free(sta->sa_query_trans_id);
837 sta->sa_query_trans_id = NULL;
838 sta->sa_query_count = 0;
839}
840
841#endif /* CONFIG_IEEE80211W */
842
843
844void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
845 int authorized)
846{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800847 const u8 *dev_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700848#ifdef CONFIG_P2P
849 u8 addr[ETH_ALEN];
850#endif /* CONFIG_P2P */
851
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
853 return;
854
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800855#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700856 if (hapd->p2p_group == NULL) {
857 if (sta->p2p_ie != NULL &&
858 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
859 dev_addr = addr;
860 } else
861 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800862#endif /* CONFIG_P2P */
863
864 if (authorized) {
865 if (dev_addr)
866 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
867 MACSTR " p2p_dev_addr=" MACSTR,
868 MAC2STR(sta->addr), MAC2STR(dev_addr));
869 else
870 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
871 MACSTR, MAC2STR(sta->addr));
872 if (hapd->msg_ctx_parent &&
873 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
874 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
875 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
876 MACSTR,
877 MAC2STR(sta->addr), MAC2STR(dev_addr));
878 else if (hapd->msg_ctx_parent &&
879 hapd->msg_ctx_parent != hapd->msg_ctx)
880 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
881 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
882
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700883 sta->flags |= WLAN_STA_AUTHORIZED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800884 } else {
885 if (dev_addr)
886 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
887 MACSTR " p2p_dev_addr=" MACSTR,
888 MAC2STR(sta->addr), MAC2STR(dev_addr));
889 else
890 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
891 MACSTR, MAC2STR(sta->addr));
892 if (hapd->msg_ctx_parent &&
893 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
894 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
895 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
896 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
897 else if (hapd->msg_ctx_parent &&
898 hapd->msg_ctx_parent != hapd->msg_ctx)
899 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
900 AP_STA_DISCONNECTED MACSTR,
901 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700902 sta->flags &= ~WLAN_STA_AUTHORIZED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800903 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904
905 if (hapd->sta_authorized_cb)
906 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800907 sta->addr, authorized, dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700908}
909
910
911void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
912 const u8 *addr, u16 reason)
913{
914
915 if (sta == NULL && addr)
916 sta = ap_get_sta(hapd, addr);
917
918 if (addr)
919 hostapd_drv_sta_deauth(hapd, addr, reason);
920
921 if (sta == NULL)
922 return;
923 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800924 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
925 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700926 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700927 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
928 "for " MACSTR " (%d seconds - "
929 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
930 __func__, MAC2STR(sta->addr),
931 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700932 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800933 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
934 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936
937 sta->deauth_reason = reason;
938 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
939 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
940 eloop_register_timeout(hapd->iface->drv_flags &
941 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
942 ap_sta_deauth_cb_timeout, hapd, sta);
943}
944
945
946void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
947{
948 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
949 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
950 return;
951 }
952 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
953 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
954 ap_sta_deauth_cb_timeout(hapd, sta);
955}
956
957
958void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
959{
960 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
961 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
962 return;
963 }
964 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
965 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
966 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967}