]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/ap/sta_info.c
P2P: Clear driver Probe Response IE on stop_listen
[thirdparty/hostap.git] / src / ap / sta_info.c
CommitLineData
6fc6879b
JM
1/*
2 * hostapd / Station table
bdee6fce 3 * Copyright (c) 2002-2009, 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"
bdee6fce
JM
20#include "radius/radius.h"
21#include "radius/radius_client.h"
22#include "drivers/driver.h"
8ccbe415 23#include "p2p/p2p.h"
6fc6879b 24#include "hostapd.h"
6fc6879b
JM
25#include "accounting.h"
26#include "ieee802_1x.h"
27#include "ieee802_11.h"
6226e38d
JM
28#include "wpa_auth.h"
29#include "preauth_auth.h"
30#include "ap_config.h"
6fc6879b 31#include "beacon.h"
6226e38d 32#include "ap_mlme.h"
6fc6879b 33#include "vlan_init.h"
aefb53bd 34#include "p2p_hostapd.h"
cee7d66b 35#include "ap_drv_ops.h"
6226e38d 36#include "sta_info.h"
6fc6879b 37
53f3d6f3
FF
38static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
39 struct sta_info *sta);
6fc6879b 40static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 41#ifdef CONFIG_IEEE80211W
93b76319 42static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
5d22a1d5 43#endif /* CONFIG_IEEE80211W */
6fc6879b
JM
44
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
39f42d11 127 if (sta->flags & WLAN_STA_WDS)
0e8a96a9 128 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
53f3d6f3
FF
129
130 if (!(sta->flags & WLAN_STA_PREAUTH))
51e2a27a 131 hostapd_drv_sta_remove(hapd, sta->addr);
6fc6879b
JM
132
133 ap_sta_hash_del(hapd, sta);
134 ap_sta_list_del(hapd, sta);
135
136 if (sta->aid > 0)
2991469c
JM
137 hapd->sta_aid[(sta->aid - 1) / 32] &=
138 ~BIT((sta->aid - 1) % 32);
6fc6879b
JM
139
140 hapd->num_sta--;
141 if (sta->nonerp_set) {
142 sta->nonerp_set = 0;
143 hapd->iface->num_sta_non_erp--;
144 if (hapd->iface->num_sta_non_erp == 0)
145 set_beacon++;
146 }
147
148 if (sta->no_short_slot_time_set) {
149 sta->no_short_slot_time_set = 0;
150 hapd->iface->num_sta_no_short_slot_time--;
151 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
152 && hapd->iface->num_sta_no_short_slot_time == 0)
153 set_beacon++;
154 }
155
156 if (sta->no_short_preamble_set) {
157 sta->no_short_preamble_set = 0;
158 hapd->iface->num_sta_no_short_preamble--;
159 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
160 && hapd->iface->num_sta_no_short_preamble == 0)
161 set_beacon++;
162 }
163
e8ff1e59
JM
164 if (sta->no_ht_gf_set) {
165 sta->no_ht_gf_set = 0;
166 hapd->iface->num_sta_ht_no_gf--;
167 }
168
169 if (sta->no_ht_set) {
170 sta->no_ht_set = 0;
de9289c8 171 hapd->iface->num_sta_no_ht--;
e8ff1e59
JM
172 }
173
174 if (sta->ht_20mhz_set) {
175 sta->ht_20mhz_set = 0;
176 hapd->iface->num_sta_ht_20mhz--;
177 }
de9289c8 178
aefb53bd
JM
179#ifdef CONFIG_P2P
180 if (sta->no_p2p_set) {
181 sta->no_p2p_set = 0;
182 hapd->num_sta_no_p2p--;
183 if (hapd->num_sta_no_p2p == 0)
184 hostapd_p2p_non_p2p_sta_disconnected(hapd);
185 }
186#endif /* CONFIG_P2P */
187
d45354be 188#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
de9289c8
JM
189 if (hostapd_ht_operation_update(hapd->iface) > 0)
190 set_beacon++;
d45354be 191#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
de9289c8 192
6fc6879b
JM
193 if (set_beacon)
194 ieee802_11_set_beacons(hapd->iface);
195
196 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
197 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
198
199 ieee802_1x_free_station(sta);
200 wpa_auth_sta_deinit(sta->wpa_sm);
201 rsn_preauth_free_station(hapd, sta);
74784010 202#ifndef CONFIG_NO_RADIUS
6fc6879b 203 radius_client_flush_auth(hapd->radius, sta->addr);
74784010 204#endif /* CONFIG_NO_RADIUS */
6fc6879b
JM
205
206 os_free(sta->last_assoc_req);
207 os_free(sta->challenge);
5d22a1d5
JM
208
209#ifdef CONFIG_IEEE80211W
93b76319
JM
210 os_free(sta->sa_query_trans_id);
211 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
5d22a1d5
JM
212#endif /* CONFIG_IEEE80211W */
213
8ccbe415
JM
214#ifdef CONFIG_P2P
215 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
216#endif /* CONFIG_P2P */
217
eb76b7e3 218 wpabuf_free(sta->wps_ie);
b305c684 219 wpabuf_free(sta->p2p_ie);
eb76b7e3 220
df84268a
JM
221 os_free(sta->ht_capabilities);
222
6fc6879b
JM
223 os_free(sta);
224}
225
226
227void hostapd_free_stas(struct hostapd_data *hapd)
228{
229 struct sta_info *sta, *prev;
230
231 sta = hapd->sta_list;
232
233 while (sta) {
234 prev = sta;
235 if (sta->flags & WLAN_STA_AUTH) {
236 mlme_deauthenticate_indication(
237 hapd, sta, WLAN_REASON_UNSPECIFIED);
238 }
239 sta = sta->next;
240 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
241 MAC2STR(prev->addr));
242 ap_free_sta(hapd, prev);
243 }
244}
245
246
1c6e69cc
JM
247/**
248 * ap_handle_timer - Per STA timer handler
249 * @eloop_ctx: struct hostapd_data *
250 * @timeout_ctx: struct sta_info *
251 *
252 * This function is called to check station activity and to remove inactive
253 * stations.
254 */
6fc6879b
JM
255void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
256{
257 struct hostapd_data *hapd = eloop_ctx;
258 struct sta_info *sta = timeout_ctx;
259 unsigned long next_time = 0;
260
261 if (sta->timeout_next == STA_REMOVE) {
262 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
263 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
264 "local deauth request");
265 ap_free_sta(hapd, sta);
266 return;
267 }
268
269 if ((sta->flags & WLAN_STA_ASSOC) &&
270 (sta->timeout_next == STA_NULLFUNC ||
271 sta->timeout_next == STA_DISASSOC)) {
272 int inactive_sec;
273 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
274 MAC2STR(sta->addr));
51e2a27a 275 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
6fc6879b
JM
276 if (inactive_sec == -1) {
277 wpa_printf(MSG_DEBUG, "Could not get station info "
278 "from kernel driver for " MACSTR ".",
279 MAC2STR(sta->addr));
280 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
281 sta->flags & WLAN_STA_ASSOC) {
282 /* station activity detected; reset timeout state */
283 wpa_printf(MSG_DEBUG, " Station has been active");
284 sta->timeout_next = STA_NULLFUNC;
285 next_time = hapd->conf->ap_max_inactivity -
286 inactive_sec;
287 }
288 }
289
290 if ((sta->flags & WLAN_STA_ASSOC) &&
291 sta->timeout_next == STA_DISASSOC &&
292 !(sta->flags & WLAN_STA_PENDING_POLL)) {
293 wpa_printf(MSG_DEBUG, " Station has ACKed data poll");
294 /* data nullfunc frame poll did not produce TX errors; assume
295 * station ACKed it */
296 sta->timeout_next = STA_NULLFUNC;
297 next_time = hapd->conf->ap_max_inactivity;
298 }
299
300 if (next_time) {
301 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
302 sta);
303 return;
304 }
305
306 if (sta->timeout_next == STA_NULLFUNC &&
307 (sta->flags & WLAN_STA_ASSOC)) {
08230317 308#ifndef CONFIG_NATIVE_WINDOWS
6fc6879b
JM
309 /* send data frame to poll STA and check whether this frame
310 * is ACKed */
311 struct ieee80211_hdr hdr;
312
313 wpa_printf(MSG_DEBUG, " Polling STA with data frame");
314 sta->flags |= WLAN_STA_PENDING_POLL;
315
6fc6879b 316 os_memset(&hdr, 0, sizeof(hdr));
1e145265
JM
317 if (hapd->driver &&
318 os_strcmp(hapd->driver->name, "hostap") == 0) {
319 /*
320 * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
321 * but it is apparently not retried so TX Exc events
322 * are not received for it.
323 */
324 hdr.frame_control =
325 IEEE80211_FC(WLAN_FC_TYPE_DATA,
326 WLAN_FC_STYPE_DATA);
327 } else {
328 hdr.frame_control =
329 IEEE80211_FC(WLAN_FC_TYPE_DATA,
330 WLAN_FC_STYPE_NULLFUNC);
331 }
332
6fc6879b
JM
333 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
334 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
335 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
336 ETH_ALEN);
337 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
338
cee7d66b 339 if (hostapd_drv_send_mlme(hapd, &hdr, sizeof(hdr)) < 0)
6fc6879b
JM
340 perror("ap_handle_timer: send");
341#endif /* CONFIG_NATIVE_WINDOWS */
342 } else if (sta->timeout_next != STA_REMOVE) {
343 int deauth = sta->timeout_next == STA_DEAUTH;
344
345 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
346 deauth ? "deauthentication" : "disassociation",
347 MAC2STR(sta->addr));
348
349 if (deauth) {
51e2a27a
JM
350 hostapd_drv_sta_deauth(
351 hapd, sta->addr,
352 WLAN_REASON_PREV_AUTH_NOT_VALID);
6fc6879b 353 } else {
51e2a27a 354 hostapd_drv_sta_disassoc(
6fc6879b
JM
355 hapd, sta->addr,
356 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
357 }
358 }
359
360 switch (sta->timeout_next) {
361 case STA_NULLFUNC:
362 sta->timeout_next = STA_DISASSOC;
363 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
364 hapd, sta);
365 break;
366 case STA_DISASSOC:
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 "
387 "inactivity");
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
519void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
520 u16 reason)
521{
522 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
523 hapd->conf->iface, MAC2STR(sta->addr));
524 sta->flags &= ~WLAN_STA_ASSOC;
53f3d6f3 525 ap_sta_remove(hapd, sta);
6fc6879b
JM
526 sta->timeout_next = STA_DEAUTH;
527 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
528 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
529 ap_handle_timer, hapd, sta);
530 accounting_sta_stop(hapd, sta);
531 ieee802_1x_free_station(sta);
532
533 mlme_disassociate_indication(hapd, sta, reason);
534}
535
536
537void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
538 u16 reason)
539{
540 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
541 hapd->conf->iface, MAC2STR(sta->addr));
542 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
53f3d6f3 543 ap_sta_remove(hapd, sta);
6fc6879b
JM
544 sta->timeout_next = STA_REMOVE;
545 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
546 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
547 ap_handle_timer, hapd, sta);
548 accounting_sta_stop(hapd, sta);
549 ieee802_1x_free_station(sta);
550
551 mlme_deauthenticate_indication(hapd, sta, reason);
552}
553
554
555int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
556 int old_vlanid)
557{
30b32314 558#ifndef CONFIG_NO_VLAN
6fc6879b
JM
559 const char *iface;
560 struct hostapd_vlan *vlan = NULL;
4254100d 561 int ret;
6fc6879b
JM
562
563 /*
564 * Do not proceed furthur if the vlan id remains same. We do not want
565 * duplicate dynamic vlan entries.
566 */
567 if (sta->vlan_id == old_vlanid)
568 return 0;
569
570 /*
571 * During 1x reauth, if the vlan id changes, then remove the old id and
572 * proceed furthur to add the new one.
573 */
574 if (old_vlanid > 0)
575 vlan_remove_dynamic(hapd, old_vlanid);
576
577 iface = hapd->conf->iface;
578 if (sta->ssid->vlan[0])
579 iface = sta->ssid->vlan;
580
581 if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
582 sta->vlan_id = 0;
583 else if (sta->vlan_id > 0) {
584 vlan = hapd->conf->vlan;
585 while (vlan) {
586 if (vlan->vlan_id == sta->vlan_id ||
587 vlan->vlan_id == VLAN_ID_WILDCARD) {
588 iface = vlan->ifname;
589 break;
590 }
591 vlan = vlan->next;
592 }
593 }
594
595 if (sta->vlan_id > 0 && vlan == NULL) {
596 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
597 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
598 "binding station to (vlan_id=%d)",
599 sta->vlan_id);
600 return -1;
601 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
602 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
603 if (vlan == NULL) {
604 hostapd_logger(hapd, sta->addr,
605 HOSTAPD_MODULE_IEEE80211,
606 HOSTAPD_LEVEL_DEBUG, "could not add "
607 "dynamic VLAN interface for vlan_id=%d",
608 sta->vlan_id);
609 return -1;
610 }
611
612 iface = vlan->ifname;
613 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
614 hostapd_logger(hapd, sta->addr,
615 HOSTAPD_MODULE_IEEE80211,
616 HOSTAPD_LEVEL_DEBUG, "could not "
617 "configure encryption for dynamic VLAN "
618 "interface for vlan_id=%d",
619 sta->vlan_id);
620 }
621
622 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
623 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
624 "interface '%s'", iface);
625 } else if (vlan && vlan->vlan_id == sta->vlan_id) {
626 if (sta->vlan_id > 0) {
627 vlan->dynamic_vlan++;
628 hostapd_logger(hapd, sta->addr,
629 HOSTAPD_MODULE_IEEE80211,
630 HOSTAPD_LEVEL_DEBUG, "updated existing "
631 "dynamic VLAN interface '%s'", iface);
632 }
633
634 /*
635 * Update encryption configuration for statically generated
636 * VLAN interface. This is only used for static WEP
637 * configuration for the case where hostapd did not yet know
638 * which keys are to be used when the interface was added.
639 */
640 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
641 hostapd_logger(hapd, sta->addr,
642 HOSTAPD_MODULE_IEEE80211,
643 HOSTAPD_LEVEL_DEBUG, "could not "
644 "configure encryption for VLAN "
645 "interface for vlan_id=%d",
646 sta->vlan_id);
647 }
648 }
649
650 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
651 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
652 "'%s'", iface);
653
654 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
655 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
656
51e2a27a 657 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
4254100d
JM
658 if (ret < 0) {
659 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
660 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
661 "entry to vlan_id=%d", sta->vlan_id);
662 }
663 return ret;
30b32314
JM
664#else /* CONFIG_NO_VLAN */
665 return 0;
666#endif /* CONFIG_NO_VLAN */
6fc6879b 667}
5d22a1d5
JM
668
669
670#ifdef CONFIG_IEEE80211W
671
45c94154 672int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 673{
45c94154
JM
674 u32 tu;
675 struct os_time now, passed;
676 os_get_time(&now);
677 os_time_sub(&now, &sta->sa_query_start, &passed);
678 tu = (passed.sec * 1000000 + passed.usec) / 1024;
679 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
680 hostapd_logger(hapd, sta->addr,
681 HOSTAPD_MODULE_IEEE80211,
5d22a1d5 682 HOSTAPD_LEVEL_DEBUG,
93b76319
JM
683 "association SA Query timed out");
684 sta->sa_query_timed_out = 1;
685 os_free(sta->sa_query_trans_id);
686 sta->sa_query_trans_id = NULL;
687 sta->sa_query_count = 0;
45c94154
JM
688 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
689 return 1;
5d22a1d5
JM
690 }
691
45c94154
JM
692 return 0;
693}
694
695
696static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
697{
698 struct hostapd_data *hapd = eloop_ctx;
699 struct sta_info *sta = timeout_ctx;
700 unsigned int timeout, sec, usec;
701 u8 *trans_id, *nbuf;
702
703 if (sta->sa_query_count > 0 &&
704 ap_check_sa_query_timeout(hapd, sta))
705 return;
706
93b76319
JM
707 nbuf = os_realloc(sta->sa_query_trans_id,
708 (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5
JM
709 if (nbuf == NULL)
710 return;
45c94154
JM
711 if (sta->sa_query_count == 0) {
712 /* Starting a new SA Query procedure */
713 os_get_time(&sta->sa_query_start);
714 }
93b76319
JM
715 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
716 sta->sa_query_trans_id = nbuf;
717 sta->sa_query_count++;
5d22a1d5 718
93b76319 719 os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
5d22a1d5 720
45c94154
JM
721 timeout = hapd->conf->assoc_sa_query_retry_timeout;
722 sec = ((timeout / 1000) * 1024) / 1000;
723 usec = (timeout % 1000) * 1024;
724 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
725
5d22a1d5
JM
726 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
727 HOSTAPD_LEVEL_DEBUG,
93b76319 728 "association SA Query attempt %d", sta->sa_query_count);
5d22a1d5 729
fe6bdb77 730#ifdef NEED_AP_MLME
93b76319 731 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
fe6bdb77 732#endif /* NEED_AP_MLME */
5d22a1d5
JM
733}
734
735
93b76319 736void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 737{
93b76319 738 ap_sa_query_timer(hapd, sta);
5d22a1d5
JM
739}
740
741
93b76319 742void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
5d22a1d5 743{
93b76319
JM
744 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
745 os_free(sta->sa_query_trans_id);
746 sta->sa_query_trans_id = NULL;
747 sta->sa_query_count = 0;
5d22a1d5
JM
748}
749
750#endif /* CONFIG_IEEE80211W */
45cefa0b
JM
751
752
753void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
754 const u8 *addr, u16 reason)
755{
756
757 if (sta == NULL && addr)
758 sta = ap_get_sta(hapd, addr);
759
760 if (addr)
51e2a27a 761 hostapd_drv_sta_deauth(hapd, addr, reason);
45cefa0b
JM
762
763 if (sta == NULL)
764 return;
765 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
766 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
767 eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
768 sta->timeout_next = STA_REMOVE;
769}