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