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