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