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