blob: 6bc43d2d5e95fc4763557cbab880a11521f1ce72 [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"
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080023#include "ieee802_11_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070024#include "wpa_auth.h"
25#include "preauth_auth.h"
26#include "ap_config.h"
27#include "beacon.h"
28#include "ap_mlme.h"
29#include "vlan_init.h"
30#include "p2p_hostapd.h"
31#include "ap_drv_ops.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "gas_serv.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070033#include "sta_info.h"
34
35static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
36 struct sta_info *sta);
37static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
39static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070040#ifdef CONFIG_IEEE80211W
41static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
42#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044
45int ap_for_each_sta(struct hostapd_data *hapd,
46 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
47 void *ctx),
48 void *ctx)
49{
50 struct sta_info *sta;
51
52 for (sta = hapd->sta_list; sta; sta = sta->next) {
53 if (cb(hapd, sta, ctx))
54 return 1;
55 }
56
57 return 0;
58}
59
60
61struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
62{
63 struct sta_info *s;
64
65 s = hapd->sta_hash[STA_HASH(sta)];
66 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
67 s = s->hnext;
68 return s;
69}
70
71
72static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
73{
74 struct sta_info *tmp;
75
76 if (hapd->sta_list == sta) {
77 hapd->sta_list = sta->next;
78 return;
79 }
80
81 tmp = hapd->sta_list;
82 while (tmp != NULL && tmp->next != sta)
83 tmp = tmp->next;
84 if (tmp == NULL) {
85 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
86 "list.", MAC2STR(sta->addr));
87 } else
88 tmp->next = sta->next;
89}
90
91
92void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
93{
94 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
95 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
96}
97
98
99static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
100{
101 struct sta_info *s;
102
103 s = hapd->sta_hash[STA_HASH(sta->addr)];
104 if (s == NULL) return;
105 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
106 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
107 return;
108 }
109
110 while (s->hnext != NULL &&
111 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
112 s = s->hnext;
113 if (s->hnext != NULL)
114 s->hnext = s->hnext->hnext;
115 else
116 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
117 " from hash table", MAC2STR(sta->addr));
118}
119
120
121void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
122{
123 int set_beacon = 0;
124
125 accounting_sta_stop(hapd, sta);
126
127 /* just in case */
128 ap_sta_set_authorized(hapd, sta, 0);
129
130 if (sta->flags & WLAN_STA_WDS)
131 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
132
133 if (!(sta->flags & WLAN_STA_PREAUTH))
134 hostapd_drv_sta_remove(hapd, sta->addr);
135
136 ap_sta_hash_del(hapd, sta);
137 ap_sta_list_del(hapd, sta);
138
139 if (sta->aid > 0)
140 hapd->sta_aid[(sta->aid - 1) / 32] &=
141 ~BIT((sta->aid - 1) % 32);
142
143 hapd->num_sta--;
144 if (sta->nonerp_set) {
145 sta->nonerp_set = 0;
146 hapd->iface->num_sta_non_erp--;
147 if (hapd->iface->num_sta_non_erp == 0)
148 set_beacon++;
149 }
150
151 if (sta->no_short_slot_time_set) {
152 sta->no_short_slot_time_set = 0;
153 hapd->iface->num_sta_no_short_slot_time--;
154 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
155 && hapd->iface->num_sta_no_short_slot_time == 0)
156 set_beacon++;
157 }
158
159 if (sta->no_short_preamble_set) {
160 sta->no_short_preamble_set = 0;
161 hapd->iface->num_sta_no_short_preamble--;
162 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
163 && hapd->iface->num_sta_no_short_preamble == 0)
164 set_beacon++;
165 }
166
167 if (sta->no_ht_gf_set) {
168 sta->no_ht_gf_set = 0;
169 hapd->iface->num_sta_ht_no_gf--;
170 }
171
172 if (sta->no_ht_set) {
173 sta->no_ht_set = 0;
174 hapd->iface->num_sta_no_ht--;
175 }
176
177 if (sta->ht_20mhz_set) {
178 sta->ht_20mhz_set = 0;
179 hapd->iface->num_sta_ht_20mhz--;
180 }
181
182#ifdef CONFIG_P2P
183 if (sta->no_p2p_set) {
184 sta->no_p2p_set = 0;
185 hapd->num_sta_no_p2p--;
186 if (hapd->num_sta_no_p2p == 0)
187 hostapd_p2p_non_p2p_sta_disconnected(hapd);
188 }
189#endif /* CONFIG_P2P */
190
191#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
192 if (hostapd_ht_operation_update(hapd->iface) > 0)
193 set_beacon++;
194#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
195
196 if (set_beacon)
197 ieee802_11_set_beacons(hapd->iface);
198
Dmitry Shmidt04949592012-07-19 12:16:46 -0700199 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
200 __func__, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
202 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
204 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205
206 ieee802_1x_free_station(sta);
207 wpa_auth_sta_deinit(sta->wpa_sm);
208 rsn_preauth_free_station(hapd, sta);
209#ifndef CONFIG_NO_RADIUS
210 radius_client_flush_auth(hapd->radius, sta->addr);
211#endif /* CONFIG_NO_RADIUS */
212
213 os_free(sta->last_assoc_req);
214 os_free(sta->challenge);
215
216#ifdef CONFIG_IEEE80211W
217 os_free(sta->sa_query_trans_id);
218 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
219#endif /* CONFIG_IEEE80211W */
220
221#ifdef CONFIG_P2P
222 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
223#endif /* CONFIG_P2P */
224
Dmitry Shmidt04949592012-07-19 12:16:46 -0700225#ifdef CONFIG_INTERWORKING
226 if (sta->gas_dialog) {
227 int i;
228 for (i = 0; i < GAS_DIALOG_MAX; i++)
229 gas_serv_dialog_clear(&sta->gas_dialog[i]);
230 os_free(sta->gas_dialog);
231 }
232#endif /* CONFIG_INTERWORKING */
233
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 wpabuf_free(sta->wps_ie);
235 wpabuf_free(sta->p2p_ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800236 wpabuf_free(sta->hs20_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237
238 os_free(sta->ht_capabilities);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800239 hostapd_free_psk_list(sta->psk);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700240 os_free(sta->identity);
241 os_free(sta->radius_cui);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700242
243 os_free(sta);
244}
245
246
247void hostapd_free_stas(struct hostapd_data *hapd)
248{
249 struct sta_info *sta, *prev;
250
251 sta = hapd->sta_list;
252
253 while (sta) {
254 prev = sta;
255 if (sta->flags & WLAN_STA_AUTH) {
256 mlme_deauthenticate_indication(
257 hapd, sta, WLAN_REASON_UNSPECIFIED);
258 }
259 sta = sta->next;
260 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
261 MAC2STR(prev->addr));
262 ap_free_sta(hapd, prev);
263 }
264}
265
266
267/**
268 * ap_handle_timer - Per STA timer handler
269 * @eloop_ctx: struct hostapd_data *
270 * @timeout_ctx: struct sta_info *
271 *
272 * This function is called to check station activity and to remove inactive
273 * stations.
274 */
275void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
276{
277 struct hostapd_data *hapd = eloop_ctx;
278 struct sta_info *sta = timeout_ctx;
279 unsigned long next_time = 0;
280
Dmitry Shmidt04949592012-07-19 12:16:46 -0700281 wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
282 __func__, MAC2STR(sta->addr), sta->flags,
283 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284 if (sta->timeout_next == STA_REMOVE) {
285 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
286 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
287 "local deauth request");
288 ap_free_sta(hapd, sta);
289 return;
290 }
291
292 if ((sta->flags & WLAN_STA_ASSOC) &&
293 (sta->timeout_next == STA_NULLFUNC ||
294 sta->timeout_next == STA_DISASSOC)) {
295 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700296 /*
297 * Add random value to timeout so that we don't end up bouncing
298 * all stations at the same time if we have lots of associated
299 * stations that are idle (but keep re-associating).
300 */
301 int fuzz = os_random() % 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700302 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
303 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800304 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
305 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800306 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800308 /*
309 * The driver may not support this functionality.
310 * Anyway, try again after the next inactivity timeout,
311 * but do not disconnect the station now.
312 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700313 next_time = hapd->conf->ap_max_inactivity + fuzz;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700314 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
315 sta->flags & WLAN_STA_ASSOC) {
316 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800317 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
318 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700319 MAC2STR(sta->addr), inactive_sec);
320 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700321 next_time = hapd->conf->ap_max_inactivity + fuzz -
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 inactive_sec;
323 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800324 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
325 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700326 "inactive too long: %d sec, max allowed: %d",
327 MAC2STR(sta->addr), inactive_sec,
328 hapd->conf->ap_max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800329
330 if (hapd->conf->skip_inactivity_poll)
331 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700332 }
333 }
334
335 if ((sta->flags & WLAN_STA_ASSOC) &&
336 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800337 !(sta->flags & WLAN_STA_PENDING_POLL) &&
338 !hapd->conf->skip_inactivity_poll) {
339 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
340 " has ACKed data poll", MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 /* data nullfunc frame poll did not produce TX errors; assume
342 * station ACKed it */
343 sta->timeout_next = STA_NULLFUNC;
344 next_time = hapd->conf->ap_max_inactivity;
345 }
346
347 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700348 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
349 "for " MACSTR " (%lu seconds)",
350 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700351 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
352 sta);
353 return;
354 }
355
356 if (sta->timeout_next == STA_NULLFUNC &&
357 (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800358 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700359 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800360 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
361 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362 } else if (sta->timeout_next != STA_REMOVE) {
363 int deauth = sta->timeout_next == STA_DEAUTH;
364
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800365 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
366 "Timeout, sending %s info to STA " MACSTR,
367 deauth ? "deauthentication" : "disassociation",
368 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700369
370 if (deauth) {
371 hostapd_drv_sta_deauth(
372 hapd, sta->addr,
373 WLAN_REASON_PREV_AUTH_NOT_VALID);
374 } else {
375 hostapd_drv_sta_disassoc(
376 hapd, sta->addr,
377 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
378 }
379 }
380
381 switch (sta->timeout_next) {
382 case STA_NULLFUNC:
383 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700384 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
385 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
386 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
388 hapd, sta);
389 break;
390 case STA_DISASSOC:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800391 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700392 sta->flags &= ~WLAN_STA_ASSOC;
393 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
394 if (!sta->acct_terminate_cause)
395 sta->acct_terminate_cause =
396 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
397 accounting_sta_stop(hapd, sta);
398 ieee802_1x_free_station(sta);
399 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
400 HOSTAPD_LEVEL_INFO, "disassociated due to "
401 "inactivity");
402 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700403 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
404 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
405 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
407 hapd, sta);
408 mlme_disassociate_indication(
409 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
410 break;
411 case STA_DEAUTH:
412 case STA_REMOVE:
413 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
414 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800415 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700416 if (!sta->acct_terminate_cause)
417 sta->acct_terminate_cause =
418 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
419 mlme_deauthenticate_indication(
420 hapd, sta,
421 WLAN_REASON_PREV_AUTH_NOT_VALID);
422 ap_free_sta(hapd, sta);
423 break;
424 }
425}
426
427
428static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
429{
430 struct hostapd_data *hapd = eloop_ctx;
431 struct sta_info *sta = timeout_ctx;
432 u8 addr[ETH_ALEN];
433
Dmitry Shmidt04949592012-07-19 12:16:46 -0700434 if (!(sta->flags & WLAN_STA_AUTH)) {
435 if (sta->flags & WLAN_STA_GAS) {
436 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
437 "entry " MACSTR, MAC2STR(sta->addr));
438 ap_free_sta(hapd, sta);
439 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700440 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700441 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700442
443 mlme_deauthenticate_indication(hapd, sta,
444 WLAN_REASON_PREV_AUTH_NOT_VALID);
445 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
446 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
447 "session timeout");
448 sta->acct_terminate_cause =
449 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
450 os_memcpy(addr, sta->addr, ETH_ALEN);
451 ap_free_sta(hapd, sta);
452 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
453}
454
455
456void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
457 u32 session_timeout)
458{
459 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
460 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
461 "seconds", session_timeout);
462 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
463 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
464 hapd, sta);
465}
466
467
468void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
469{
470 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
471}
472
473
474struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
475{
476 struct sta_info *sta;
477
478 sta = ap_get_sta(hapd, addr);
479 if (sta)
480 return sta;
481
482 wpa_printf(MSG_DEBUG, " New STA");
483 if (hapd->num_sta >= hapd->conf->max_num_sta) {
484 /* FIX: might try to remove some old STAs first? */
485 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
486 hapd->num_sta, hapd->conf->max_num_sta);
487 return NULL;
488 }
489
490 sta = os_zalloc(sizeof(struct sta_info));
491 if (sta == NULL) {
492 wpa_printf(MSG_ERROR, "malloc failed");
493 return NULL;
494 }
495 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
496
497 /* initialize STA info data */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700498 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
499 "for " MACSTR " (%d seconds - ap_max_inactivity)",
500 __func__, MAC2STR(addr),
501 hapd->conf->ap_max_inactivity);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
503 ap_handle_timer, hapd, sta);
504 os_memcpy(sta->addr, addr, ETH_ALEN);
505 sta->next = hapd->sta_list;
506 hapd->sta_list = sta;
507 hapd->num_sta++;
508 ap_sta_hash_add(hapd, sta);
509 sta->ssid = &hapd->conf->ssid;
510 ap_sta_remove_in_other_bss(hapd, sta);
511
512 return sta;
513}
514
515
516static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
517{
518 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
519
520 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
521 MAC2STR(sta->addr));
522 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
523 sta->flags & WLAN_STA_ASSOC) {
524 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
525 " from kernel driver.", MAC2STR(sta->addr));
526 return -1;
527 }
528 return 0;
529}
530
531
532static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
533 struct sta_info *sta)
534{
535 struct hostapd_iface *iface = hapd->iface;
536 size_t i;
537
538 for (i = 0; i < iface->num_bss; i++) {
539 struct hostapd_data *bss = iface->bss[i];
540 struct sta_info *sta2;
541 /* bss should always be set during operation, but it may be
542 * NULL during reconfiguration. Assume the STA is not
543 * associated to another BSS in that case to avoid NULL pointer
544 * dereferences. */
545 if (bss == hapd || bss == NULL)
546 continue;
547 sta2 = ap_get_sta(bss, sta->addr);
548 if (!sta2)
549 continue;
550
551 ap_sta_disconnect(bss, sta2, sta2->addr,
552 WLAN_REASON_PREV_AUTH_NOT_VALID);
553 }
554}
555
556
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800557static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
558{
559 struct hostapd_data *hapd = eloop_ctx;
560 struct sta_info *sta = timeout_ctx;
561
562 ap_sta_remove(hapd, sta);
563 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
564}
565
566
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700567void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
568 u16 reason)
569{
570 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
571 hapd->conf->iface, MAC2STR(sta->addr));
572 sta->flags &= ~WLAN_STA_ASSOC;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800573 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700574 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700575 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
576 "for " MACSTR " (%d seconds - "
577 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
578 __func__, MAC2STR(sta->addr),
579 AP_MAX_INACTIVITY_AFTER_DISASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700580 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
581 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
582 ap_handle_timer, hapd, sta);
583 accounting_sta_stop(hapd, sta);
584 ieee802_1x_free_station(sta);
585
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800586 sta->disassoc_reason = reason;
587 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
588 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
589 eloop_register_timeout(hapd->iface->drv_flags &
590 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
591 ap_sta_disassoc_cb_timeout, hapd, sta);
592}
593
594
595static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
596{
597 struct hostapd_data *hapd = eloop_ctx;
598 struct sta_info *sta = timeout_ctx;
599
600 ap_sta_remove(hapd, sta);
601 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700602}
603
604
605void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
606 u16 reason)
607{
608 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
609 hapd->conf->iface, MAC2STR(sta->addr));
610 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800611 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700613 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
614 "for " MACSTR " (%d seconds - "
615 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
616 __func__, MAC2STR(sta->addr),
617 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
619 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
620 ap_handle_timer, hapd, sta);
621 accounting_sta_stop(hapd, sta);
622 ieee802_1x_free_station(sta);
623
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800624 sta->deauth_reason = reason;
625 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
626 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
627 eloop_register_timeout(hapd->iface->drv_flags &
628 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
629 ap_sta_deauth_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700630}
631
632
Dmitry Shmidt04949592012-07-19 12:16:46 -0700633#ifdef CONFIG_WPS
634int ap_sta_wps_cancel(struct hostapd_data *hapd,
635 struct sta_info *sta, void *ctx)
636{
637 if (sta && (sta->flags & WLAN_STA_WPS)) {
638 ap_sta_deauthenticate(hapd, sta,
639 WLAN_REASON_PREV_AUTH_NOT_VALID);
640 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
641 __func__, MAC2STR(sta->addr));
642 return 1;
643 }
644
645 return 0;
646}
647#endif /* CONFIG_WPS */
648
649
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700650int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
651 int old_vlanid)
652{
653#ifndef CONFIG_NO_VLAN
654 const char *iface;
655 struct hostapd_vlan *vlan = NULL;
656 int ret;
657
658 /*
659 * Do not proceed furthur if the vlan id remains same. We do not want
660 * duplicate dynamic vlan entries.
661 */
662 if (sta->vlan_id == old_vlanid)
663 return 0;
664
665 /*
666 * During 1x reauth, if the vlan id changes, then remove the old id and
667 * proceed furthur to add the new one.
668 */
669 if (old_vlanid > 0)
670 vlan_remove_dynamic(hapd, old_vlanid);
671
672 iface = hapd->conf->iface;
673 if (sta->ssid->vlan[0])
674 iface = sta->ssid->vlan;
675
676 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
677 sta->vlan_id = 0;
678 else if (sta->vlan_id > 0) {
679 vlan = hapd->conf->vlan;
680 while (vlan) {
681 if (vlan->vlan_id == sta->vlan_id ||
682 vlan->vlan_id == VLAN_ID_WILDCARD) {
683 iface = vlan->ifname;
684 break;
685 }
686 vlan = vlan->next;
687 }
688 }
689
690 if (sta->vlan_id > 0 && vlan == NULL) {
691 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
692 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
693 "binding station to (vlan_id=%d)",
694 sta->vlan_id);
695 return -1;
696 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
697 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
698 if (vlan == NULL) {
699 hostapd_logger(hapd, sta->addr,
700 HOSTAPD_MODULE_IEEE80211,
701 HOSTAPD_LEVEL_DEBUG, "could not add "
702 "dynamic VLAN interface for vlan_id=%d",
703 sta->vlan_id);
704 return -1;
705 }
706
707 iface = vlan->ifname;
708 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
709 hostapd_logger(hapd, sta->addr,
710 HOSTAPD_MODULE_IEEE80211,
711 HOSTAPD_LEVEL_DEBUG, "could not "
712 "configure encryption for dynamic VLAN "
713 "interface for vlan_id=%d",
714 sta->vlan_id);
715 }
716
717 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
718 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
719 "interface '%s'", iface);
720 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
721 if (sta->vlan_id > 0) {
722 vlan->dynamic_vlan++;
723 hostapd_logger(hapd, sta->addr,
724 HOSTAPD_MODULE_IEEE80211,
725 HOSTAPD_LEVEL_DEBUG, "updated existing "
726 "dynamic VLAN interface '%s'", iface);
727 }
728
729 /*
730 * Update encryption configuration for statically generated
731 * VLAN interface. This is only used for static WEP
732 * configuration for the case where hostapd did not yet know
733 * which keys are to be used when the interface was added.
734 */
735 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
736 hostapd_logger(hapd, sta->addr,
737 HOSTAPD_MODULE_IEEE80211,
738 HOSTAPD_LEVEL_DEBUG, "could not "
739 "configure encryption for VLAN "
740 "interface for vlan_id=%d",
741 sta->vlan_id);
742 }
743 }
744
745 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
746 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
747 "'%s'", iface);
748
749 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
750 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
751
752 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
753 if (ret < 0) {
754 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
755 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
756 "entry to vlan_id=%d", sta->vlan_id);
757 }
758 return ret;
759#else /* CONFIG_NO_VLAN */
760 return 0;
761#endif /* CONFIG_NO_VLAN */
762}
763
764
765#ifdef CONFIG_IEEE80211W
766
767int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
768{
769 u32 tu;
770 struct os_time now, passed;
771 os_get_time(&now);
772 os_time_sub(&now, &sta->sa_query_start, &passed);
773 tu = (passed.sec * 1000000 + passed.usec) / 1024;
774 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
775 hostapd_logger(hapd, sta->addr,
776 HOSTAPD_MODULE_IEEE80211,
777 HOSTAPD_LEVEL_DEBUG,
778 "association SA Query timed out");
779 sta->sa_query_timed_out = 1;
780 os_free(sta->sa_query_trans_id);
781 sta->sa_query_trans_id = NULL;
782 sta->sa_query_count = 0;
783 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
784 return 1;
785 }
786
787 return 0;
788}
789
790
791static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
792{
793 struct hostapd_data *hapd = eloop_ctx;
794 struct sta_info *sta = timeout_ctx;
795 unsigned int timeout, sec, usec;
796 u8 *trans_id, *nbuf;
797
798 if (sta->sa_query_count > 0 &&
799 ap_check_sa_query_timeout(hapd, sta))
800 return;
801
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700802 nbuf = os_realloc_array(sta->sa_query_trans_id,
803 sta->sa_query_count + 1,
804 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 if (nbuf == NULL)
806 return;
807 if (sta->sa_query_count == 0) {
808 /* Starting a new SA Query procedure */
809 os_get_time(&sta->sa_query_start);
810 }
811 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
812 sta->sa_query_trans_id = nbuf;
813 sta->sa_query_count++;
814
815 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
816
817 timeout = hapd->conf->assoc_sa_query_retry_timeout;
818 sec = ((timeout / 1000) * 1024) / 1000;
819 usec = (timeout % 1000) * 1024;
820 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
821
822 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
823 HOSTAPD_LEVEL_DEBUG,
824 "association SA Query attempt %d", sta->sa_query_count);
825
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700826 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827}
828
829
830void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
831{
832 ap_sa_query_timer(hapd, sta);
833}
834
835
836void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
837{
838 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
839 os_free(sta->sa_query_trans_id);
840 sta->sa_query_trans_id = NULL;
841 sta->sa_query_count = 0;
842}
843
844#endif /* CONFIG_IEEE80211W */
845
846
847void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
848 int authorized)
849{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800850 const u8 *dev_addr = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700851#ifdef CONFIG_P2P
852 u8 addr[ETH_ALEN];
853#endif /* CONFIG_P2P */
854
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700855 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
856 return;
857
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800858#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -0700859 if (hapd->p2p_group == NULL) {
860 if (sta->p2p_ie != NULL &&
861 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
862 dev_addr = addr;
863 } else
864 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800865#endif /* CONFIG_P2P */
866
867 if (authorized) {
868 if (dev_addr)
869 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
870 MACSTR " p2p_dev_addr=" MACSTR,
871 MAC2STR(sta->addr), MAC2STR(dev_addr));
872 else
873 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
874 MACSTR, MAC2STR(sta->addr));
875 if (hapd->msg_ctx_parent &&
876 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
877 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
878 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
879 MACSTR,
880 MAC2STR(sta->addr), MAC2STR(dev_addr));
881 else if (hapd->msg_ctx_parent &&
882 hapd->msg_ctx_parent != hapd->msg_ctx)
883 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
884 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
885
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 sta->flags |= WLAN_STA_AUTHORIZED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800887 } else {
888 if (dev_addr)
889 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
890 MACSTR " p2p_dev_addr=" MACSTR,
891 MAC2STR(sta->addr), MAC2STR(dev_addr));
892 else
893 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
894 MACSTR, MAC2STR(sta->addr));
895 if (hapd->msg_ctx_parent &&
896 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
897 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
898 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
899 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
900 else if (hapd->msg_ctx_parent &&
901 hapd->msg_ctx_parent != hapd->msg_ctx)
902 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
903 AP_STA_DISCONNECTED MACSTR,
904 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700905 sta->flags &= ~WLAN_STA_AUTHORIZED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800906 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700907
908 if (hapd->sta_authorized_cb)
909 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800910 sta->addr, authorized, dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911}
912
913
914void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
915 const u8 *addr, u16 reason)
916{
917
918 if (sta == NULL && addr)
919 sta = ap_get_sta(hapd, addr);
920
921 if (addr)
922 hostapd_drv_sta_deauth(hapd, addr, reason);
923
924 if (sta == NULL)
925 return;
926 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800927 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
928 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700929 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700930 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
931 "for " MACSTR " (%d seconds - "
932 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
933 __func__, MAC2STR(sta->addr),
934 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700935 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800936 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
937 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700938 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800939
940 sta->deauth_reason = reason;
941 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
942 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
943 eloop_register_timeout(hapd->iface->drv_flags &
944 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
945 ap_sta_deauth_cb_timeout, hapd, sta);
946}
947
948
949void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
950{
951 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
952 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
953 return;
954 }
955 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
956 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
957 ap_sta_deauth_cb_timeout(hapd, sta);
958}
959
960
961void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
962{
963 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
964 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
965 return;
966 }
967 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
968 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
969 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970}