Delay STA entry removal until Deauth/Disassoc TX status in AP mode
[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         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
559         eloop_register_timeout(hapd->iface->drv_flags &
560                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
561                                ap_sta_disassoc_cb_timeout, hapd, sta);
562 }
563
564
565 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
566 {
567         struct hostapd_data *hapd = eloop_ctx;
568         struct sta_info *sta = timeout_ctx;
569
570         ap_sta_remove(hapd, sta);
571         mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
572 }
573
574
575 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
576                            u16 reason)
577 {
578         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
579                    hapd->conf->iface, MAC2STR(sta->addr));
580         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
581         ap_sta_set_authorized(hapd, sta, 0);
582         sta->timeout_next = STA_REMOVE;
583         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
584         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
585                                ap_handle_timer, hapd, sta);
586         accounting_sta_stop(hapd, sta);
587         ieee802_1x_free_station(sta);
588
589         sta->deauth_reason = reason;
590         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
591         eloop_register_timeout(hapd->iface->drv_flags &
592                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
593                                ap_sta_deauth_cb_timeout, hapd, sta);
594 }
595
596
597 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
598                      int old_vlanid)
599 {
600 #ifndef CONFIG_NO_VLAN
601         const char *iface;
602         struct hostapd_vlan *vlan = NULL;
603         int ret;
604
605         /*
606          * Do not proceed furthur if the vlan id remains same. We do not want
607          * duplicate dynamic vlan entries.
608          */
609         if (sta->vlan_id == old_vlanid)
610                 return 0;
611
612         /*
613          * During 1x reauth, if the vlan id changes, then remove the old id and
614          * proceed furthur to add the new one.
615          */
616         if (old_vlanid > 0)
617                 vlan_remove_dynamic(hapd, old_vlanid);
618
619         iface = hapd->conf->iface;
620         if (sta->ssid->vlan[0])
621                 iface = sta->ssid->vlan;
622
623         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
624                 sta->vlan_id = 0;
625         else if (sta->vlan_id > 0) {
626                 vlan = hapd->conf->vlan;
627                 while (vlan) {
628                         if (vlan->vlan_id == sta->vlan_id ||
629                             vlan->vlan_id == VLAN_ID_WILDCARD) {
630                                 iface = vlan->ifname;
631                                 break;
632                         }
633                         vlan = vlan->next;
634                 }
635         }
636
637         if (sta->vlan_id > 0 && vlan == NULL) {
638                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
639                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
640                                "binding station to (vlan_id=%d)",
641                                sta->vlan_id);
642                 return -1;
643         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
644                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
645                 if (vlan == NULL) {
646                         hostapd_logger(hapd, sta->addr,
647                                        HOSTAPD_MODULE_IEEE80211,
648                                        HOSTAPD_LEVEL_DEBUG, "could not add "
649                                        "dynamic VLAN interface for vlan_id=%d",
650                                        sta->vlan_id);
651                         return -1;
652                 }
653
654                 iface = vlan->ifname;
655                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
656                         hostapd_logger(hapd, sta->addr,
657                                        HOSTAPD_MODULE_IEEE80211,
658                                        HOSTAPD_LEVEL_DEBUG, "could not "
659                                        "configure encryption for dynamic VLAN "
660                                        "interface for vlan_id=%d",
661                                        sta->vlan_id);
662                 }
663
664                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
665                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
666                                "interface '%s'", iface);
667         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
668                 if (sta->vlan_id > 0) {
669                         vlan->dynamic_vlan++;
670                         hostapd_logger(hapd, sta->addr,
671                                        HOSTAPD_MODULE_IEEE80211,
672                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
673                                        "dynamic VLAN interface '%s'", iface);
674                 }
675
676                 /*
677                  * Update encryption configuration for statically generated
678                  * VLAN interface. This is only used for static WEP
679                  * configuration for the case where hostapd did not yet know
680                  * which keys are to be used when the interface was added.
681                  */
682                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
683                         hostapd_logger(hapd, sta->addr,
684                                        HOSTAPD_MODULE_IEEE80211,
685                                        HOSTAPD_LEVEL_DEBUG, "could not "
686                                        "configure encryption for VLAN "
687                                        "interface for vlan_id=%d",
688                                        sta->vlan_id);
689                 }
690         }
691
692         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
693                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
694                        "'%s'", iface);
695
696         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
697                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
698
699         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
700         if (ret < 0) {
701                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
702                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
703                                "entry to vlan_id=%d", sta->vlan_id);
704         }
705         return ret;
706 #else /* CONFIG_NO_VLAN */
707         return 0;
708 #endif /* CONFIG_NO_VLAN */
709 }
710
711
712 #ifdef CONFIG_IEEE80211W
713
714 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
715 {
716         u32 tu;
717         struct os_time now, passed;
718         os_get_time(&now);
719         os_time_sub(&now, &sta->sa_query_start, &passed);
720         tu = (passed.sec * 1000000 + passed.usec) / 1024;
721         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
722                 hostapd_logger(hapd, sta->addr,
723                                HOSTAPD_MODULE_IEEE80211,
724                                HOSTAPD_LEVEL_DEBUG,
725                                "association SA Query timed out");
726                 sta->sa_query_timed_out = 1;
727                 os_free(sta->sa_query_trans_id);
728                 sta->sa_query_trans_id = NULL;
729                 sta->sa_query_count = 0;
730                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
731                 return 1;
732         }
733
734         return 0;
735 }
736
737
738 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
739 {
740         struct hostapd_data *hapd = eloop_ctx;
741         struct sta_info *sta = timeout_ctx;
742         unsigned int timeout, sec, usec;
743         u8 *trans_id, *nbuf;
744
745         if (sta->sa_query_count > 0 &&
746             ap_check_sa_query_timeout(hapd, sta))
747                 return;
748
749         nbuf = os_realloc(sta->sa_query_trans_id,
750                           (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
751         if (nbuf == NULL)
752                 return;
753         if (sta->sa_query_count == 0) {
754                 /* Starting a new SA Query procedure */
755                 os_get_time(&sta->sa_query_start);
756         }
757         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
758         sta->sa_query_trans_id = nbuf;
759         sta->sa_query_count++;
760
761         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
762
763         timeout = hapd->conf->assoc_sa_query_retry_timeout;
764         sec = ((timeout / 1000) * 1024) / 1000;
765         usec = (timeout % 1000) * 1024;
766         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
767
768         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
769                        HOSTAPD_LEVEL_DEBUG,
770                        "association SA Query attempt %d", sta->sa_query_count);
771
772 #ifdef NEED_AP_MLME
773         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
774 #endif /* NEED_AP_MLME */
775 }
776
777
778 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
779 {
780         ap_sa_query_timer(hapd, sta);
781 }
782
783
784 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
785 {
786         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
787         os_free(sta->sa_query_trans_id);
788         sta->sa_query_trans_id = NULL;
789         sta->sa_query_count = 0;
790 }
791
792 #endif /* CONFIG_IEEE80211W */
793
794
795 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
796                            int authorized)
797 {
798         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
799                 return;
800
801         if (authorized)
802                 sta->flags |= WLAN_STA_AUTHORIZED;
803         else
804                 sta->flags &= ~WLAN_STA_AUTHORIZED;
805
806         if (hapd->sta_authorized_cb)
807                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
808                                         sta->addr, authorized);
809 }
810
811
812 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
813                        const u8 *addr, u16 reason)
814 {
815
816         if (sta == NULL && addr)
817                 sta = ap_get_sta(hapd, addr);
818
819         if (addr)
820                 hostapd_drv_sta_deauth(hapd, addr, reason);
821
822         if (sta == NULL)
823                 return;
824         ap_sta_set_authorized(hapd, sta, 0);
825         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
826         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
827         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
828                                ap_handle_timer, hapd, sta);
829         sta->timeout_next = STA_REMOVE;
830
831         sta->deauth_reason = reason;
832         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
833         eloop_register_timeout(hapd->iface->drv_flags &
834                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
835                                ap_sta_deauth_cb_timeout, hapd, sta);
836 }
837
838
839 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
840 {
841         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
842         ap_sta_deauth_cb_timeout(hapd, sta);
843 }
844
845
846 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
847 {
848 }