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