]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/sta_info.c
Remove the GPL notification from files contributed by Atheros
[thirdparty/hostap.git] / src / ap / sta_info.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / Station table
4dc03726 3 * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
6fc6879b
JM
4 *
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.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
6226e38d 15#include "utils/includes.h"
6fc6879b 16
6226e38d
JM
17#include "utils/common.h"
18#include "utils/eloop.h"
81f4f619 19#include "common/ieee802_11_defs.h"
6caaae1e 20#include "common/wpa_ctrl.h"
bdee6fce
JM
21#include "radius/radius.h"
22#include "radius/radius_client.h"
23#include "drivers/driver.h"
8ccbe415 24#include "p2p/p2p.h"
6fc6879b 25#include "hostapd.h"
6fc6879b
JM
26#include "accounting.h"
27#include "ieee802_1x.h"
28#include "ieee802_11.h"
6226e38d
JM
29#include "wpa_auth.h"
30#include "preauth_auth.h"
31#include "ap_config.h"
6fc6879b 32#include "beacon.h"
6226e38d 33#include "ap_mlme.h"
6fc6879b 34#include "vlan_init.h"
aefb53bd 35#include "p2p_hostapd.h"
cee7d66b 36#include "ap_drv_ops.h"
6226e38d 37#include "sta_info.h"
6fc6879b 38
53f3d6f3
FF
39static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
40 struct sta_info *sta);
6fc6879b 41static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
4dc03726
JM
42static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 44#ifdef CONFIG_IEEE80211W
93b76319 45static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 46#endif /* CONFIG_IEEE80211W */
4dc03726 47static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
6fc6879b
JM
48
49int ap_for_each_sta(struct hostapd_data *hapd,
50 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
51 void *ctx),
52 void *ctx)
53{
54 struct sta_info *sta;
55
56 for (sta = hapd->sta_list; sta; sta = sta->next) {
57 if (cb(hapd, sta, ctx))
58 return 1;
59 }
60
61 return 0;
62}
63
64
65struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
66{
67 struct sta_info *s;
68
69 s = hapd->sta_hash[STA_HASH(sta)];
70 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
71 s = s->hnext;
72 return s;
73}
74
75
76static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
77{
78 struct sta_info *tmp;
79
80 if (hapd->sta_list == sta) {
81 hapd->sta_list = sta->next;
82 return;
83 }
84
85 tmp = hapd->sta_list;
86 while (tmp != NULL && tmp->next != sta)
87 tmp = tmp->next;
88 if (tmp == NULL) {
89 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
90 "list.", MAC2STR(sta->addr));
91 } else
92 tmp->next = sta->next;
93}
94
95
96void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
97{
98 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
99 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
100}
101
102
103static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
104{
105 struct sta_info *s;
106
107 s = hapd->sta_hash[STA_HASH(sta->addr)];
108 if (s == NULL) return;
109 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
110 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
111 return;
112 }
113
114 while (s->hnext != NULL &&
115 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
116 s = s->hnext;
117 if (s->hnext != NULL)
118 s->hnext = s->hnext->hnext;
119 else
120 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
121 " from hash table", MAC2STR(sta->addr));
122}
123
124
125void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
126{
127 int set_beacon = 0;
128
129 accounting_sta_stop(hapd, sta);
130
6905dcb1
JB
131 /* just in case */
132 ap_sta_set_authorized(hapd, sta, 0);
133
39f42d11 134 if (sta->flags & WLAN_STA_WDS)
0e8a96a9 135 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
53f3d6f3
FF
136
137 if (!(sta->flags & WLAN_STA_PREAUTH))
51e2a27a 138 hostapd_drv_sta_remove(hapd, sta->addr);
6fc6879b
JM
139
140 ap_sta_hash_del(hapd, sta);
141 ap_sta_list_del(hapd, sta);
142
143 if (sta->aid > 0)
2991469c
JM
144 hapd->sta_aid[(sta->aid - 1) / 32] &=
145 ~BIT((sta->aid - 1) % 32);
6fc6879b
JM
146
147 hapd->num_sta--;
148 if (sta->nonerp_set) {
149 sta->nonerp_set = 0;
150 hapd->iface->num_sta_non_erp--;
151 if (hapd->iface->num_sta_non_erp == 0)
152 set_beacon++;
153 }
154
155 if (sta->no_short_slot_time_set) {
156 sta->no_short_slot_time_set = 0;
157 hapd->iface->num_sta_no_short_slot_time--;
158 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
159 && hapd->iface->num_sta_no_short_slot_time == 0)
160 set_beacon++;
161 }
162
163 if (sta->no_short_preamble_set) {
164 sta->no_short_preamble_set = 0;
165 hapd->iface->num_sta_no_short_preamble--;
166 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
167 && hapd->iface->num_sta_no_short_preamble == 0)
168 set_beacon++;
169 }
170
e8ff1e59
JM
171 if (sta->no_ht_gf_set) {
172 sta->no_ht_gf_set = 0;
173 hapd->iface->num_sta_ht_no_gf--;
174 }
175
176 if (sta->no_ht_set) {
177 sta->no_ht_set = 0;
de9289c8 178 hapd->iface->num_sta_no_ht--;
e8ff1e59
JM
179 }
180
181 if (sta->ht_20mhz_set) {
182 sta->ht_20mhz_set = 0;
183 hapd->iface->num_sta_ht_20mhz--;
184 }
de9289c8 185
aefb53bd
JM
186#ifdef CONFIG_P2P
187 if (sta->no_p2p_set) {
188 sta->no_p2p_set = 0;
189 hapd->num_sta_no_p2p--;
190 if (hapd->num_sta_no_p2p == 0)
191 hostapd_p2p_non_p2p_sta_disconnected(hapd);
192 }
193#endif /* CONFIG_P2P */
194
d45354be 195#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
de9289c8
JM
196 if (hostapd_ht_operation_update(hapd->iface) > 0)
197 set_beacon++;
d45354be 198#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
de9289c8 199
6fc6879b
JM
200 if (set_beacon)
201 ieee802_11_set_beacons(hapd->iface);
202
203 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
204 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
4dc03726
JM
205 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
206 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
6fc6879b
JM
207
208 ieee802_1x_free_station(sta);
209 wpa_auth_sta_deinit(sta->wpa_sm);
210 rsn_preauth_free_station(hapd, sta);
74784010 211#ifndef CONFIG_NO_RADIUS
6fc6879b 212 radius_client_flush_auth(hapd->radius, sta->addr);
74784010 213#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
214
215 os_free(sta->last_assoc_req);
216 os_free(sta->challenge);
5d22a1d5
JM
217
218#ifdef CONFIG_IEEE80211W
93b76319
JM
219 os_free(sta->sa_query_trans_id);
220 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
5d22a1d5
JM
221#endif /* CONFIG_IEEE80211W */
222
8ccbe415
JM
223#ifdef CONFIG_P2P
224 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
225#endif /* CONFIG_P2P */
226
eb76b7e3 227 wpabuf_free(sta->wps_ie);
b305c684 228 wpabuf_free(sta->p2p_ie);
eb76b7e3 229
df84268a 230 os_free(sta->ht_capabilities);
05ab9712 231 os_free(sta->psk);
df84268a 232
6fc6879b
JM
233 os_free(sta);
234}
235
236
237void hostapd_free_stas(struct hostapd_data *hapd)
238{
239 struct sta_info *sta, *prev;
240
241 sta = hapd->sta_list;
242
243 while (sta) {
244 prev = sta;
245 if (sta->flags & WLAN_STA_AUTH) {
246 mlme_deauthenticate_indication(
247 hapd, sta, WLAN_REASON_UNSPECIFIED);
248 }
249 sta = sta->next;
250 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
251 MAC2STR(prev->addr));
252 ap_free_sta(hapd, prev);
253 }
254}
255
256
1c6e69cc
JM
257/**
258 * ap_handle_timer - Per STA timer handler
259 * @eloop_ctx: struct hostapd_data *
260 * @timeout_ctx: struct sta_info *
261 *
262 * This function is called to check station activity and to remove inactive
263 * stations.
264 */
6fc6879b
JM
265void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
266{
267 struct hostapd_data *hapd = eloop_ctx;
268 struct sta_info *sta = timeout_ctx;
269 unsigned long next_time = 0;
270
271 if (sta->timeout_next == STA_REMOVE) {
272 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
273 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
274 "local deauth request");
275 ap_free_sta(hapd, sta);
276 return;
277 }
278
279 if ((sta->flags & WLAN_STA_ASSOC) &&
280 (sta->timeout_next == STA_NULLFUNC ||
281 sta->timeout_next == STA_DISASSOC)) {
282 int inactive_sec;
51e2a27a 283 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
6fc6879b 284 if (inactive_sec == -1) {
3ec1e902
JM
285 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
286 "Check inactivity: Could not "
d5674791 287 "get station info from kernel driver for "
24d75245 288 MACSTR, MAC2STR(sta->addr));
d5674791
JM
289 /*
290 * The driver may not support this functionality.
291 * Anyway, try again after the next inactivity timeout,
292 * but do not disconnect the station now.
293 */
294 next_time = hapd->conf->ap_max_inactivity;
6fc6879b
JM
295 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
296 sta->flags & WLAN_STA_ASSOC) {
297 /* station activity detected; reset timeout state */
3ec1e902
JM
298 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
299 "Station " MACSTR " has been active %is ago",
24d75245 300 MAC2STR(sta->addr), inactive_sec);
6fc6879b
JM
301 sta->timeout_next = STA_NULLFUNC;
302 next_time = hapd->conf->ap_max_inactivity -
303 inactive_sec;
24d75245 304 } else {
3ec1e902
JM
305 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
306 "Station " MACSTR " has been "
24d75245
BG
307 "inactive too long: %d sec, max allowed: %d",
308 MAC2STR(sta->addr), inactive_sec,
309 hapd->conf->ap_max_inactivity);
ef01fa7b
YAP
310
311 if (hapd->conf->skip_inactivity_poll)
312 sta->timeout_next = STA_DISASSOC;
6fc6879b
JM
313 }
314 }
315
316 if ((sta->flags & WLAN_STA_ASSOC) &&
317 sta->timeout_next == STA_DISASSOC &&
ef01fa7b
YAP
318 !(sta->flags & WLAN_STA_PENDING_POLL) &&
319 !hapd->conf->skip_inactivity_poll) {
3ec1e902
JM
320 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
321 " has ACKed data poll", MAC2STR(sta->addr));
6fc6879b
JM
322 /* data nullfunc frame poll did not produce TX errors; assume
323 * station ACKed it */
324 sta->timeout_next = STA_NULLFUNC;
325 next_time = hapd->conf->ap_max_inactivity;
326 }
327
328 if (next_time) {
329 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
330 sta);
331 return;
332 }
333
334 if (sta->timeout_next == STA_NULLFUNC &&
335 (sta->flags & WLAN_STA_ASSOC)) {
bcf24348 336 wpa_printf(MSG_DEBUG, " Polling STA");
6fc6879b 337 sta->flags |= WLAN_STA_PENDING_POLL;
bcf24348
JB
338 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
339 sta->flags & WLAN_STA_WMM);
6fc6879b
JM
340 } else if (sta->timeout_next != STA_REMOVE) {
341 int deauth = sta->timeout_next == STA_DEAUTH;
342
afcc9ea1
BG
343 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
344 "Timeout, sending %s info to STA " MACSTR,
345 deauth ? "deauthentication" : "disassociation",
346 MAC2STR(sta->addr));
6fc6879b
JM
347
348 if (deauth) {
51e2a27a
JM
349 hostapd_drv_sta_deauth(
350 hapd, sta->addr,
351 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b 352 } else {
51e2a27a 353 hostapd_drv_sta_disassoc(
6fc6879b
JM
354 hapd, sta->addr,
355 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
356 }
357 }
358
359 switch (sta->timeout_next) {
360 case STA_NULLFUNC:
361 sta->timeout_next = STA_DISASSOC;
362 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
363 hapd, sta);
364 break;
365 case STA_DISASSOC:
ae055af4 366 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b
JM
367 sta->flags &= ~WLAN_STA_ASSOC;
368 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
369 if (!sta->acct_terminate_cause)
370 sta->acct_terminate_cause =
371 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
372 accounting_sta_stop(hapd, sta);
373 ieee802_1x_free_station(sta);
374 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
375 HOSTAPD_LEVEL_INFO, "disassociated due to "
376 "inactivity");
377 sta->timeout_next = STA_DEAUTH;
378 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
379 hapd, sta);
380 mlme_disassociate_indication(
381 hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
382 break;
383 case STA_DEAUTH:
384 case STA_REMOVE:
385 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
386 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
afcc9ea1 387 "inactivity (timer DEAUTH/REMOVE)");
6fc6879b
JM
388 if (!sta->acct_terminate_cause)
389 sta->acct_terminate_cause =
390 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
391 mlme_deauthenticate_indication(
392 hapd, sta,
393 WLAN_REASON_PREV_AUTH_NOT_VALID);
394 ap_free_sta(hapd, sta);
395 break;
396 }
397}
398
399
400static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
401{
402 struct hostapd_data *hapd = eloop_ctx;
403 struct sta_info *sta = timeout_ctx;
404 u8 addr[ETH_ALEN];
405
406 if (!(sta->flags & WLAN_STA_AUTH))
407 return;
408
409 mlme_deauthenticate_indication(hapd, sta,
410 WLAN_REASON_PREV_AUTH_NOT_VALID);
411 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
412 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
413 "session timeout");
414 sta->acct_terminate_cause =
415 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
416 os_memcpy(addr, sta->addr, ETH_ALEN);
417 ap_free_sta(hapd, sta);
51e2a27a 418 hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b
JM
419}
420
421
422void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
423 u32 session_timeout)
424{
425 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
426 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
427 "seconds", session_timeout);
428 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
429 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
430 hapd, sta);
431}
432
433
434void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
435{
436 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
437}
438
439
440struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
441{
442 struct sta_info *sta;
443
444 sta = ap_get_sta(hapd, addr);
445 if (sta)
446 return sta;
447
448 wpa_printf(MSG_DEBUG, " New STA");
449 if (hapd->num_sta >= hapd->conf->max_num_sta) {
450 /* FIX: might try to remove some old STAs first? */
451 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
452 hapd->num_sta, hapd->conf->max_num_sta);
453 return NULL;
454 }
455
456 sta = os_zalloc(sizeof(struct sta_info));
457 if (sta == NULL) {
458 wpa_printf(MSG_ERROR, "malloc failed");
459 return NULL;
460 }
5843e1c9 461 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
6fc6879b
JM
462
463 /* initialize STA info data */
464 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
465 ap_handle_timer, hapd, sta);
466 os_memcpy(sta->addr, addr, ETH_ALEN);
467 sta->next = hapd->sta_list;
468 hapd->sta_list = sta;
469 hapd->num_sta++;
470 ap_sta_hash_add(hapd, sta);
471 sta->ssid = &hapd->conf->ssid;
53f3d6f3 472 ap_sta_remove_in_other_bss(hapd, sta);
6fc6879b
JM
473
474 return sta;
475}
476
477
478static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
479{
480 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
481
482 wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
483 MAC2STR(sta->addr));
51e2a27a 484 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
6fc6879b
JM
485 sta->flags & WLAN_STA_ASSOC) {
486 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
487 " from kernel driver.", MAC2STR(sta->addr));
488 return -1;
489 }
490 return 0;
491}
492
493
53f3d6f3
FF
494static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
495 struct sta_info *sta)
6fc6879b
JM
496{
497 struct hostapd_iface *iface = hapd->iface;
498 size_t i;
499
500 for (i = 0; i < iface->num_bss; i++) {
501 struct hostapd_data *bss = iface->bss[i];
502 struct sta_info *sta2;
503 /* bss should always be set during operation, but it may be
504 * NULL during reconfiguration. Assume the STA is not
505 * associated to another BSS in that case to avoid NULL pointer
506 * dereferences. */
507 if (bss == hapd || bss == NULL)
508 continue;
509 sta2 = ap_get_sta(bss, sta->addr);
53f3d6f3
FF
510 if (!sta2)
511 continue;
6fc6879b 512
53f3d6f3
FF
513 ap_sta_disconnect(bss, sta2, sta2->addr,
514 WLAN_REASON_PREV_AUTH_NOT_VALID);
515 }
6fc6879b
JM
516}
517
518
4dc03726
JM
519static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
520{
521 struct hostapd_data *hapd = eloop_ctx;
522 struct sta_info *sta = timeout_ctx;
523
524 ap_sta_remove(hapd, sta);
525 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
526}
527
528
6fc6879b
JM
529void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
530 u16 reason)
531{
532 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
533 hapd->conf->iface, MAC2STR(sta->addr));
534 sta->flags &= ~WLAN_STA_ASSOC;
4dc03726 535 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b
JM
536 sta->timeout_next = STA_DEAUTH;
537 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
538 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
539 ap_handle_timer, hapd, sta);
540 accounting_sta_stop(hapd, sta);
541 ieee802_1x_free_station(sta);
542
4dc03726 543 sta->disassoc_reason = reason;
cc28ad8c 544 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
4dc03726
JM
545 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
546 eloop_register_timeout(hapd->iface->drv_flags &
547 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
548 ap_sta_disassoc_cb_timeout, hapd, sta);
549}
550
551
552static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
553{
554 struct hostapd_data *hapd = eloop_ctx;
555 struct sta_info *sta = timeout_ctx;
556
557 ap_sta_remove(hapd, sta);
558 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
6fc6879b
JM
559}
560
561
562void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
563 u16 reason)
564{
565 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
566 hapd->conf->iface, MAC2STR(sta->addr));
567 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
4dc03726 568 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b
JM
569 sta->timeout_next = STA_REMOVE;
570 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
571 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
572 ap_handle_timer, hapd, sta);
573 accounting_sta_stop(hapd, sta);
574 ieee802_1x_free_station(sta);
575
4dc03726 576 sta->deauth_reason = reason;
cc28ad8c 577 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
578 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
579 eloop_register_timeout(hapd->iface->drv_flags &
580 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
581 ap_sta_deauth_cb_timeout, hapd, sta);
6fc6879b
JM
582}
583
584
585int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
586 int old_vlanid)
587{
30b32314 588#ifndef CONFIG_NO_VLAN
6fc6879b
JM
589 const char *iface;
590 struct hostapd_vlan *vlan = NULL;
4254100d 591 int ret;
6fc6879b
JM
592
593 /*
594 * Do not proceed furthur if the vlan id remains same. We do not want
595 * duplicate dynamic vlan entries.
596 */
597 if (sta->vlan_id == old_vlanid)
598 return 0;
599
600 /*
601 * During 1x reauth, if the vlan id changes, then remove the old id and
602 * proceed furthur to add the new one.
603 */
604 if (old_vlanid > 0)
605 vlan_remove_dynamic(hapd, old_vlanid);
606
607 iface = hapd->conf->iface;
608 if (sta->ssid->vlan[0])
609 iface = sta->ssid->vlan;
610
611 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
612 sta->vlan_id = 0;
613 else if (sta->vlan_id > 0) {
614 vlan = hapd->conf->vlan;
615 while (vlan) {
616 if (vlan->vlan_id == sta->vlan_id ||
617 vlan->vlan_id == VLAN_ID_WILDCARD) {
618 iface = vlan->ifname;
619 break;
620 }
621 vlan = vlan->next;
622 }
623 }
624
625 if (sta->vlan_id > 0 && vlan == NULL) {
626 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
627 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
628 "binding station to (vlan_id=%d)",
629 sta->vlan_id);
630 return -1;
631 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
632 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
633 if (vlan == NULL) {
634 hostapd_logger(hapd, sta->addr,
635 HOSTAPD_MODULE_IEEE80211,
636 HOSTAPD_LEVEL_DEBUG, "could not add "
637 "dynamic VLAN interface for vlan_id=%d",
638 sta->vlan_id);
639 return -1;
640 }
641
642 iface = vlan->ifname;
643 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
644 hostapd_logger(hapd, sta->addr,
645 HOSTAPD_MODULE_IEEE80211,
646 HOSTAPD_LEVEL_DEBUG, "could not "
647 "configure encryption for dynamic VLAN "
648 "interface for vlan_id=%d",
649 sta->vlan_id);
650 }
651
652 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
653 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
654 "interface '%s'", iface);
655 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
656 if (sta->vlan_id > 0) {
657 vlan->dynamic_vlan++;
658 hostapd_logger(hapd, sta->addr,
659 HOSTAPD_MODULE_IEEE80211,
660 HOSTAPD_LEVEL_DEBUG, "updated existing "
661 "dynamic VLAN interface '%s'", iface);
662 }
663
664 /*
665 * Update encryption configuration for statically generated
666 * VLAN interface. This is only used for static WEP
667 * configuration for the case where hostapd did not yet know
668 * which keys are to be used when the interface was added.
669 */
670 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
671 hostapd_logger(hapd, sta->addr,
672 HOSTAPD_MODULE_IEEE80211,
673 HOSTAPD_LEVEL_DEBUG, "could not "
674 "configure encryption for VLAN "
675 "interface for vlan_id=%d",
676 sta->vlan_id);
677 }
678 }
679
680 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
681 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
682 "'%s'", iface);
683
684 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
685 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
686
51e2a27a 687 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
4254100d
JM
688 if (ret < 0) {
689 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
690 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
691 "entry to vlan_id=%d", sta->vlan_id);
692 }
693 return ret;
30b32314
JM
694#else /* CONFIG_NO_VLAN */
695 return 0;
696#endif /* CONFIG_NO_VLAN */
6fc6879b 697}
5d22a1d5
JM
698
699
700#ifdef CONFIG_IEEE80211W
701
45c94154 702int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 703{
45c94154
JM
704 u32 tu;
705 struct os_time now, passed;
706 os_get_time(&now);
707 os_time_sub(&now, &sta->sa_query_start, &passed);
708 tu = (passed.sec * 1000000 + passed.usec) / 1024;
709 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
710 hostapd_logger(hapd, sta->addr,
711 HOSTAPD_MODULE_IEEE80211,
5d22a1d5 712 HOSTAPD_LEVEL_DEBUG,
93b76319
JM
713 "association SA Query timed out");
714 sta->sa_query_timed_out = 1;
715 os_free(sta->sa_query_trans_id);
716 sta->sa_query_trans_id = NULL;
717 sta->sa_query_count = 0;
45c94154
JM
718 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
719 return 1;
5d22a1d5
JM
720 }
721
45c94154
JM
722 return 0;
723}
724
725
726static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
727{
728 struct hostapd_data *hapd = eloop_ctx;
729 struct sta_info *sta = timeout_ctx;
730 unsigned int timeout, sec, usec;
731 u8 *trans_id, *nbuf;
732
733 if (sta->sa_query_count > 0 &&
734 ap_check_sa_query_timeout(hapd, sta))
735 return;
736
93b76319
JM
737 nbuf = os_realloc(sta->sa_query_trans_id,
738 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5
JM
739 if (nbuf == NULL)
740 return;
45c94154
JM
741 if (sta->sa_query_count == 0) {
742 /* Starting a new SA Query procedure */
743 os_get_time(&sta->sa_query_start);
744 }
93b76319
JM
745 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
746 sta->sa_query_trans_id = nbuf;
747 sta->sa_query_count++;
5d22a1d5 748
93b76319 749 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5 750
45c94154
JM
751 timeout = hapd->conf->assoc_sa_query_retry_timeout;
752 sec = ((timeout / 1000) * 1024) / 1000;
753 usec = (timeout % 1000) * 1024;
754 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
755
5d22a1d5
JM
756 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
757 HOSTAPD_LEVEL_DEBUG,
93b76319 758 "association SA Query attempt %d", sta->sa_query_count);
5d22a1d5 759
fe6bdb77 760#ifdef NEED_AP_MLME
93b76319 761 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
fe6bdb77 762#endif /* NEED_AP_MLME */
5d22a1d5
JM
763}
764
765
93b76319 766void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 767{
93b76319 768 ap_sa_query_timer(hapd, sta);
5d22a1d5
JM
769}
770
771
93b76319 772void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 773{
93b76319
JM
774 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
775 os_free(sta->sa_query_trans_id);
776 sta->sa_query_trans_id = NULL;
777 sta->sa_query_count = 0;
5d22a1d5
JM
778}
779
780#endif /* CONFIG_IEEE80211W */
45cefa0b
JM
781
782
6905dcb1
JB
783void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
784 int authorized)
785{
10cc6c88 786 const u8 *dev_addr = NULL;
6905dcb1
JB
787 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
788 return;
789
ae055af4 790#ifdef CONFIG_P2P
10cc6c88 791 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
ae055af4 792#endif /* CONFIG_P2P */
10cc6c88
JJ
793
794 if (authorized) {
ae055af4
JM
795 if (dev_addr)
796 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
10cc6c88 797 MACSTR " p2p_dev_addr=" MACSTR,
ae055af4
JM
798 MAC2STR(sta->addr), MAC2STR(dev_addr));
799 else
800 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
801 MACSTR, MAC2STR(sta->addr));
8a5e75f6
DS
802 if (hapd->msg_ctx_parent &&
803 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
804 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
10cc6c88
JJ
805 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
806 MACSTR,
8a5e75f6
DS
807 MAC2STR(sta->addr), MAC2STR(dev_addr));
808 else if (hapd->msg_ctx_parent &&
809 hapd->msg_ctx_parent != hapd->msg_ctx)
810 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
811 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
ae055af4 812
6905dcb1 813 sta->flags |= WLAN_STA_AUTHORIZED;
ae055af4 814 } else {
10cc6c88
JJ
815 if (dev_addr)
816 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
817 MACSTR " p2p_dev_addr=" MACSTR,
818 MAC2STR(sta->addr), MAC2STR(dev_addr));
819 else
820 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
821 MACSTR, MAC2STR(sta->addr));
8a5e75f6 822 if (hapd->msg_ctx_parent &&
10cc6c88
JJ
823 hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
824 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
825 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
826 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
827 else if (hapd->msg_ctx_parent &&
828 hapd->msg_ctx_parent != hapd->msg_ctx)
8a5e75f6
DS
829 wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
830 AP_STA_DISCONNECTED MACSTR,
831 MAC2STR(sta->addr));
6905dcb1 832 sta->flags &= ~WLAN_STA_AUTHORIZED;
ae055af4 833 }
0661eed2
JB
834
835 if (hapd->sta_authorized_cb)
836 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
fbdcfd57 837 sta->addr, authorized, dev_addr);
6905dcb1
JB
838}
839
840
45cefa0b
JM
841void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
842 const u8 *addr, u16 reason)
843{
844
845 if (sta == NULL && addr)
846 sta = ap_get_sta(hapd, addr);
847
848 if (addr)
51e2a27a 849 hostapd_drv_sta_deauth(hapd, addr, reason);
45cefa0b
JM
850
851 if (sta == NULL)
852 return;
6905dcb1 853 ap_sta_set_authorized(hapd, sta, 0);
ceb997f3
JM
854 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
855 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
6905dcb1 856 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
45cefa0b 857 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4dc03726
JM
858 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
859 ap_handle_timer, hapd, sta);
45cefa0b 860 sta->timeout_next = STA_REMOVE;
4dc03726
JM
861
862 sta->deauth_reason = reason;
cc28ad8c 863 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
864 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
865 eloop_register_timeout(hapd->iface->drv_flags &
866 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
867 ap_sta_deauth_cb_timeout, hapd, sta);
868}
869
870
871void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
872{
cc28ad8c
JM
873 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
874 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
875 return;
876 }
877 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
878 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
879 ap_sta_deauth_cb_timeout(hapd, sta);
880}
881
882
883void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
884{
cc28ad8c
JM
885 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
886 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
887 return;
888 }
889 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
890 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
891 ap_sta_disassoc_cb_timeout(hapd, sta);
45cefa0b 892}