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