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