Move AP events for STA connected/disconnected into one function
[mech_eap.git] / src / ap / sta_info.c
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2011, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "utils/includes.h"
16
17 #include "utils/common.h"
18 #include "utils/eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/wpa_ctrl.h"
21 #include "radius/radius.h"
22 #include "radius/radius_client.h"
23 #include "drivers/driver.h"
24 #include "p2p/p2p.h"
25 #include "hostapd.h"
26 #include "accounting.h"
27 #include "ieee802_1x.h"
28 #include "ieee802_11.h"
29 #include "wpa_auth.h"
30 #include "preauth_auth.h"
31 #include "ap_config.h"
32 #include "beacon.h"
33 #include "ap_mlme.h"
34 #include "vlan_init.h"
35 #include "p2p_hostapd.h"
36 #include "ap_drv_ops.h"
37 #include "sta_info.h"
38
39 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
40                                        struct sta_info *sta);
41 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
42 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
44 #ifdef CONFIG_IEEE80211W
45 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
46 #endif /* CONFIG_IEEE80211W */
47 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
48
49 int ap_for_each_sta(struct hostapd_data *hapd,
50                     int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
51                               void *ctx),
52                     void *ctx)
53 {
54         struct sta_info *sta;
55
56         for (sta = hapd->sta_list; sta; sta = sta->next) {
57                 if (cb(hapd, sta, ctx))
58                         return 1;
59         }
60
61         return 0;
62 }
63
64
65 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
66 {
67         struct sta_info *s;
68
69         s = hapd->sta_hash[STA_HASH(sta)];
70         while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
71                 s = s->hnext;
72         return s;
73 }
74
75
76 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
77 {
78         struct sta_info *tmp;
79
80         if (hapd->sta_list == sta) {
81                 hapd->sta_list = sta->next;
82                 return;
83         }
84
85         tmp = hapd->sta_list;
86         while (tmp != NULL && tmp->next != sta)
87                 tmp = tmp->next;
88         if (tmp == NULL) {
89                 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
90                            "list.", MAC2STR(sta->addr));
91         } else
92                 tmp->next = sta->next;
93 }
94
95
96 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
97 {
98         sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
99         hapd->sta_hash[STA_HASH(sta->addr)] = sta;
100 }
101
102
103 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
104 {
105         struct sta_info *s;
106
107         s = hapd->sta_hash[STA_HASH(sta->addr)];
108         if (s == NULL) return;
109         if (os_memcmp(s->addr, sta->addr, 6) == 0) {
110                 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
111                 return;
112         }
113
114         while (s->hnext != NULL &&
115                os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
116                 s = s->hnext;
117         if (s->hnext != NULL)
118                 s->hnext = s->hnext->hnext;
119         else
120                 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
121                            " from hash table", MAC2STR(sta->addr));
122 }
123
124
125 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
126 {
127         int set_beacon = 0;
128
129         accounting_sta_stop(hapd, sta);
130
131         /* just in case */
132         ap_sta_set_authorized(hapd, sta, 0);
133
134         if (sta->flags & WLAN_STA_WDS)
135                 hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
136
137         if (!(sta->flags & WLAN_STA_PREAUTH))
138                 hostapd_drv_sta_remove(hapd, sta->addr);
139
140         ap_sta_hash_del(hapd, sta);
141         ap_sta_list_del(hapd, sta);
142
143         if (sta->aid > 0)
144                 hapd->sta_aid[(sta->aid - 1) / 32] &=
145                         ~BIT((sta->aid - 1) % 32);
146
147         hapd->num_sta--;
148         if (sta->nonerp_set) {
149                 sta->nonerp_set = 0;
150                 hapd->iface->num_sta_non_erp--;
151                 if (hapd->iface->num_sta_non_erp == 0)
152                         set_beacon++;
153         }
154
155         if (sta->no_short_slot_time_set) {
156                 sta->no_short_slot_time_set = 0;
157                 hapd->iface->num_sta_no_short_slot_time--;
158                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
159                     && hapd->iface->num_sta_no_short_slot_time == 0)
160                         set_beacon++;
161         }
162
163         if (sta->no_short_preamble_set) {
164                 sta->no_short_preamble_set = 0;
165                 hapd->iface->num_sta_no_short_preamble--;
166                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
167                     && hapd->iface->num_sta_no_short_preamble == 0)
168                         set_beacon++;
169         }
170
171         if (sta->no_ht_gf_set) {
172                 sta->no_ht_gf_set = 0;
173                 hapd->iface->num_sta_ht_no_gf--;
174         }
175
176         if (sta->no_ht_set) {
177                 sta->no_ht_set = 0;
178                 hapd->iface->num_sta_no_ht--;
179         }
180
181         if (sta->ht_20mhz_set) {
182                 sta->ht_20mhz_set = 0;
183                 hapd->iface->num_sta_ht_20mhz--;
184         }
185
186 #ifdef CONFIG_P2P
187         if (sta->no_p2p_set) {
188                 sta->no_p2p_set = 0;
189                 hapd->num_sta_no_p2p--;
190                 if (hapd->num_sta_no_p2p == 0)
191                         hostapd_p2p_non_p2p_sta_disconnected(hapd);
192         }
193 #endif /* CONFIG_P2P */
194
195 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
196         if (hostapd_ht_operation_update(hapd->iface) > 0)
197                 set_beacon++;
198 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
199
200         if (set_beacon)
201                 ieee802_11_set_beacons(hapd->iface);
202
203         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
204         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
205         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
206         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
207
208         ieee802_1x_free_station(sta);
209         wpa_auth_sta_deinit(sta->wpa_sm);
210         rsn_preauth_free_station(hapd, sta);
211 #ifndef CONFIG_NO_RADIUS
212         radius_client_flush_auth(hapd->radius, sta->addr);
213 #endif /* CONFIG_NO_RADIUS */
214
215         os_free(sta->last_assoc_req);
216         os_free(sta->challenge);
217
218 #ifdef CONFIG_IEEE80211W
219         os_free(sta->sa_query_trans_id);
220         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
221 #endif /* CONFIG_IEEE80211W */
222
223 #ifdef CONFIG_P2P
224         p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
225 #endif /* CONFIG_P2P */
226
227         wpabuf_free(sta->wps_ie);
228         wpabuf_free(sta->p2p_ie);
229
230         os_free(sta->ht_capabilities);
231
232         os_free(sta);
233 }
234
235
236 void hostapd_free_stas(struct hostapd_data *hapd)
237 {
238         struct sta_info *sta, *prev;
239
240         sta = hapd->sta_list;
241
242         while (sta) {
243                 prev = sta;
244                 if (sta->flags & WLAN_STA_AUTH) {
245                         mlme_deauthenticate_indication(
246                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
247                 }
248                 sta = sta->next;
249                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
250                            MAC2STR(prev->addr));
251                 ap_free_sta(hapd, prev);
252         }
253 }
254
255
256 /**
257  * ap_handle_timer - Per STA timer handler
258  * @eloop_ctx: struct hostapd_data *
259  * @timeout_ctx: struct sta_info *
260  *
261  * This function is called to check station activity and to remove inactive
262  * stations.
263  */
264 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
265 {
266         struct hostapd_data *hapd = eloop_ctx;
267         struct sta_info *sta = timeout_ctx;
268         unsigned long next_time = 0;
269
270         if (sta->timeout_next == STA_REMOVE) {
271                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
272                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
273                                "local deauth request");
274                 ap_free_sta(hapd, sta);
275                 return;
276         }
277
278         if ((sta->flags & WLAN_STA_ASSOC) &&
279             (sta->timeout_next == STA_NULLFUNC ||
280              sta->timeout_next == STA_DISASSOC)) {
281                 int inactive_sec;
282                 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
283                 if (inactive_sec == -1) {
284                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
285                                 "Check inactivity: Could not "
286                                 "get station info rom kernel driver for "
287                                 MACSTR, MAC2STR(sta->addr));
288                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
289                            sta->flags & WLAN_STA_ASSOC) {
290                         /* station activity detected; reset timeout state */
291                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
292                                 "Station " MACSTR " has been active %is ago",
293                                 MAC2STR(sta->addr), inactive_sec);
294                         sta->timeout_next = STA_NULLFUNC;
295                         next_time = hapd->conf->ap_max_inactivity -
296                                 inactive_sec;
297                 } else {
298                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
299                                 "Station " MACSTR " has been "
300                                 "inactive too long: %d sec, max allowed: %d",
301                                 MAC2STR(sta->addr), inactive_sec,
302                                 hapd->conf->ap_max_inactivity);
303                 }
304         }
305
306         if ((sta->flags & WLAN_STA_ASSOC) &&
307             sta->timeout_next == STA_DISASSOC &&
308             !(sta->flags & WLAN_STA_PENDING_POLL)) {
309                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
310                         " has ACKed data poll", MAC2STR(sta->addr));
311                 /* data nullfunc frame poll did not produce TX errors; assume
312                  * station ACKed it */
313                 sta->timeout_next = STA_NULLFUNC;
314                 next_time = hapd->conf->ap_max_inactivity;
315         }
316
317         if (next_time) {
318                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
319                                        sta);
320                 return;
321         }
322
323         if (sta->timeout_next == STA_NULLFUNC &&
324             (sta->flags & WLAN_STA_ASSOC)) {
325                 wpa_printf(MSG_DEBUG, "  Polling STA");
326                 sta->flags |= WLAN_STA_PENDING_POLL;
327                 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
328                                         sta->flags & WLAN_STA_WMM);
329         } else if (sta->timeout_next != STA_REMOVE) {
330                 int deauth = sta->timeout_next == STA_DEAUTH;
331
332                 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
333                            deauth ? "deauthentication" : "disassociation",
334                            MAC2STR(sta->addr));
335
336                 if (deauth) {
337                         hostapd_drv_sta_deauth(
338                                 hapd, sta->addr,
339                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
340                 } else {
341                         hostapd_drv_sta_disassoc(
342                                 hapd, sta->addr,
343                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
344                 }
345         }
346
347         switch (sta->timeout_next) {
348         case STA_NULLFUNC:
349                 sta->timeout_next = STA_DISASSOC;
350                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
351                                        hapd, sta);
352                 break;
353         case STA_DISASSOC:
354                 ap_sta_set_authorized(hapd, sta, 0);
355                 sta->flags &= ~WLAN_STA_ASSOC;
356                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
357                 if (!sta->acct_terminate_cause)
358                         sta->acct_terminate_cause =
359                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
360                 accounting_sta_stop(hapd, sta);
361                 ieee802_1x_free_station(sta);
362                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
363                                HOSTAPD_LEVEL_INFO, "disassociated due to "
364                                "inactivity");
365                 sta->timeout_next = STA_DEAUTH;
366                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
367                                        hapd, sta);
368                 mlme_disassociate_indication(
369                         hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
370                 break;
371         case STA_DEAUTH:
372         case STA_REMOVE:
373                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
374                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
375                                "inactivity");
376                 if (!sta->acct_terminate_cause)
377                         sta->acct_terminate_cause =
378                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
379                 mlme_deauthenticate_indication(
380                         hapd, sta,
381                         WLAN_REASON_PREV_AUTH_NOT_VALID);
382                 ap_free_sta(hapd, sta);
383                 break;
384         }
385 }
386
387
388 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
389 {
390         struct hostapd_data *hapd = eloop_ctx;
391         struct sta_info *sta = timeout_ctx;
392         u8 addr[ETH_ALEN];
393
394         if (!(sta->flags & WLAN_STA_AUTH))
395                 return;
396
397         mlme_deauthenticate_indication(hapd, sta,
398                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
399         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
400                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
401                        "session timeout");
402         sta->acct_terminate_cause =
403                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
404         os_memcpy(addr, sta->addr, ETH_ALEN);
405         ap_free_sta(hapd, sta);
406         hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
407 }
408
409
410 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
411                             u32 session_timeout)
412 {
413         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
414                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
415                        "seconds", session_timeout);
416         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
417         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
418                                hapd, sta);
419 }
420
421
422 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
423 {
424         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
425 }
426
427
428 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
429 {
430         struct sta_info *sta;
431
432         sta = ap_get_sta(hapd, addr);
433         if (sta)
434                 return sta;
435
436         wpa_printf(MSG_DEBUG, "  New STA");
437         if (hapd->num_sta >= hapd->conf->max_num_sta) {
438                 /* FIX: might try to remove some old STAs first? */
439                 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
440                            hapd->num_sta, hapd->conf->max_num_sta);
441                 return NULL;
442         }
443
444         sta = os_zalloc(sizeof(struct sta_info));
445         if (sta == NULL) {
446                 wpa_printf(MSG_ERROR, "malloc failed");
447                 return NULL;
448         }
449         sta->acct_interim_interval = hapd->conf->acct_interim_interval;
450
451         /* initialize STA info data */
452         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
453                                ap_handle_timer, hapd, sta);
454         os_memcpy(sta->addr, addr, ETH_ALEN);
455         sta->next = hapd->sta_list;
456         hapd->sta_list = sta;
457         hapd->num_sta++;
458         ap_sta_hash_add(hapd, sta);
459         sta->ssid = &hapd->conf->ssid;
460         ap_sta_remove_in_other_bss(hapd, sta);
461
462         return sta;
463 }
464
465
466 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
467 {
468         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
469
470         wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
471                    MAC2STR(sta->addr));
472         if (hostapd_drv_sta_remove(hapd, sta->addr) &&
473             sta->flags & WLAN_STA_ASSOC) {
474                 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
475                            " from kernel driver.", MAC2STR(sta->addr));
476                 return -1;
477         }
478         return 0;
479 }
480
481
482 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
483                                        struct sta_info *sta)
484 {
485         struct hostapd_iface *iface = hapd->iface;
486         size_t i;
487
488         for (i = 0; i < iface->num_bss; i++) {
489                 struct hostapd_data *bss = iface->bss[i];
490                 struct sta_info *sta2;
491                 /* bss should always be set during operation, but it may be
492                  * NULL during reconfiguration. Assume the STA is not
493                  * associated to another BSS in that case to avoid NULL pointer
494                  * dereferences. */
495                 if (bss == hapd || bss == NULL)
496                         continue;
497                 sta2 = ap_get_sta(bss, sta->addr);
498                 if (!sta2)
499                         continue;
500
501                 ap_sta_disconnect(bss, sta2, sta2->addr,
502                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
503         }
504 }
505
506
507 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
508 {
509         struct hostapd_data *hapd = eloop_ctx;
510         struct sta_info *sta = timeout_ctx;
511
512         ap_sta_remove(hapd, sta);
513         mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
514 }
515
516
517 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
518                          u16 reason)
519 {
520         wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
521                    hapd->conf->iface, MAC2STR(sta->addr));
522         sta->flags &= ~WLAN_STA_ASSOC;
523         ap_sta_set_authorized(hapd, sta, 0);
524         sta->timeout_next = STA_DEAUTH;
525         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
526         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
527                                ap_handle_timer, hapd, sta);
528         accounting_sta_stop(hapd, sta);
529         ieee802_1x_free_station(sta);
530
531         sta->disassoc_reason = reason;
532         sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
533         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
534         eloop_register_timeout(hapd->iface->drv_flags &
535                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
536                                ap_sta_disassoc_cb_timeout, hapd, sta);
537 }
538
539
540 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
541 {
542         struct hostapd_data *hapd = eloop_ctx;
543         struct sta_info *sta = timeout_ctx;
544
545         ap_sta_remove(hapd, sta);
546         mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
547 }
548
549
550 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
551                            u16 reason)
552 {
553         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
554                    hapd->conf->iface, MAC2STR(sta->addr));
555         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
556         ap_sta_set_authorized(hapd, sta, 0);
557         sta->timeout_next = STA_REMOVE;
558         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
559         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
560                                ap_handle_timer, hapd, sta);
561         accounting_sta_stop(hapd, sta);
562         ieee802_1x_free_station(sta);
563
564         sta->deauth_reason = reason;
565         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
566         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
567         eloop_register_timeout(hapd->iface->drv_flags &
568                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
569                                ap_sta_deauth_cb_timeout, hapd, sta);
570 }
571
572
573 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
574                      int old_vlanid)
575 {
576 #ifndef CONFIG_NO_VLAN
577         const char *iface;
578         struct hostapd_vlan *vlan = NULL;
579         int ret;
580
581         /*
582          * Do not proceed furthur if the vlan id remains same. We do not want
583          * duplicate dynamic vlan entries.
584          */
585         if (sta->vlan_id == old_vlanid)
586                 return 0;
587
588         /*
589          * During 1x reauth, if the vlan id changes, then remove the old id and
590          * proceed furthur to add the new one.
591          */
592         if (old_vlanid > 0)
593                 vlan_remove_dynamic(hapd, old_vlanid);
594
595         iface = hapd->conf->iface;
596         if (sta->ssid->vlan[0])
597                 iface = sta->ssid->vlan;
598
599         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
600                 sta->vlan_id = 0;
601         else if (sta->vlan_id > 0) {
602                 vlan = hapd->conf->vlan;
603                 while (vlan) {
604                         if (vlan->vlan_id == sta->vlan_id ||
605                             vlan->vlan_id == VLAN_ID_WILDCARD) {
606                                 iface = vlan->ifname;
607                                 break;
608                         }
609                         vlan = vlan->next;
610                 }
611         }
612
613         if (sta->vlan_id > 0 && vlan == NULL) {
614                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
615                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
616                                "binding station to (vlan_id=%d)",
617                                sta->vlan_id);
618                 return -1;
619         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
620                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
621                 if (vlan == NULL) {
622                         hostapd_logger(hapd, sta->addr,
623                                        HOSTAPD_MODULE_IEEE80211,
624                                        HOSTAPD_LEVEL_DEBUG, "could not add "
625                                        "dynamic VLAN interface for vlan_id=%d",
626                                        sta->vlan_id);
627                         return -1;
628                 }
629
630                 iface = vlan->ifname;
631                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
632                         hostapd_logger(hapd, sta->addr,
633                                        HOSTAPD_MODULE_IEEE80211,
634                                        HOSTAPD_LEVEL_DEBUG, "could not "
635                                        "configure encryption for dynamic VLAN "
636                                        "interface for vlan_id=%d",
637                                        sta->vlan_id);
638                 }
639
640                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
641                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
642                                "interface '%s'", iface);
643         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
644                 if (sta->vlan_id > 0) {
645                         vlan->dynamic_vlan++;
646                         hostapd_logger(hapd, sta->addr,
647                                        HOSTAPD_MODULE_IEEE80211,
648                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
649                                        "dynamic VLAN interface '%s'", iface);
650                 }
651
652                 /*
653                  * Update encryption configuration for statically generated
654                  * VLAN interface. This is only used for static WEP
655                  * configuration for the case where hostapd did not yet know
656                  * which keys are to be used when the interface was added.
657                  */
658                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
659                         hostapd_logger(hapd, sta->addr,
660                                        HOSTAPD_MODULE_IEEE80211,
661                                        HOSTAPD_LEVEL_DEBUG, "could not "
662                                        "configure encryption for VLAN "
663                                        "interface for vlan_id=%d",
664                                        sta->vlan_id);
665                 }
666         }
667
668         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
669                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
670                        "'%s'", iface);
671
672         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
673                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
674
675         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
676         if (ret < 0) {
677                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
678                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
679                                "entry to vlan_id=%d", sta->vlan_id);
680         }
681         return ret;
682 #else /* CONFIG_NO_VLAN */
683         return 0;
684 #endif /* CONFIG_NO_VLAN */
685 }
686
687
688 #ifdef CONFIG_IEEE80211W
689
690 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
691 {
692         u32 tu;
693         struct os_time now, passed;
694         os_get_time(&now);
695         os_time_sub(&now, &sta->sa_query_start, &passed);
696         tu = (passed.sec * 1000000 + passed.usec) / 1024;
697         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
698                 hostapd_logger(hapd, sta->addr,
699                                HOSTAPD_MODULE_IEEE80211,
700                                HOSTAPD_LEVEL_DEBUG,
701                                "association SA Query timed out");
702                 sta->sa_query_timed_out = 1;
703                 os_free(sta->sa_query_trans_id);
704                 sta->sa_query_trans_id = NULL;
705                 sta->sa_query_count = 0;
706                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
707                 return 1;
708         }
709
710         return 0;
711 }
712
713
714 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
715 {
716         struct hostapd_data *hapd = eloop_ctx;
717         struct sta_info *sta = timeout_ctx;
718         unsigned int timeout, sec, usec;
719         u8 *trans_id, *nbuf;
720
721         if (sta->sa_query_count > 0 &&
722             ap_check_sa_query_timeout(hapd, sta))
723                 return;
724
725         nbuf = os_realloc(sta->sa_query_trans_id,
726                           (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
727         if (nbuf == NULL)
728                 return;
729         if (sta->sa_query_count == 0) {
730                 /* Starting a new SA Query procedure */
731                 os_get_time(&sta->sa_query_start);
732         }
733         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
734         sta->sa_query_trans_id = nbuf;
735         sta->sa_query_count++;
736
737         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
738
739         timeout = hapd->conf->assoc_sa_query_retry_timeout;
740         sec = ((timeout / 1000) * 1024) / 1000;
741         usec = (timeout % 1000) * 1024;
742         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
743
744         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
745                        HOSTAPD_LEVEL_DEBUG,
746                        "association SA Query attempt %d", sta->sa_query_count);
747
748 #ifdef NEED_AP_MLME
749         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
750 #endif /* NEED_AP_MLME */
751 }
752
753
754 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
755 {
756         ap_sa_query_timer(hapd, sta);
757 }
758
759
760 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
761 {
762         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
763         os_free(sta->sa_query_trans_id);
764         sta->sa_query_trans_id = NULL;
765         sta->sa_query_count = 0;
766 }
767
768 #endif /* CONFIG_IEEE80211W */
769
770
771 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
772                            int authorized)
773 {
774         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
775                 return;
776
777         if (authorized) {
778                 const u8 *dev_addr = NULL;
779 #ifdef CONFIG_P2P
780                 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
781 #endif /* CONFIG_P2P */
782                 if (dev_addr)
783                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
784                                 MACSTR " dev_addr=" MACSTR,
785                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
786                 else
787                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
788                                 MACSTR, MAC2STR(sta->addr));
789
790                 sta->flags |= WLAN_STA_AUTHORIZED;
791         } else {
792                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED MACSTR,
793                         MAC2STR(sta->addr));
794                 sta->flags &= ~WLAN_STA_AUTHORIZED;
795         }
796
797         if (hapd->sta_authorized_cb)
798                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
799                                         sta->addr, authorized);
800 }
801
802
803 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
804                        const u8 *addr, u16 reason)
805 {
806
807         if (sta == NULL && addr)
808                 sta = ap_get_sta(hapd, addr);
809
810         if (addr)
811                 hostapd_drv_sta_deauth(hapd, addr, reason);
812
813         if (sta == NULL)
814                 return;
815         ap_sta_set_authorized(hapd, sta, 0);
816         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
817         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
818         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
819                                ap_handle_timer, hapd, sta);
820         sta->timeout_next = STA_REMOVE;
821
822         sta->deauth_reason = reason;
823         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
824         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
825         eloop_register_timeout(hapd->iface->drv_flags &
826                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
827                                ap_sta_deauth_cb_timeout, hapd, sta);
828 }
829
830
831 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
832 {
833         if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
834                 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
835                 return;
836         }
837         sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
838         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
839         ap_sta_deauth_cb_timeout(hapd, sta);
840 }
841
842
843 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
844 {
845         if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
846                 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
847                 return;
848         }
849         sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
850         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
851         ap_sta_disassoc_cb_timeout(hapd, sta);
852 }