]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/sta_info.c
AP: Expose PMK outside of wpa_auth module
[thirdparty/hostap.git] / src / ap / sta_info.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / Station table
09368515 3 * Copyright (c) 2002-2017, Jouni Malinen <j@w1.fi>
6fc6879b 4 *
0f3d578e
JM
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
6fc6879b
JM
7 */
8
6226e38d 9#include "utils/includes.h"
6fc6879b 10
6226e38d
JM
11#include "utils/common.h"
12#include "utils/eloop.h"
81f4f619 13#include "common/ieee802_11_defs.h"
6caaae1e 14#include "common/wpa_ctrl.h"
a46d72d7 15#include "common/sae.h"
bdee6fce
JM
16#include "radius/radius.h"
17#include "radius/radius_client.h"
8ccbe415 18#include "p2p/p2p.h"
6959145b 19#include "fst/fst.h"
09368515 20#include "crypto/crypto.h"
6fc6879b 21#include "hostapd.h"
6fc6879b
JM
22#include "accounting.h"
23#include "ieee802_1x.h"
24#include "ieee802_11.h"
f2a14be7 25#include "ieee802_11_auth.h"
6226e38d
JM
26#include "wpa_auth.h"
27#include "preauth_auth.h"
28#include "ap_config.h"
6fc6879b 29#include "beacon.h"
6226e38d 30#include "ap_mlme.h"
6fc6879b 31#include "vlan_init.h"
aefb53bd 32#include "p2p_hostapd.h"
cee7d66b 33#include "ap_drv_ops.h"
dca30c3f 34#include "gas_serv.h"
97596f8e 35#include "wnm_ap.h"
ca911d61 36#include "mbo_ap.h"
bd00c431 37#include "ndisc_snoop.h"
6226e38d 38#include "sta_info.h"
1889af2e 39#include "vlan.h"
0f5eb69f 40#include "wps_hostapd.h"
6fc6879b 41
53f3d6f3
FF
42static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
43 struct sta_info *sta);
6fc6879b 44static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
97596f8e 45static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
4dc03726
JM
46static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
47static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 48#ifdef CONFIG_IEEE80211W
93b76319 49static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 50#endif /* CONFIG_IEEE80211W */
4dc03726 51static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
0f5eb69f 52static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx);
6fc6879b
JM
53
54int ap_for_each_sta(struct hostapd_data *hapd,
55 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
56 void *ctx),
57 void *ctx)
58{
59 struct sta_info *sta;
60
61 for (sta = hapd->sta_list; sta; sta = sta->next) {
62 if (cb(hapd, sta, ctx))
63 return 1;
64 }
65
66 return 0;
67}
68
69
70struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
71{
72 struct sta_info *s;
73
74 s = hapd->sta_hash[STA_HASH(sta)];
75 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
76 s = s->hnext;
77 return s;
78}
79
80
f2c56602
JM
81#ifdef CONFIG_P2P
82struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
83{
84 struct sta_info *sta;
85
86 for (sta = hapd->sta_list; sta; sta = sta->next) {
87 const u8 *p2p_dev_addr;
88
89 if (sta->p2p_ie == NULL)
90 continue;
91
92 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
93 if (p2p_dev_addr == NULL)
94 continue;
95
96 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
97 return sta;
98 }
99
100 return NULL;
101}
102#endif /* CONFIG_P2P */
103
104
6fc6879b
JM
105static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
106{
107 struct sta_info *tmp;
108
109 if (hapd->sta_list == sta) {
110 hapd->sta_list = sta->next;
111 return;
112 }
113
114 tmp = hapd->sta_list;
115 while (tmp != NULL && tmp->next != sta)
116 tmp = tmp->next;
117 if (tmp == NULL) {
118 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
119 "list.", MAC2STR(sta->addr));
120 } else
121 tmp->next = sta->next;
122}
123
124
125void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
126{
127 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
128 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
129}
130
131
132static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
133{
134 struct sta_info *s;
135
136 s = hapd->sta_hash[STA_HASH(sta->addr)];
137 if (s == NULL) return;
138 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
139 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
140 return;
141 }
142
143 while (s->hnext != NULL &&
144 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
145 s = s->hnext;
146 if (s->hnext != NULL)
147 s->hnext = s->hnext->hnext;
148 else
149 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
150 " from hash table", MAC2STR(sta->addr));
151}
152
153
bd00c431
KP
154void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
155{
156 sta_ip6addr_del(hapd, sta);
157}
158
159
6fc6879b
JM
160void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
161{
162 int set_beacon = 0;
163
164 accounting_sta_stop(hapd, sta);
165
6905dcb1
JB
166 /* just in case */
167 ap_sta_set_authorized(hapd, sta, 0);
168
9c06f0f6 169 if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP))
69dd2967 170 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
53f3d6f3 171
7d597d46 172 if (sta->ipaddr)
ed4ddb6d 173 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
bd00c431 174 ap_sta_ip6addr_del(hapd, sta);
7d597d46 175
354c903f 176 if (!hapd->iface->driver_ap_teardown &&
bb598c3b 177 !(sta->flags & WLAN_STA_PREAUTH)) {
51e2a27a 178 hostapd_drv_sta_remove(hapd, sta->addr);
bb598c3b
AB
179 sta->added_unassoc = 0;
180 }
6fc6879b
JM
181
182 ap_sta_hash_del(hapd, sta);
183 ap_sta_list_del(hapd, sta);
184
185 if (sta->aid > 0)
2991469c
JM
186 hapd->sta_aid[(sta->aid - 1) / 32] &=
187 ~BIT((sta->aid - 1) % 32);
6fc6879b
JM
188
189 hapd->num_sta--;
190 if (sta->nonerp_set) {
191 sta->nonerp_set = 0;
192 hapd->iface->num_sta_non_erp--;
193 if (hapd->iface->num_sta_non_erp == 0)
194 set_beacon++;
195 }
196
197 if (sta->no_short_slot_time_set) {
198 sta->no_short_slot_time_set = 0;
199 hapd->iface->num_sta_no_short_slot_time--;
28d12641
AKP
200 if (hapd->iface->current_mode &&
201 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
6fc6879b
JM
202 && hapd->iface->num_sta_no_short_slot_time == 0)
203 set_beacon++;
204 }
205
206 if (sta->no_short_preamble_set) {
207 sta->no_short_preamble_set = 0;
208 hapd->iface->num_sta_no_short_preamble--;
28d12641
AKP
209 if (hapd->iface->current_mode &&
210 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
6fc6879b
JM
211 && hapd->iface->num_sta_no_short_preamble == 0)
212 set_beacon++;
213 }
214
e8ff1e59
JM
215 if (sta->no_ht_gf_set) {
216 sta->no_ht_gf_set = 0;
217 hapd->iface->num_sta_ht_no_gf--;
218 }
219
220 if (sta->no_ht_set) {
221 sta->no_ht_set = 0;
de9289c8 222 hapd->iface->num_sta_no_ht--;
e8ff1e59
JM
223 }
224
225 if (sta->ht_20mhz_set) {
226 sta->ht_20mhz_set = 0;
227 hapd->iface->num_sta_ht_20mhz--;
228 }
de9289c8 229
04059ab8
DG
230#ifdef CONFIG_TAXONOMY
231 wpabuf_free(sta->probe_ie_taxonomy);
232 sta->probe_ie_taxonomy = NULL;
233 wpabuf_free(sta->assoc_ie_taxonomy);
234 sta->assoc_ie_taxonomy = NULL;
235#endif /* CONFIG_TAXONOMY */
236
9c47f6a2
PX
237#ifdef CONFIG_IEEE80211N
238 ht40_intolerant_remove(hapd->iface, sta);
239#endif /* CONFIG_IEEE80211N */
240
aefb53bd
JM
241#ifdef CONFIG_P2P
242 if (sta->no_p2p_set) {
243 sta->no_p2p_set = 0;
244 hapd->num_sta_no_p2p--;
245 if (hapd->num_sta_no_p2p == 0)
246 hostapd_p2p_non_p2p_sta_disconnected(hapd);
247 }
248#endif /* CONFIG_P2P */
249
d45354be 250#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
de9289c8
JM
251 if (hostapd_ht_operation_update(hapd->iface) > 0)
252 set_beacon++;
d45354be 253#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
de9289c8 254
c596f3f0
CYY
255#ifdef CONFIG_MESH
256 if (hapd->mesh_sta_free_cb)
9684c756 257 hapd->mesh_sta_free_cb(hapd, sta);
c596f3f0
CYY
258#endif /* CONFIG_MESH */
259
6fc6879b
JM
260 if (set_beacon)
261 ieee802_11_set_beacons(hapd->iface);
262
42ca9845
JM
263 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
264 __func__, MAC2STR(sta->addr));
6fc6879b
JM
265 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
266 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
97596f8e 267 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
9e8fde21 268 ap_sta_clear_disconnect_timeouts(hapd, sta);
f3b8ad4d 269 sae_clear_retransmit_timer(hapd, sta);
6fc6879b 270
d7c3347f 271 ieee802_1x_free_station(hapd, sta);
6fc6879b
JM
272 wpa_auth_sta_deinit(sta->wpa_sm);
273 rsn_preauth_free_station(hapd, sta);
74784010 274#ifndef CONFIG_NO_RADIUS
ded22b53
HS
275 if (hapd->radius)
276 radius_client_flush_auth(hapd->radius, sta->addr);
74784010 277#endif /* CONFIG_NO_RADIUS */
6fc6879b 278
7cebc8e2
MB
279#ifndef CONFIG_NO_VLAN
280 /*
281 * sta->wpa_sm->group needs to be released before so that
282 * vlan_remove_dynamic() can check that no stations are left on the
283 * AP_VLAN netdev.
284 */
1889af2e
MB
285 if (sta->vlan_id)
286 vlan_remove_dynamic(hapd, sta->vlan_id);
7cebc8e2
MB
287 if (sta->vlan_id_bound) {
288 /*
289 * Need to remove the STA entry before potentially removing the
290 * VLAN.
291 */
292 if (hapd->iface->driver_ap_teardown &&
bb598c3b 293 !(sta->flags & WLAN_STA_PREAUTH)) {
7cebc8e2 294 hostapd_drv_sta_remove(hapd, sta->addr);
bb598c3b
AB
295 sta->added_unassoc = 0;
296 }
7cebc8e2
MB
297 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
298 }
299#endif /* CONFIG_NO_VLAN */
300
6fc6879b 301 os_free(sta->challenge);
5d22a1d5
JM
302
303#ifdef CONFIG_IEEE80211W
93b76319
JM
304 os_free(sta->sa_query_trans_id);
305 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
5d22a1d5
JM
306#endif /* CONFIG_IEEE80211W */
307
8ccbe415
JM
308#ifdef CONFIG_P2P
309 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
310#endif /* CONFIG_P2P */
311
dca30c3f
JK
312#ifdef CONFIG_INTERWORKING
313 if (sta->gas_dialog) {
314 int i;
315 for (i = 0; i < GAS_DIALOG_MAX; i++)
316 gas_serv_dialog_clear(&sta->gas_dialog[i]);
317 os_free(sta->gas_dialog);
318 }
319#endif /* CONFIG_INTERWORKING */
320
eb76b7e3 321 wpabuf_free(sta->wps_ie);
b305c684 322 wpabuf_free(sta->p2p_ie);
f403dcd6 323 wpabuf_free(sta->hs20_ie);
67cca346 324 wpabuf_free(sta->roaming_consortium);
ae667c08
AN
325#ifdef CONFIG_FST
326 wpabuf_free(sta->mb_ies);
327#endif /* CONFIG_FST */
eb76b7e3 328
df84268a 329 os_free(sta->ht_capabilities);
cc14091e 330 os_free(sta->vht_capabilities);
ad20a136 331 os_free(sta->vht_operation);
f2a14be7 332 hostapd_free_psk_list(sta->psk);
2092597f
MB
333 os_free(sta->identity);
334 os_free(sta->radius_cui);
6ca0853d 335 os_free(sta->remediation_url);
d4e39c51 336 os_free(sta->t_c_url);
8e1146d9 337 wpabuf_free(sta->hs20_deauth_req);
97596f8e 338 os_free(sta->hs20_session_info_url);
df84268a 339
98efcc41 340#ifdef CONFIG_SAE
a46d72d7 341 sae_clear_data(sta->sae);
98efcc41
JM
342 os_free(sta->sae);
343#endif /* CONFIG_SAE */
344
ca911d61 345 mbo_ap_sta_free(sta);
adf0478e 346 os_free(sta->supp_op_classes);
ca911d61 347
91d91abf
JM
348#ifdef CONFIG_FILS
349 os_free(sta->fils_pending_assoc_req);
350 wpabuf_free(sta->fils_hlp_resp);
351 wpabuf_free(sta->hlp_dhcp_discover);
352 eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
1764559e
JM
353#ifdef CONFIG_FILS_SK_PFS
354 crypto_ecdh_deinit(sta->fils_ecdh);
355 wpabuf_clear_free(sta->fils_dh_ss);
80ddf5d9 356 wpabuf_free(sta->fils_g_sta);
1764559e 357#endif /* CONFIG_FILS_SK_PFS */
91d91abf
JM
358#endif /* CONFIG_FILS */
359
09368515 360#ifdef CONFIG_OWE
7a12edd1 361 bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
09368515
JM
362 crypto_ecdh_deinit(sta->owe_ecdh);
363#endif /* CONFIG_OWE */
364
65f9db6b 365 os_free(sta->ext_capability);
366
d58c3bd8
RM
367#ifdef CONFIG_WNM_AP
368 eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
369#endif /* CONFIG_WNM_AP */
370
1952b626
BP
371 os_free(sta->ifname_wds);
372
6fc6879b
JM
373 os_free(sta);
374}
375
376
377void hostapd_free_stas(struct hostapd_data *hapd)
378{
379 struct sta_info *sta, *prev;
380
381 sta = hapd->sta_list;
382
383 while (sta) {
384 prev = sta;
385 if (sta->flags & WLAN_STA_AUTH) {
386 mlme_deauthenticate_indication(
387 hapd, sta, WLAN_REASON_UNSPECIFIED);
388 }
389 sta = sta->next;
390 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
391 MAC2STR(prev->addr));
392 ap_free_sta(hapd, prev);
393 }
394}
395
396
1c6e69cc
JM
397/**
398 * ap_handle_timer - Per STA timer handler
399 * @eloop_ctx: struct hostapd_data *
400 * @timeout_ctx: struct sta_info *
401 *
402 * This function is called to check station activity and to remove inactive
403 * stations.
404 */
6fc6879b
JM
405void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
406{
407 struct hostapd_data *hapd = eloop_ctx;
408 struct sta_info *sta = timeout_ctx;
409 unsigned long next_time = 0;
d5b559b6 410 int reason;
6fc6879b 411
03269d55
JM
412 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
413 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
42ca9845 414 sta->timeout_next);
6fc6879b
JM
415 if (sta->timeout_next == STA_REMOVE) {
416 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
417 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
418 "local deauth request");
419 ap_free_sta(hapd, sta);
420 return;
421 }
422
423 if ((sta->flags & WLAN_STA_ASSOC) &&
424 (sta->timeout_next == STA_NULLFUNC ||
425 sta->timeout_next == STA_DISASSOC)) {
426 int inactive_sec;
ce28e279
BG
427 /*
428 * Add random value to timeout so that we don't end up bouncing
429 * all stations at the same time if we have lots of associated
430 * stations that are idle (but keep re-associating).
431 */
432 int fuzz = os_random() % 20;
51e2a27a 433 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
6fc6879b 434 if (inactive_sec == -1) {
3ec1e902
JM
435 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
436 "Check inactivity: Could not "
d5674791 437 "get station info from kernel driver for "
24d75245 438 MACSTR, MAC2STR(sta->addr));
d5674791
JM
439 /*
440 * The driver may not support this functionality.
441 * Anyway, try again after the next inactivity timeout,
442 * but do not disconnect the station now.
443 */
ce28e279 444 next_time = hapd->conf->ap_max_inactivity + fuzz;
b9749bac
MH
445 } else if (inactive_sec == -ENOENT) {
446 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
447 "Station " MACSTR " has lost its driver entry",
448 MAC2STR(sta->addr));
449
47e5fbde
PO
450 /* Avoid sending client probe on removed client */
451 sta->timeout_next = STA_DISASSOC;
452 goto skip_poll;
a114c723 453 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
6fc6879b 454 /* station activity detected; reset timeout state */
3ec1e902
JM
455 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
456 "Station " MACSTR " has been active %is ago",
24d75245 457 MAC2STR(sta->addr), inactive_sec);
6fc6879b 458 sta->timeout_next = STA_NULLFUNC;
ce28e279 459 next_time = hapd->conf->ap_max_inactivity + fuzz -
6fc6879b 460 inactive_sec;
24d75245 461 } else {
3ec1e902
JM
462 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
463 "Station " MACSTR " has been "
24d75245
BG
464 "inactive too long: %d sec, max allowed: %d",
465 MAC2STR(sta->addr), inactive_sec,
466 hapd->conf->ap_max_inactivity);
ef01fa7b
YAP
467
468 if (hapd->conf->skip_inactivity_poll)
469 sta->timeout_next = STA_DISASSOC;
6fc6879b
JM
470 }
471 }
472
473 if ((sta->flags & WLAN_STA_ASSOC) &&
474 sta->timeout_next == STA_DISASSOC &&
ef01fa7b
YAP
475 !(sta->flags & WLAN_STA_PENDING_POLL) &&
476 !hapd->conf->skip_inactivity_poll) {
3ec1e902
JM
477 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
478 " has ACKed data poll", MAC2STR(sta->addr));
6fc6879b
JM
479 /* data nullfunc frame poll did not produce TX errors; assume
480 * station ACKed it */
481 sta->timeout_next = STA_NULLFUNC;
482 next_time = hapd->conf->ap_max_inactivity;
483 }
484
47e5fbde 485skip_poll:
6fc6879b 486 if (next_time) {
42ca9845
JM
487 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
488 "for " MACSTR " (%lu seconds)",
489 __func__, MAC2STR(sta->addr), next_time);
6fc6879b
JM
490 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
491 sta);
492 return;
493 }
494
495 if (sta->timeout_next == STA_NULLFUNC &&
496 (sta->flags & WLAN_STA_ASSOC)) {
bcf24348 497 wpa_printf(MSG_DEBUG, " Polling STA");
6fc6879b 498 sta->flags |= WLAN_STA_PENDING_POLL;
bcf24348
JB
499 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
500 sta->flags & WLAN_STA_WMM);
6fc6879b
JM
501 } else if (sta->timeout_next != STA_REMOVE) {
502 int deauth = sta->timeout_next == STA_DEAUTH;
503
afcc9ea1
BG
504 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
505 "Timeout, sending %s info to STA " MACSTR,
506 deauth ? "deauthentication" : "disassociation",
507 MAC2STR(sta->addr));
6fc6879b
JM
508
509 if (deauth) {
51e2a27a
JM
510 hostapd_drv_sta_deauth(
511 hapd, sta->addr,
512 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b 513 } else {
d5b559b6
KP
514 reason = (sta->timeout_next == STA_DISASSOC) ?
515 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
516 WLAN_REASON_PREV_AUTH_NOT_VALID;
517
518 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
6fc6879b
JM
519 }
520 }
521
522 switch (sta->timeout_next) {
523 case STA_NULLFUNC:
524 sta->timeout_next = STA_DISASSOC;
42ca9845
JM
525 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
526 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
527 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
6fc6879b
JM
528 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
529 hapd, sta);
530 break;
531 case STA_DISASSOC:
d5b559b6 532 case STA_DISASSOC_FROM_CLI:
ae055af4 533 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b
JM
534 sta->flags &= ~WLAN_STA_ASSOC;
535 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
536 if (!sta->acct_terminate_cause)
537 sta->acct_terminate_cause =
538 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
539 accounting_sta_stop(hapd, sta);
d7c3347f 540 ieee802_1x_free_station(hapd, sta);
6fc6879b
JM
541 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
542 HOSTAPD_LEVEL_INFO, "disassociated due to "
543 "inactivity");
d5b559b6
KP
544 reason = (sta->timeout_next == STA_DISASSOC) ?
545 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
546 WLAN_REASON_PREV_AUTH_NOT_VALID;
6fc6879b 547 sta->timeout_next = STA_DEAUTH;
42ca9845
JM
548 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
549 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
550 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
6fc6879b
JM
551 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
552 hapd, sta);
d5b559b6 553 mlme_disassociate_indication(hapd, sta, reason);
6fc6879b
JM
554 break;
555 case STA_DEAUTH:
556 case STA_REMOVE:
557 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
558 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
afcc9ea1 559 "inactivity (timer DEAUTH/REMOVE)");
6fc6879b
JM
560 if (!sta->acct_terminate_cause)
561 sta->acct_terminate_cause =
562 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
563 mlme_deauthenticate_indication(
564 hapd, sta,
565 WLAN_REASON_PREV_AUTH_NOT_VALID);
566 ap_free_sta(hapd, sta);
567 break;
568 }
569}
570
571
572static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
573{
574 struct hostapd_data *hapd = eloop_ctx;
575 struct sta_info *sta = timeout_ctx;
6fc6879b 576
03269d55
JM
577 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
578 hapd->conf->iface, MAC2STR(sta->addr));
dca30c3f
JK
579 if (!(sta->flags & WLAN_STA_AUTH)) {
580 if (sta->flags & WLAN_STA_GAS) {
581 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
582 "entry " MACSTR, MAC2STR(sta->addr));
583 ap_free_sta(hapd, sta);
584 }
6fc6879b 585 return;
dca30c3f 586 }
6fc6879b 587
0ac38766
JM
588 hostapd_drv_sta_deauth(hapd, sta->addr,
589 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b
JM
590 mlme_deauthenticate_indication(hapd, sta,
591 WLAN_REASON_PREV_AUTH_NOT_VALID);
592 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
593 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
594 "session timeout");
595 sta->acct_terminate_cause =
596 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
6fc6879b 597 ap_free_sta(hapd, sta);
6fc6879b
JM
598}
599
600
91f9e607
KP
601void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
602 u32 session_timeout)
603{
604 if (eloop_replenish_timeout(session_timeout, 0,
a09ffd5f 605 ap_handle_session_timer, hapd, sta) == 1) {
91f9e607
KP
606 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
607 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
608 "to %d seconds", session_timeout);
609 }
610}
611
612
6fc6879b
JM
613void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
614 u32 session_timeout)
615{
616 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
617 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
618 "seconds", session_timeout);
619 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
620 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
621 hapd, sta);
622}
623
624
625void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
626{
627 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
628}
629
630
97596f8e
JM
631static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
632{
b5bf84ba 633#ifdef CONFIG_WNM_AP
97596f8e
JM
634 struct hostapd_data *hapd = eloop_ctx;
635 struct sta_info *sta = timeout_ctx;
636
03269d55
JM
637 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
638 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
97596f8e
JM
639 if (sta->hs20_session_info_url == NULL)
640 return;
641
642 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
643 sta->hs20_disassoc_timer);
b5bf84ba 644#endif /* CONFIG_WNM_AP */
97596f8e
JM
645}
646
647
648void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
649 struct sta_info *sta, int warning_time)
650{
651 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
652 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
653 hapd, sta);
654}
655
656
6fc6879b
JM
657struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
658{
659 struct sta_info *sta;
660
661 sta = ap_get_sta(hapd, addr);
662 if (sta)
663 return sta;
664
665 wpa_printf(MSG_DEBUG, " New STA");
666 if (hapd->num_sta >= hapd->conf->max_num_sta) {
667 /* FIX: might try to remove some old STAs first? */
668 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
669 hapd->num_sta, hapd->conf->max_num_sta);
670 return NULL;
671 }
672
673 sta = os_zalloc(sizeof(struct sta_info));
674 if (sta == NULL) {
675 wpa_printf(MSG_ERROR, "malloc failed");
676 return NULL;
677 }
5843e1c9 678 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
d72a0053
NL
679 if (accounting_sta_get_id(hapd, sta) < 0) {
680 os_free(sta);
681 return NULL;
682 }
6fc6879b 683
336167c8
MSS
684 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
685 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
686 "for " MACSTR " (%d seconds - ap_max_inactivity)",
687 __func__, MAC2STR(addr),
688 hapd->conf->ap_max_inactivity);
689 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
690 ap_handle_timer, hapd, sta);
691 }
692
6fc6879b 693 /* initialize STA info data */
6fc6879b
JM
694 os_memcpy(sta->addr, addr, ETH_ALEN);
695 sta->next = hapd->sta_list;
696 hapd->sta_list = sta;
697 hapd->num_sta++;
698 ap_sta_hash_add(hapd, sta);
53f3d6f3 699 ap_sta_remove_in_other_bss(hapd, sta);
38cb0a2d 700 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
bd00c431 701 dl_list_init(&sta->ip6addr);
6fc6879b 702
44281940
DG
703#ifdef CONFIG_TAXONOMY
704 sta_track_claim_taxonomy_info(hapd->iface, addr,
705 &sta->probe_ie_taxonomy);
706#endif /* CONFIG_TAXONOMY */
707
6fc6879b
JM
708 return sta;
709}
710
711
712static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
713{
714 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
715
7d597d46 716 if (sta->ipaddr)
ed4ddb6d 717 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
bd00c431 718 ap_sta_ip6addr_del(hapd, sta);
7d597d46 719
03269d55
JM
720 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
721 hapd->conf->iface, MAC2STR(sta->addr));
51e2a27a 722 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
6fc6879b 723 sta->flags & WLAN_STA_ASSOC) {
03269d55
JM
724 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
725 " from kernel driver",
726 hapd->conf->iface, MAC2STR(sta->addr));
6fc6879b
JM
727 return -1;
728 }
bb598c3b 729 sta->added_unassoc = 0;
6fc6879b
JM
730 return 0;
731}
732
733
53f3d6f3
FF
734static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
735 struct sta_info *sta)
6fc6879b
JM
736{
737 struct hostapd_iface *iface = hapd->iface;
738 size_t i;
739
740 for (i = 0; i < iface->num_bss; i++) {
741 struct hostapd_data *bss = iface->bss[i];
742 struct sta_info *sta2;
743 /* bss should always be set during operation, but it may be
744 * NULL during reconfiguration. Assume the STA is not
745 * associated to another BSS in that case to avoid NULL pointer
746 * dereferences. */
747 if (bss == hapd || bss == NULL)
748 continue;
749 sta2 = ap_get_sta(bss, sta->addr);
53f3d6f3
FF
750 if (!sta2)
751 continue;
6fc6879b 752
03269d55
JM
753 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
754 " association from another BSS %s",
755 hapd->conf->iface, MAC2STR(sta2->addr),
756 bss->conf->iface);
53f3d6f3
FF
757 ap_sta_disconnect(bss, sta2, sta2->addr,
758 WLAN_REASON_PREV_AUTH_NOT_VALID);
759 }
6fc6879b
JM
760}
761
762
4dc03726
JM
763static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
764{
765 struct hostapd_data *hapd = eloop_ctx;
766 struct sta_info *sta = timeout_ctx;
767
03269d55
JM
768 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
769 hapd->conf->iface, MAC2STR(sta->addr));
4dc03726
JM
770 ap_sta_remove(hapd, sta);
771 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
772}
773
774
6fc6879b
JM
775void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
776 u16 reason)
777{
778 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
779 hapd->conf->iface, MAC2STR(sta->addr));
38cb0a2d 780 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
05e5e615
DL
781 if (hapd->iface->current_mode &&
782 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
783 /* Skip deauthentication in DMG/IEEE 802.11ad */
784 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
785 WLAN_STA_ASSOC_REQ_OK);
786 sta->timeout_next = STA_REMOVE;
787 } else {
788 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
789 sta->timeout_next = STA_DEAUTH;
790 }
4dc03726 791 ap_sta_set_authorized(hapd, sta, 0);
42ca9845
JM
792 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
793 "for " MACSTR " (%d seconds - "
794 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
795 __func__, MAC2STR(sta->addr),
796 AP_MAX_INACTIVITY_AFTER_DISASSOC);
6fc6879b
JM
797 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
798 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
799 ap_handle_timer, hapd, sta);
800 accounting_sta_stop(hapd, sta);
d7c3347f 801 ieee802_1x_free_station(hapd, sta);
6fc6879b 802
4dc03726 803 sta->disassoc_reason = reason;
cc28ad8c 804 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
4dc03726
JM
805 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
806 eloop_register_timeout(hapd->iface->drv_flags &
807 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
808 ap_sta_disassoc_cb_timeout, hapd, sta);
809}
810
811
812static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
813{
814 struct hostapd_data *hapd = eloop_ctx;
815 struct sta_info *sta = timeout_ctx;
816
03269d55
JM
817 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
818 hapd->conf->iface, MAC2STR(sta->addr));
4dc03726
JM
819 ap_sta_remove(hapd, sta);
820 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
6fc6879b
JM
821}
822
823
824void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
825 u16 reason)
826{
05e5e615
DL
827 if (hapd->iface->current_mode &&
828 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
829 /* Deauthentication is not used in DMG/IEEE 802.11ad;
830 * disassociate the STA instead. */
831 ap_sta_disassociate(hapd, sta, reason);
832 return;
833 }
834
6fc6879b
JM
835 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
836 hapd->conf->iface, MAC2STR(sta->addr));
38cb0a2d 837 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
631739b3 838 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
4dc03726 839 ap_sta_set_authorized(hapd, sta, 0);
6fc6879b 840 sta->timeout_next = STA_REMOVE;
42ca9845
JM
841 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
842 "for " MACSTR " (%d seconds - "
843 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
844 __func__, MAC2STR(sta->addr),
845 AP_MAX_INACTIVITY_AFTER_DEAUTH);
6fc6879b
JM
846 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
847 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
848 ap_handle_timer, hapd, sta);
849 accounting_sta_stop(hapd, sta);
d7c3347f 850 ieee802_1x_free_station(hapd, sta);
6fc6879b 851
4dc03726 852 sta->deauth_reason = reason;
cc28ad8c 853 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
854 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
855 eloop_register_timeout(hapd->iface->drv_flags &
856 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
857 ap_sta_deauth_cb_timeout, hapd, sta);
6fc6879b
JM
858}
859
860
4c374cde
AS
861#ifdef CONFIG_WPS
862int ap_sta_wps_cancel(struct hostapd_data *hapd,
863 struct sta_info *sta, void *ctx)
864{
865 if (sta && (sta->flags & WLAN_STA_WPS)) {
866 ap_sta_deauthenticate(hapd, sta,
867 WLAN_REASON_PREV_AUTH_NOT_VALID);
868 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
869 __func__, MAC2STR(sta->addr));
870 return 1;
871 }
872
873 return 0;
874}
875#endif /* CONFIG_WPS */
876
877
8e44c192
MB
878static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
879{
880 struct hostapd_vlan *vlan;
881 int vlan_id = MAX_VLAN_ID + 2;
882
883retry:
884 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
885 if (vlan->vlan_id == vlan_id) {
886 vlan_id++;
887 goto retry;
888 }
889 }
890 return vlan_id;
891}
892
893
1889af2e
MB
894int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
895 struct vlan_description *vlan_desc)
896{
897 struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
898 int old_vlan_id, vlan_id = 0, ret = 0;
899
8e44c192 900 /* Check if there is something to do */
8be640b7
MB
901 if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
902 /* This sta is lacking its own vif */
903 } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
904 !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
905 /* sta->vlan_id needs to be reset */
906 } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
8e44c192 907 return 0; /* nothing to change */
8be640b7 908 }
8e44c192
MB
909
910 /* Now the real VLAN changed or the STA just needs its own vif */
8be640b7
MB
911 if (hapd->conf->ssid.per_sta_vif) {
912 /* Assign a new vif, always */
913 /* find a free vlan_id sufficiently big */
914 vlan_id = ap_sta_get_free_vlan_id(hapd);
915 /* Get wildcard VLAN */
916 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
917 if (vlan->vlan_id == VLAN_ID_WILDCARD)
918 break;
919 }
920 if (!vlan) {
921 hostapd_logger(hapd, sta->addr,
922 HOSTAPD_MODULE_IEEE80211,
923 HOSTAPD_LEVEL_DEBUG,
924 "per_sta_vif missing wildcard");
925 vlan_id = 0;
926 ret = -1;
927 goto done;
928 }
929 } else if (vlan_desc && vlan_desc->notempty) {
1889af2e
MB
930 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
931 if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
932 break;
933 if (vlan->vlan_id == VLAN_ID_WILDCARD)
934 wildcard_vlan = vlan;
935 }
936 if (vlan) {
937 vlan_id = vlan->vlan_id;
938 } else if (wildcard_vlan) {
939 vlan = wildcard_vlan;
940 vlan_id = vlan_desc->untagged;
8e44c192
MB
941 if (vlan_desc->tagged[0]) {
942 /* Tagged VLAN configuration */
943 vlan_id = ap_sta_get_free_vlan_id(hapd);
944 }
1889af2e
MB
945 } else {
946 hostapd_logger(hapd, sta->addr,
947 HOSTAPD_MODULE_IEEE80211,
948 HOSTAPD_LEVEL_DEBUG,
8e44c192
MB
949 "missing vlan and wildcard for vlan=%d%s",
950 vlan_desc->untagged,
951 vlan_desc->tagged[0] ? "+" : "");
1889af2e
MB
952 vlan_id = 0;
953 ret = -1;
954 goto done;
955 }
956 }
957
958 if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
959 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
960 if (vlan == NULL) {
961 hostapd_logger(hapd, sta->addr,
962 HOSTAPD_MODULE_IEEE80211,
963 HOSTAPD_LEVEL_DEBUG,
8e44c192 964 "could not add dynamic VLAN interface for vlan=%d%s",
75cc211d
JM
965 vlan_desc ? vlan_desc->untagged : -1,
966 (vlan_desc && vlan_desc->tagged[0]) ?
967 "+" : "");
1889af2e
MB
968 vlan_id = 0;
969 ret = -1;
970 goto done;
971 }
972
973 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
974 HOSTAPD_LEVEL_DEBUG,
975 "added new dynamic VLAN interface '%s'",
976 vlan->ifname);
977 } else if (vlan && vlan->dynamic_vlan > 0) {
978 vlan->dynamic_vlan++;
979 hostapd_logger(hapd, sta->addr,
980 HOSTAPD_MODULE_IEEE80211,
981 HOSTAPD_LEVEL_DEBUG,
982 "updated existing dynamic VLAN interface '%s'",
983 vlan->ifname);
984 }
985done:
986 old_vlan_id = sta->vlan_id;
987 sta->vlan_id = vlan_id;
988 sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
989
990 if (vlan_id != old_vlan_id && old_vlan_id)
991 vlan_remove_dynamic(hapd, old_vlan_id);
992
993 return ret;
994}
995
996
c8e6beab 997int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
6fc6879b 998{
30b32314 999#ifndef CONFIG_NO_VLAN
6fc6879b
JM
1000 const char *iface;
1001 struct hostapd_vlan *vlan = NULL;
4254100d 1002 int ret;
c8e6beab 1003 int old_vlanid = sta->vlan_id_bound;
6fc6879b 1004
6fc6879b 1005 iface = hapd->conf->iface;
f41ded6f
JM
1006 if (hapd->conf->ssid.vlan[0])
1007 iface = hapd->conf->ssid.vlan;
6fc6879b 1008
1889af2e
MB
1009 if (sta->vlan_id > 0) {
1010 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
c2db79f2 1011 if (vlan->vlan_id == sta->vlan_id)
6fc6879b 1012 break;
6fc6879b 1013 }
c2db79f2
MB
1014 if (vlan)
1015 iface = vlan->ifname;
6fc6879b
JM
1016 }
1017
c8e6beab
MB
1018 /*
1019 * Do not increment ref counters if the VLAN ID remains same, but do
1020 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1021 * have been called before.
1022 */
1023 if (sta->vlan_id == old_vlanid)
1024 goto skip_counting;
1025
6fc6879b
JM
1026 if (sta->vlan_id > 0 && vlan == NULL) {
1027 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1028 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1029 "binding station to (vlan_id=%d)",
1030 sta->vlan_id);
2dd4f3ae
JM
1031 ret = -1;
1032 goto done;
1889af2e 1033 } else if (vlan && vlan->dynamic_vlan > 0) {
41d62107
MB
1034 vlan->dynamic_vlan++;
1035 hostapd_logger(hapd, sta->addr,
1036 HOSTAPD_MODULE_IEEE80211,
1037 HOSTAPD_LEVEL_DEBUG,
1038 "updated existing dynamic VLAN interface '%s'",
1039 iface);
6fc6879b
JM
1040 }
1041
c8e6beab
MB
1042 /* ref counters have been increased, so mark the station */
1043 sta->vlan_id_bound = sta->vlan_id;
1044
1045skip_counting:
6fc6879b
JM
1046 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1047 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1048 "'%s'", iface);
1049
1050 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1051 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1052
51e2a27a 1053 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
4254100d
JM
1054 if (ret < 0) {
1055 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1056 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1057 "entry to vlan_id=%d", sta->vlan_id);
1058 }
2dd4f3ae 1059
2dd4f3ae 1060 /* During 1x reauth, if the vlan id changes, then remove the old id. */
c8e6beab 1061 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
2dd4f3ae 1062 vlan_remove_dynamic(hapd, old_vlanid);
c8e6beab 1063done:
2dd4f3ae 1064
4254100d 1065 return ret;
30b32314
JM
1066#else /* CONFIG_NO_VLAN */
1067 return 0;
1068#endif /* CONFIG_NO_VLAN */
6fc6879b 1069}
5d22a1d5
JM
1070
1071
1072#ifdef CONFIG_IEEE80211W
1073
45c94154 1074int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 1075{
45c94154 1076 u32 tu;
10e694a6
JB
1077 struct os_reltime now, passed;
1078 os_get_reltime(&now);
1079 os_reltime_sub(&now, &sta->sa_query_start, &passed);
45c94154
JM
1080 tu = (passed.sec * 1000000 + passed.usec) / 1024;
1081 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1082 hostapd_logger(hapd, sta->addr,
1083 HOSTAPD_MODULE_IEEE80211,
5d22a1d5 1084 HOSTAPD_LEVEL_DEBUG,
93b76319
JM
1085 "association SA Query timed out");
1086 sta->sa_query_timed_out = 1;
1087 os_free(sta->sa_query_trans_id);
1088 sta->sa_query_trans_id = NULL;
1089 sta->sa_query_count = 0;
45c94154
JM
1090 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1091 return 1;
5d22a1d5
JM
1092 }
1093
45c94154
JM
1094 return 0;
1095}
1096
1097
1098static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1099{
1100 struct hostapd_data *hapd = eloop_ctx;
1101 struct sta_info *sta = timeout_ctx;
1102 unsigned int timeout, sec, usec;
1103 u8 *trans_id, *nbuf;
1104
03269d55
JM
1105 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1106 " (count=%d)",
1107 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1108
45c94154
JM
1109 if (sta->sa_query_count > 0 &&
1110 ap_check_sa_query_timeout(hapd, sta))
1111 return;
1112
067ffa26
JM
1113 nbuf = os_realloc_array(sta->sa_query_trans_id,
1114 sta->sa_query_count + 1,
1115 WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5
JM
1116 if (nbuf == NULL)
1117 return;
45c94154
JM
1118 if (sta->sa_query_count == 0) {
1119 /* Starting a new SA Query procedure */
10e694a6 1120 os_get_reltime(&sta->sa_query_start);
45c94154 1121 }
93b76319
JM
1122 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1123 sta->sa_query_trans_id = nbuf;
1124 sta->sa_query_count++;
5d22a1d5 1125
24661bba
JM
1126 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1127 /*
1128 * We don't really care which ID is used here, so simply
1129 * hardcode this if the mostly theoretical os_get_random()
1130 * failure happens.
1131 */
1132 trans_id[0] = 0x12;
1133 trans_id[1] = 0x34;
1134 }
5d22a1d5 1135
45c94154
JM
1136 timeout = hapd->conf->assoc_sa_query_retry_timeout;
1137 sec = ((timeout / 1000) * 1024) / 1000;
1138 usec = (timeout % 1000) * 1024;
1139 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1140
5d22a1d5
JM
1141 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1142 HOSTAPD_LEVEL_DEBUG,
93b76319 1143 "association SA Query attempt %d", sta->sa_query_count);
5d22a1d5 1144
93b76319 1145 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
5d22a1d5
JM
1146}
1147
1148
93b76319 1149void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 1150{
93b76319 1151 ap_sa_query_timer(hapd, sta);
5d22a1d5
JM
1152}
1153
1154
93b76319 1155void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 1156{
93b76319
JM
1157 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1158 os_free(sta->sa_query_trans_id);
1159 sta->sa_query_trans_id = NULL;
1160 sta->sa_query_count = 0;
5d22a1d5
JM
1161}
1162
1163#endif /* CONFIG_IEEE80211W */
45cefa0b
JM
1164
1165
6905dcb1
JB
1166void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1167 int authorized)
1168{
10cc6c88 1169 const u8 *dev_addr = NULL;
7793c959 1170 char buf[100];
c2d76aa6
MH
1171#ifdef CONFIG_P2P
1172 u8 addr[ETH_ALEN];
25ef8529 1173 u8 ip_addr_buf[4];
c2d76aa6
MH
1174#endif /* CONFIG_P2P */
1175
6905dcb1
JB
1176 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1177 return;
1178
61fc9048
SD
1179 if (authorized)
1180 sta->flags |= WLAN_STA_AUTHORIZED;
1181 else
1182 sta->flags &= ~WLAN_STA_AUTHORIZED;
1183
ae055af4 1184#ifdef CONFIG_P2P
c2d76aa6
MH
1185 if (hapd->p2p_group == NULL) {
1186 if (sta->p2p_ie != NULL &&
1187 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1188 dev_addr = addr;
1189 } else
1190 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
10cc6c88 1191
7793c959
JM
1192 if (dev_addr)
1193 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1194 MAC2STR(sta->addr), MAC2STR(dev_addr));
1195 else
375f4a3b 1196#endif /* CONFIG_P2P */
7793c959
JM
1197 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1198
61fc9048
SD
1199 if (hapd->sta_authorized_cb)
1200 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1201 sta->addr, authorized, dev_addr);
1202
10cc6c88 1203 if (authorized) {
25ef8529
JM
1204 char ip_addr[100];
1205 ip_addr[0] = '\0';
1206#ifdef CONFIG_P2P
1207 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1208 os_snprintf(ip_addr, sizeof(ip_addr),
1209 " ip_addr=%u.%u.%u.%u",
1210 ip_addr_buf[0], ip_addr_buf[1],
1211 ip_addr_buf[2], ip_addr_buf[3]);
1212 }
1213#endif /* CONFIG_P2P */
1214
1215 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1216 buf, ip_addr);
7793c959 1217
8a5e75f6 1218 if (hapd->msg_ctx_parent &&
7793c959 1219 hapd->msg_ctx_parent != hapd->msg_ctx)
c4bf83a7 1220 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
25ef8529
JM
1221 AP_STA_CONNECTED "%s%s",
1222 buf, ip_addr);
ae055af4 1223 } else {
7793c959
JM
1224 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1225
8a5e75f6 1226 if (hapd->msg_ctx_parent &&
7793c959 1227 hapd->msg_ctx_parent != hapd->msg_ctx)
c4bf83a7
JM
1228 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1229 AP_STA_DISCONNECTED "%s", buf);
ae055af4 1230 }
6959145b
AN
1231
1232#ifdef CONFIG_FST
1233 if (hapd->iface->fst) {
1234 if (authorized)
1235 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1236 else
1237 fst_notify_peer_disconnected(hapd->iface->fst,
1238 sta->addr);
1239 }
1240#endif /* CONFIG_FST */
6905dcb1
JB
1241}
1242
1243
45cefa0b
JM
1244void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1245 const u8 *addr, u16 reason)
1246{
03269d55
JM
1247 if (sta)
1248 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1249 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1250 reason);
1251 else if (addr)
1252 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1253 hapd->conf->iface, __func__, MAC2STR(addr),
1254 reason);
45cefa0b
JM
1255
1256 if (sta == NULL && addr)
1257 sta = ap_get_sta(hapd, addr);
1258
1259 if (addr)
51e2a27a 1260 hostapd_drv_sta_deauth(hapd, addr, reason);
45cefa0b
JM
1261
1262 if (sta == NULL)
1263 return;
6905dcb1 1264 ap_sta_set_authorized(hapd, sta, 0);
ceb997f3
JM
1265 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1266 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
6905dcb1 1267 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
03269d55 1268 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
42ca9845
JM
1269 "for " MACSTR " (%d seconds - "
1270 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
03269d55 1271 hapd->conf->iface, __func__, MAC2STR(sta->addr),
42ca9845 1272 AP_MAX_INACTIVITY_AFTER_DEAUTH);
45cefa0b 1273 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
4dc03726
JM
1274 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1275 ap_handle_timer, hapd, sta);
45cefa0b 1276 sta->timeout_next = STA_REMOVE;
4dc03726 1277
05e5e615
DL
1278 if (hapd->iface->current_mode &&
1279 hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1280 /* Deauthentication is not used in DMG/IEEE 802.11ad;
1281 * disassociate the STA instead. */
1282 sta->disassoc_reason = reason;
1283 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1284 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1285 eloop_register_timeout(hapd->iface->drv_flags &
1286 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ?
1287 2 : 0, 0, ap_sta_disassoc_cb_timeout,
1288 hapd, sta);
1289 return;
1290 }
1291
4dc03726 1292 sta->deauth_reason = reason;
cc28ad8c 1293 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
1294 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1295 eloop_register_timeout(hapd->iface->drv_flags &
1296 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1297 ap_sta_deauth_cb_timeout, hapd, sta);
1298}
1299
1300
1301void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1302{
cc28ad8c
JM
1303 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1304 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1305 return;
1306 }
1307 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
4dc03726
JM
1308 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1309 ap_sta_deauth_cb_timeout(hapd, sta);
1310}
1311
1312
1313void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1314{
cc28ad8c
JM
1315 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1316 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1317 return;
1318 }
1319 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1320 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1321 ap_sta_disassoc_cb_timeout(hapd, sta);
45cefa0b 1322}
b76f4c27
JM
1323
1324
9e8fde21
JM
1325void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1326 struct sta_info *sta)
1327{
1328 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1329 wpa_printf(MSG_DEBUG,
1330 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1331 MACSTR,
1332 hapd->conf->iface, MAC2STR(sta->addr));
1333 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1334 wpa_printf(MSG_DEBUG,
1335 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1336 MACSTR,
1337 hapd->conf->iface, MAC2STR(sta->addr));
0f5eb69f
JM
1338 if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1339 {
1340 wpa_printf(MSG_DEBUG,
1341 "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for "
1342 MACSTR,
1343 hapd->conf->iface, MAC2STR(sta->addr));
1344 if (sta->flags & WLAN_STA_WPS)
1345 hostapd_wps_eap_completed(hapd);
1346 }
9e8fde21
JM
1347}
1348
1349
b76f4c27
JM
1350int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1351{
1352 int res;
1353
1354 buf[0] = '\0';
d1f3a814 1355 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
b76f4c27
JM
1356 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1357 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1358 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1359 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1360 ""),
1361 (flags & WLAN_STA_SHORT_PREAMBLE ?
1362 "[SHORT_PREAMBLE]" : ""),
1363 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1364 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1365 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1366 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1367 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1368 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1369 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1370 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1371 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
d1f3a814 1372 (flags & WLAN_STA_HT ? "[HT]" : ""),
b76f4c27 1373 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
e7d0e97b 1374 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
b76f4c27
JM
1375 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1376 "[WNM_SLEEP_MODE]" : ""));
aaadd727
JM
1377 if (os_snprintf_error(buflen, res))
1378 res = -1;
b76f4c27
JM
1379
1380 return res;
1381}
0f5eb69f
JM
1382
1383
1384static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1385{
1386 struct hostapd_data *hapd = eloop_ctx;
1387 struct sta_info *sta = timeout_ctx;
5d5ee699 1388 u16 reason;
0f5eb69f
JM
1389
1390 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1391 "IEEE 802.1X: Scheduled disconnection of " MACSTR
1392 " after EAP-Failure", MAC2STR(sta->addr));
1393
5d5ee699
JM
1394 reason = sta->disconnect_reason_code;
1395 if (!reason)
1396 reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1397 ap_sta_disconnect(hapd, sta, sta->addr, reason);
0f5eb69f
JM
1398 if (sta->flags & WLAN_STA_WPS)
1399 hostapd_wps_eap_completed(hapd);
1400}
1401
1402
1403void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1404 struct sta_info *sta)
1405{
1406 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1407 "IEEE 802.1X: Force disconnection of " MACSTR
1408 " after EAP-Failure in 10 ms", MAC2STR(sta->addr));
1409
1410 /*
1411 * Add a small sleep to increase likelihood of previously requested
1412 * EAP-Failure TX getting out before this should the driver reorder
1413 * operations.
1414 */
1415 eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
1416 eloop_register_timeout(0, 10000, ap_sta_delayed_1x_auth_fail_cb,
1417 hapd, sta);
1418}
1419
1420
1421int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1422 struct sta_info *sta)
1423{
1424 return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1425 hapd, sta);
1426}