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