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