47b35f044321e8d061a7cc9ab9aa8db451db2631
[mech_eap.git] / src / ap / sta_info.c
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8
9 #include "utils/includes.h"
10
11 #include "utils/common.h"
12 #include "utils/eloop.h"
13 #include "common/ieee802_11_defs.h"
14 #include "common/wpa_ctrl.h"
15 #include "common/sae.h"
16 #include "radius/radius.h"
17 #include "radius/radius_client.h"
18 #include "p2p/p2p.h"
19 #include "fst/fst.h"
20 #include "hostapd.h"
21 #include "accounting.h"
22 #include "ieee802_1x.h"
23 #include "ieee802_11.h"
24 #include "ieee802_11_auth.h"
25 #include "wpa_auth.h"
26 #include "preauth_auth.h"
27 #include "ap_config.h"
28 #include "beacon.h"
29 #include "ap_mlme.h"
30 #include "vlan_init.h"
31 #include "p2p_hostapd.h"
32 #include "ap_drv_ops.h"
33 #include "gas_serv.h"
34 #include "wnm_ap.h"
35 #include "ndisc_snoop.h"
36 #include "sta_info.h"
37 #include "vlan.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_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
43 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
44 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
45 #ifdef CONFIG_IEEE80211W
46 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
47 #endif /* CONFIG_IEEE80211W */
48 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
49
50 int ap_for_each_sta(struct hostapd_data *hapd,
51                     int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
52                               void *ctx),
53                     void *ctx)
54 {
55         struct sta_info *sta;
56
57         for (sta = hapd->sta_list; sta; sta = sta->next) {
58                 if (cb(hapd, sta, ctx))
59                         return 1;
60         }
61
62         return 0;
63 }
64
65
66 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
67 {
68         struct sta_info *s;
69
70         s = hapd->sta_hash[STA_HASH(sta)];
71         while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
72                 s = s->hnext;
73         return s;
74 }
75
76
77 #ifdef CONFIG_P2P
78 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
79 {
80         struct sta_info *sta;
81
82         for (sta = hapd->sta_list; sta; sta = sta->next) {
83                 const u8 *p2p_dev_addr;
84
85                 if (sta->p2p_ie == NULL)
86                         continue;
87
88                 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
89                 if (p2p_dev_addr == NULL)
90                         continue;
91
92                 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
93                         return sta;
94         }
95
96         return NULL;
97 }
98 #endif /* CONFIG_P2P */
99
100
101 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
102 {
103         struct sta_info *tmp;
104
105         if (hapd->sta_list == sta) {
106                 hapd->sta_list = sta->next;
107                 return;
108         }
109
110         tmp = hapd->sta_list;
111         while (tmp != NULL && tmp->next != sta)
112                 tmp = tmp->next;
113         if (tmp == NULL) {
114                 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
115                            "list.", MAC2STR(sta->addr));
116         } else
117                 tmp->next = sta->next;
118 }
119
120
121 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
122 {
123         sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
124         hapd->sta_hash[STA_HASH(sta->addr)] = sta;
125 }
126
127
128 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
129 {
130         struct sta_info *s;
131
132         s = hapd->sta_hash[STA_HASH(sta->addr)];
133         if (s == NULL) return;
134         if (os_memcmp(s->addr, sta->addr, 6) == 0) {
135                 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
136                 return;
137         }
138
139         while (s->hnext != NULL &&
140                os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
141                 s = s->hnext;
142         if (s->hnext != NULL)
143                 s->hnext = s->hnext->hnext;
144         else
145                 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
146                            " from hash table", MAC2STR(sta->addr));
147 }
148
149
150 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
151 {
152         sta_ip6addr_del(hapd, sta);
153 }
154
155
156 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
157 {
158         int set_beacon = 0;
159
160         accounting_sta_stop(hapd, sta);
161
162         /* just in case */
163         ap_sta_set_authorized(hapd, sta, 0);
164
165         if (sta->flags & WLAN_STA_WDS)
166                 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
167
168         if (sta->ipaddr)
169                 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
170         ap_sta_ip6addr_del(hapd, sta);
171
172         if (!hapd->iface->driver_ap_teardown &&
173             !(sta->flags & WLAN_STA_PREAUTH))
174                 hostapd_drv_sta_remove(hapd, sta->addr);
175
176         ap_sta_hash_del(hapd, sta);
177         ap_sta_list_del(hapd, sta);
178
179         if (sta->aid > 0)
180                 hapd->sta_aid[(sta->aid - 1) / 32] &=
181                         ~BIT((sta->aid - 1) % 32);
182
183         hapd->num_sta--;
184         if (sta->nonerp_set) {
185                 sta->nonerp_set = 0;
186                 hapd->iface->num_sta_non_erp--;
187                 if (hapd->iface->num_sta_non_erp == 0)
188                         set_beacon++;
189         }
190
191         if (sta->no_short_slot_time_set) {
192                 sta->no_short_slot_time_set = 0;
193                 hapd->iface->num_sta_no_short_slot_time--;
194                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
195                     && hapd->iface->num_sta_no_short_slot_time == 0)
196                         set_beacon++;
197         }
198
199         if (sta->no_short_preamble_set) {
200                 sta->no_short_preamble_set = 0;
201                 hapd->iface->num_sta_no_short_preamble--;
202                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
203                     && hapd->iface->num_sta_no_short_preamble == 0)
204                         set_beacon++;
205         }
206
207         if (sta->no_ht_gf_set) {
208                 sta->no_ht_gf_set = 0;
209                 hapd->iface->num_sta_ht_no_gf--;
210         }
211
212         if (sta->no_ht_set) {
213                 sta->no_ht_set = 0;
214                 hapd->iface->num_sta_no_ht--;
215         }
216
217         if (sta->ht_20mhz_set) {
218                 sta->ht_20mhz_set = 0;
219                 hapd->iface->num_sta_ht_20mhz--;
220         }
221
222 #ifdef CONFIG_IEEE80211N
223         ht40_intolerant_remove(hapd->iface, sta);
224 #endif /* CONFIG_IEEE80211N */
225
226 #ifdef CONFIG_P2P
227         if (sta->no_p2p_set) {
228                 sta->no_p2p_set = 0;
229                 hapd->num_sta_no_p2p--;
230                 if (hapd->num_sta_no_p2p == 0)
231                         hostapd_p2p_non_p2p_sta_disconnected(hapd);
232         }
233 #endif /* CONFIG_P2P */
234
235 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
236         if (hostapd_ht_operation_update(hapd->iface) > 0)
237                 set_beacon++;
238 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
239
240 #ifdef CONFIG_MESH
241         if (hapd->mesh_sta_free_cb)
242                 hapd->mesh_sta_free_cb(hapd, sta);
243 #endif /* CONFIG_MESH */
244
245         if (set_beacon)
246                 ieee802_11_set_beacons(hapd->iface);
247
248         wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
249                    __func__, MAC2STR(sta->addr));
250         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
251         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
252         eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
253         ap_sta_clear_disconnect_timeouts(hapd, sta);
254         sae_clear_retransmit_timer(hapd, sta);
255
256         ieee802_1x_free_station(hapd, sta);
257         wpa_auth_sta_deinit(sta->wpa_sm);
258         rsn_preauth_free_station(hapd, sta);
259 #ifndef CONFIG_NO_RADIUS
260         if (hapd->radius)
261                 radius_client_flush_auth(hapd->radius, sta->addr);
262 #endif /* CONFIG_NO_RADIUS */
263
264 #ifndef CONFIG_NO_VLAN
265         /*
266          * sta->wpa_sm->group needs to be released before so that
267          * vlan_remove_dynamic() can check that no stations are left on the
268          * AP_VLAN netdev.
269          */
270         if (sta->vlan_id)
271                 vlan_remove_dynamic(hapd, sta->vlan_id);
272         if (sta->vlan_id_bound) {
273                 /*
274                  * Need to remove the STA entry before potentially removing the
275                  * VLAN.
276                  */
277                 if (hapd->iface->driver_ap_teardown &&
278                     !(sta->flags & WLAN_STA_PREAUTH))
279                         hostapd_drv_sta_remove(hapd, sta->addr);
280                 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
281         }
282 #endif /* CONFIG_NO_VLAN */
283
284         os_free(sta->challenge);
285
286 #ifdef CONFIG_IEEE80211W
287         os_free(sta->sa_query_trans_id);
288         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
289 #endif /* CONFIG_IEEE80211W */
290
291 #ifdef CONFIG_P2P
292         p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
293 #endif /* CONFIG_P2P */
294
295 #ifdef CONFIG_INTERWORKING
296         if (sta->gas_dialog) {
297                 int i;
298                 for (i = 0; i < GAS_DIALOG_MAX; i++)
299                         gas_serv_dialog_clear(&sta->gas_dialog[i]);
300                 os_free(sta->gas_dialog);
301         }
302 #endif /* CONFIG_INTERWORKING */
303
304         wpabuf_free(sta->wps_ie);
305         wpabuf_free(sta->p2p_ie);
306         wpabuf_free(sta->hs20_ie);
307 #ifdef CONFIG_FST
308         wpabuf_free(sta->mb_ies);
309 #endif /* CONFIG_FST */
310
311         os_free(sta->ht_capabilities);
312         os_free(sta->vht_capabilities);
313         hostapd_free_psk_list(sta->psk);
314         os_free(sta->identity);
315         os_free(sta->radius_cui);
316         os_free(sta->remediation_url);
317         wpabuf_free(sta->hs20_deauth_req);
318         os_free(sta->hs20_session_info_url);
319
320 #ifdef CONFIG_SAE
321         sae_clear_data(sta->sae);
322         os_free(sta->sae);
323 #endif /* CONFIG_SAE */
324
325         os_free(sta);
326 }
327
328
329 void hostapd_free_stas(struct hostapd_data *hapd)
330 {
331         struct sta_info *sta, *prev;
332
333         sta = hapd->sta_list;
334
335         while (sta) {
336                 prev = sta;
337                 if (sta->flags & WLAN_STA_AUTH) {
338                         mlme_deauthenticate_indication(
339                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
340                 }
341                 sta = sta->next;
342                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
343                            MAC2STR(prev->addr));
344                 ap_free_sta(hapd, prev);
345         }
346 }
347
348
349 /**
350  * ap_handle_timer - Per STA timer handler
351  * @eloop_ctx: struct hostapd_data *
352  * @timeout_ctx: struct sta_info *
353  *
354  * This function is called to check station activity and to remove inactive
355  * stations.
356  */
357 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
358 {
359         struct hostapd_data *hapd = eloop_ctx;
360         struct sta_info *sta = timeout_ctx;
361         unsigned long next_time = 0;
362         int reason;
363
364         wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
365                    hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
366                    sta->timeout_next);
367         if (sta->timeout_next == STA_REMOVE) {
368                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
369                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
370                                "local deauth request");
371                 ap_free_sta(hapd, sta);
372                 return;
373         }
374
375         if ((sta->flags & WLAN_STA_ASSOC) &&
376             (sta->timeout_next == STA_NULLFUNC ||
377              sta->timeout_next == STA_DISASSOC)) {
378                 int inactive_sec;
379                 /*
380                  * Add random value to timeout so that we don't end up bouncing
381                  * all stations at the same time if we have lots of associated
382                  * stations that are idle (but keep re-associating).
383                  */
384                 int fuzz = os_random() % 20;
385                 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
386                 if (inactive_sec == -1) {
387                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
388                                 "Check inactivity: Could not "
389                                 "get station info from kernel driver for "
390                                 MACSTR, MAC2STR(sta->addr));
391                         /*
392                          * The driver may not support this functionality.
393                          * Anyway, try again after the next inactivity timeout,
394                          * but do not disconnect the station now.
395                          */
396                         next_time = hapd->conf->ap_max_inactivity + fuzz;
397                 } else if (inactive_sec == -ENOENT) {
398                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
399                                 "Station " MACSTR " has lost its driver entry",
400                                 MAC2STR(sta->addr));
401
402                         /* Avoid sending client probe on removed client */
403                         sta->timeout_next = STA_DISASSOC;
404                         goto skip_poll;
405                 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
406                         /* station activity detected; reset timeout state */
407                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
408                                 "Station " MACSTR " has been active %is ago",
409                                 MAC2STR(sta->addr), inactive_sec);
410                         sta->timeout_next = STA_NULLFUNC;
411                         next_time = hapd->conf->ap_max_inactivity + fuzz -
412                                 inactive_sec;
413                 } else {
414                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
415                                 "Station " MACSTR " has been "
416                                 "inactive too long: %d sec, max allowed: %d",
417                                 MAC2STR(sta->addr), inactive_sec,
418                                 hapd->conf->ap_max_inactivity);
419
420                         if (hapd->conf->skip_inactivity_poll)
421                                 sta->timeout_next = STA_DISASSOC;
422                 }
423         }
424
425         if ((sta->flags & WLAN_STA_ASSOC) &&
426             sta->timeout_next == STA_DISASSOC &&
427             !(sta->flags & WLAN_STA_PENDING_POLL) &&
428             !hapd->conf->skip_inactivity_poll) {
429                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
430                         " has ACKed data poll", MAC2STR(sta->addr));
431                 /* data nullfunc frame poll did not produce TX errors; assume
432                  * station ACKed it */
433                 sta->timeout_next = STA_NULLFUNC;
434                 next_time = hapd->conf->ap_max_inactivity;
435         }
436
437 skip_poll:
438         if (next_time) {
439                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
440                            "for " MACSTR " (%lu seconds)",
441                            __func__, MAC2STR(sta->addr), next_time);
442                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
443                                        sta);
444                 return;
445         }
446
447         if (sta->timeout_next == STA_NULLFUNC &&
448             (sta->flags & WLAN_STA_ASSOC)) {
449                 wpa_printf(MSG_DEBUG, "  Polling STA");
450                 sta->flags |= WLAN_STA_PENDING_POLL;
451                 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
452                                         sta->flags & WLAN_STA_WMM);
453         } else if (sta->timeout_next != STA_REMOVE) {
454                 int deauth = sta->timeout_next == STA_DEAUTH;
455
456                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
457                         "Timeout, sending %s info to STA " MACSTR,
458                         deauth ? "deauthentication" : "disassociation",
459                         MAC2STR(sta->addr));
460
461                 if (deauth) {
462                         hostapd_drv_sta_deauth(
463                                 hapd, sta->addr,
464                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
465                 } else {
466                         reason = (sta->timeout_next == STA_DISASSOC) ?
467                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
468                                 WLAN_REASON_PREV_AUTH_NOT_VALID;
469
470                         hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
471                 }
472         }
473
474         switch (sta->timeout_next) {
475         case STA_NULLFUNC:
476                 sta->timeout_next = STA_DISASSOC;
477                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
478                            "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
479                            __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
480                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
481                                        hapd, sta);
482                 break;
483         case STA_DISASSOC:
484         case STA_DISASSOC_FROM_CLI:
485                 ap_sta_set_authorized(hapd, sta, 0);
486                 sta->flags &= ~WLAN_STA_ASSOC;
487                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
488                 if (!sta->acct_terminate_cause)
489                         sta->acct_terminate_cause =
490                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
491                 accounting_sta_stop(hapd, sta);
492                 ieee802_1x_free_station(hapd, sta);
493                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
494                                HOSTAPD_LEVEL_INFO, "disassociated due to "
495                                "inactivity");
496                 reason = (sta->timeout_next == STA_DISASSOC) ?
497                         WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
498                         WLAN_REASON_PREV_AUTH_NOT_VALID;
499                 sta->timeout_next = STA_DEAUTH;
500                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
501                            "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
502                            __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
503                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
504                                        hapd, sta);
505                 mlme_disassociate_indication(hapd, sta, reason);
506                 break;
507         case STA_DEAUTH:
508         case STA_REMOVE:
509                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
510                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
511                                "inactivity (timer DEAUTH/REMOVE)");
512                 if (!sta->acct_terminate_cause)
513                         sta->acct_terminate_cause =
514                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
515                 mlme_deauthenticate_indication(
516                         hapd, sta,
517                         WLAN_REASON_PREV_AUTH_NOT_VALID);
518                 ap_free_sta(hapd, sta);
519                 break;
520         }
521 }
522
523
524 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
525 {
526         struct hostapd_data *hapd = eloop_ctx;
527         struct sta_info *sta = timeout_ctx;
528
529         wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
530                    hapd->conf->iface, MAC2STR(sta->addr));
531         if (!(sta->flags & WLAN_STA_AUTH)) {
532                 if (sta->flags & WLAN_STA_GAS) {
533                         wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
534                                    "entry " MACSTR, MAC2STR(sta->addr));
535                         ap_free_sta(hapd, sta);
536                 }
537                 return;
538         }
539
540         hostapd_drv_sta_deauth(hapd, sta->addr,
541                                WLAN_REASON_PREV_AUTH_NOT_VALID);
542         mlme_deauthenticate_indication(hapd, sta,
543                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
544         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
545                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
546                        "session timeout");
547         sta->acct_terminate_cause =
548                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
549         ap_free_sta(hapd, sta);
550 }
551
552
553 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
554                               u32 session_timeout)
555 {
556         if (eloop_replenish_timeout(session_timeout, 0,
557                                     ap_handle_session_timer, hapd, sta) == 1) {
558                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
559                                HOSTAPD_LEVEL_DEBUG, "setting session timeout "
560                                "to %d seconds", session_timeout);
561         }
562 }
563
564
565 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
566                             u32 session_timeout)
567 {
568         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
569                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
570                        "seconds", session_timeout);
571         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
572         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
573                                hapd, sta);
574 }
575
576
577 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
578 {
579         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
580 }
581
582
583 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
584 {
585 #ifdef CONFIG_WNM
586         struct hostapd_data *hapd = eloop_ctx;
587         struct sta_info *sta = timeout_ctx;
588
589         wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
590                    MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
591         if (sta->hs20_session_info_url == NULL)
592                 return;
593
594         wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
595                                        sta->hs20_disassoc_timer);
596 #endif /* CONFIG_WNM */
597 }
598
599
600 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
601                                     struct sta_info *sta, int warning_time)
602 {
603         eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
604         eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
605                                hapd, sta);
606 }
607
608
609 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
610 {
611         struct sta_info *sta;
612
613         sta = ap_get_sta(hapd, addr);
614         if (sta)
615                 return sta;
616
617         wpa_printf(MSG_DEBUG, "  New STA");
618         if (hapd->num_sta >= hapd->conf->max_num_sta) {
619                 /* FIX: might try to remove some old STAs first? */
620                 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
621                            hapd->num_sta, hapd->conf->max_num_sta);
622                 return NULL;
623         }
624
625         sta = os_zalloc(sizeof(struct sta_info));
626         if (sta == NULL) {
627                 wpa_printf(MSG_ERROR, "malloc failed");
628                 return NULL;
629         }
630         sta->acct_interim_interval = hapd->conf->acct_interim_interval;
631         if (accounting_sta_get_id(hapd, sta) < 0) {
632                 os_free(sta);
633                 return NULL;
634         }
635
636         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
637                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
638                            "for " MACSTR " (%d seconds - ap_max_inactivity)",
639                            __func__, MAC2STR(addr),
640                            hapd->conf->ap_max_inactivity);
641                 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
642                                        ap_handle_timer, hapd, sta);
643         }
644
645         /* initialize STA info data */
646         os_memcpy(sta->addr, addr, ETH_ALEN);
647         sta->next = hapd->sta_list;
648         hapd->sta_list = sta;
649         hapd->num_sta++;
650         ap_sta_hash_add(hapd, sta);
651         ap_sta_remove_in_other_bss(hapd, sta);
652         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
653         dl_list_init(&sta->ip6addr);
654
655         return sta;
656 }
657
658
659 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
660 {
661         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
662
663         if (sta->ipaddr)
664                 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
665         ap_sta_ip6addr_del(hapd, sta);
666
667         wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
668                    hapd->conf->iface, MAC2STR(sta->addr));
669         if (hostapd_drv_sta_remove(hapd, sta->addr) &&
670             sta->flags & WLAN_STA_ASSOC) {
671                 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
672                            " from kernel driver",
673                            hapd->conf->iface, MAC2STR(sta->addr));
674                 return -1;
675         }
676         return 0;
677 }
678
679
680 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
681                                        struct sta_info *sta)
682 {
683         struct hostapd_iface *iface = hapd->iface;
684         size_t i;
685
686         for (i = 0; i < iface->num_bss; i++) {
687                 struct hostapd_data *bss = iface->bss[i];
688                 struct sta_info *sta2;
689                 /* bss should always be set during operation, but it may be
690                  * NULL during reconfiguration. Assume the STA is not
691                  * associated to another BSS in that case to avoid NULL pointer
692                  * dereferences. */
693                 if (bss == hapd || bss == NULL)
694                         continue;
695                 sta2 = ap_get_sta(bss, sta->addr);
696                 if (!sta2)
697                         continue;
698
699                 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
700                            " association from another BSS %s",
701                            hapd->conf->iface, MAC2STR(sta2->addr),
702                            bss->conf->iface);
703                 ap_sta_disconnect(bss, sta2, sta2->addr,
704                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
705         }
706 }
707
708
709 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
710 {
711         struct hostapd_data *hapd = eloop_ctx;
712         struct sta_info *sta = timeout_ctx;
713
714         wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
715                    hapd->conf->iface, MAC2STR(sta->addr));
716         ap_sta_remove(hapd, sta);
717         mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
718 }
719
720
721 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
722                          u16 reason)
723 {
724         wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
725                    hapd->conf->iface, MAC2STR(sta->addr));
726         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
727         sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
728         ap_sta_set_authorized(hapd, sta, 0);
729         sta->timeout_next = STA_DEAUTH;
730         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
731                    "for " MACSTR " (%d seconds - "
732                    "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
733                    __func__, MAC2STR(sta->addr),
734                    AP_MAX_INACTIVITY_AFTER_DISASSOC);
735         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
736         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
737                                ap_handle_timer, hapd, sta);
738         accounting_sta_stop(hapd, sta);
739         ieee802_1x_free_station(hapd, sta);
740
741         sta->disassoc_reason = reason;
742         sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
743         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
744         eloop_register_timeout(hapd->iface->drv_flags &
745                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
746                                ap_sta_disassoc_cb_timeout, hapd, sta);
747 }
748
749
750 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
751 {
752         struct hostapd_data *hapd = eloop_ctx;
753         struct sta_info *sta = timeout_ctx;
754
755         wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
756                    hapd->conf->iface, MAC2STR(sta->addr));
757         ap_sta_remove(hapd, sta);
758         mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
759 }
760
761
762 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
763                            u16 reason)
764 {
765         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
766                    hapd->conf->iface, MAC2STR(sta->addr));
767         sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
768         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
769         ap_sta_set_authorized(hapd, sta, 0);
770         sta->timeout_next = STA_REMOVE;
771         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
772                    "for " MACSTR " (%d seconds - "
773                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
774                    __func__, MAC2STR(sta->addr),
775                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
776         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
777         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
778                                ap_handle_timer, hapd, sta);
779         accounting_sta_stop(hapd, sta);
780         ieee802_1x_free_station(hapd, sta);
781
782         sta->deauth_reason = reason;
783         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
784         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
785         eloop_register_timeout(hapd->iface->drv_flags &
786                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
787                                ap_sta_deauth_cb_timeout, hapd, sta);
788 }
789
790
791 #ifdef CONFIG_WPS
792 int ap_sta_wps_cancel(struct hostapd_data *hapd,
793                       struct sta_info *sta, void *ctx)
794 {
795         if (sta && (sta->flags & WLAN_STA_WPS)) {
796                 ap_sta_deauthenticate(hapd, sta,
797                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
798                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
799                            __func__, MAC2STR(sta->addr));
800                 return 1;
801         }
802
803         return 0;
804 }
805 #endif /* CONFIG_WPS */
806
807
808 int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
809                     struct vlan_description *vlan_desc)
810 {
811         struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
812         int old_vlan_id, vlan_id = 0, ret = 0;
813
814         if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED) {
815                 vlan_desc = NULL;
816         } else if (vlan_desc && vlan_desc->notempty) {
817                 if (!vlan_compare(vlan_desc, sta->vlan_desc))
818                         return 0; /* nothing to change */
819
820                 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
821                         if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
822                                 break;
823                         if (vlan->vlan_id == VLAN_ID_WILDCARD)
824                                 wildcard_vlan = vlan;
825                 }
826                 if (vlan) {
827                         vlan_id = vlan->vlan_id;
828                 } else if (wildcard_vlan) {
829                         vlan = wildcard_vlan;
830                         vlan_id = vlan_desc->untagged;
831                 } else {
832                         hostapd_logger(hapd, sta->addr,
833                                        HOSTAPD_MODULE_IEEE80211,
834                                        HOSTAPD_LEVEL_DEBUG,
835                                        "missing VLAN and wildcard for vlan=%d",
836                                        vlan_desc->untagged);
837                         vlan_id = 0;
838                         ret = -1;
839                         goto done;
840                 }
841         }
842
843         if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
844                 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
845                 if (vlan == NULL) {
846                         hostapd_logger(hapd, sta->addr,
847                                        HOSTAPD_MODULE_IEEE80211,
848                                        HOSTAPD_LEVEL_DEBUG,
849                                        "could not add dynamic VLAN interface for vlan=%d",
850                                        vlan_desc->untagged);
851                         vlan_id = 0;
852                         ret = -1;
853                         goto done;
854                 }
855
856                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
857                                HOSTAPD_LEVEL_DEBUG,
858                                "added new dynamic VLAN interface '%s'",
859                                vlan->ifname);
860         } else if (vlan && vlan->dynamic_vlan > 0) {
861                 vlan->dynamic_vlan++;
862                 hostapd_logger(hapd, sta->addr,
863                                HOSTAPD_MODULE_IEEE80211,
864                                HOSTAPD_LEVEL_DEBUG,
865                                "updated existing dynamic VLAN interface '%s'",
866                                vlan->ifname);
867         }
868 done:
869         old_vlan_id = sta->vlan_id;
870         sta->vlan_id = vlan_id;
871         sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
872
873         if (vlan_id != old_vlan_id && old_vlan_id)
874                 vlan_remove_dynamic(hapd, old_vlan_id);
875
876         return ret;
877 }
878
879
880 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
881 {
882 #ifndef CONFIG_NO_VLAN
883         const char *iface;
884         struct hostapd_vlan *vlan = NULL;
885         int ret;
886         int old_vlanid = sta->vlan_id_bound;
887
888         iface = hapd->conf->iface;
889         if (hapd->conf->ssid.vlan[0])
890                 iface = hapd->conf->ssid.vlan;
891
892         if (sta->vlan_id > 0) {
893                 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
894                         if (vlan->vlan_id == sta->vlan_id)
895                                 break;
896                 }
897                 if (vlan)
898                         iface = vlan->ifname;
899         }
900
901         /*
902          * Do not increment ref counters if the VLAN ID remains same, but do
903          * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
904          * have been called before.
905          */
906         if (sta->vlan_id == old_vlanid)
907                 goto skip_counting;
908
909         if (sta->vlan_id > 0 && vlan == NULL) {
910                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
911                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
912                                "binding station to (vlan_id=%d)",
913                                sta->vlan_id);
914                 ret = -1;
915                 goto done;
916         } else if (vlan && vlan->dynamic_vlan > 0) {
917                 vlan->dynamic_vlan++;
918                 hostapd_logger(hapd, sta->addr,
919                                HOSTAPD_MODULE_IEEE80211,
920                                HOSTAPD_LEVEL_DEBUG,
921                                "updated existing dynamic VLAN interface '%s'",
922                                iface);
923         }
924
925         /* ref counters have been increased, so mark the station */
926         sta->vlan_id_bound = sta->vlan_id;
927
928 skip_counting:
929         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
930                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
931                        "'%s'", iface);
932
933         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
934                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
935
936         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
937         if (ret < 0) {
938                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
939                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
940                                "entry to vlan_id=%d", sta->vlan_id);
941         }
942
943         /* During 1x reauth, if the vlan id changes, then remove the old id. */
944         if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
945                 vlan_remove_dynamic(hapd, old_vlanid);
946 done:
947
948         return ret;
949 #else /* CONFIG_NO_VLAN */
950         return 0;
951 #endif /* CONFIG_NO_VLAN */
952 }
953
954
955 #ifdef CONFIG_IEEE80211W
956
957 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
958 {
959         u32 tu;
960         struct os_reltime now, passed;
961         os_get_reltime(&now);
962         os_reltime_sub(&now, &sta->sa_query_start, &passed);
963         tu = (passed.sec * 1000000 + passed.usec) / 1024;
964         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
965                 hostapd_logger(hapd, sta->addr,
966                                HOSTAPD_MODULE_IEEE80211,
967                                HOSTAPD_LEVEL_DEBUG,
968                                "association SA Query timed out");
969                 sta->sa_query_timed_out = 1;
970                 os_free(sta->sa_query_trans_id);
971                 sta->sa_query_trans_id = NULL;
972                 sta->sa_query_count = 0;
973                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
974                 return 1;
975         }
976
977         return 0;
978 }
979
980
981 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
982 {
983         struct hostapd_data *hapd = eloop_ctx;
984         struct sta_info *sta = timeout_ctx;
985         unsigned int timeout, sec, usec;
986         u8 *trans_id, *nbuf;
987
988         wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
989                    " (count=%d)",
990                    hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
991
992         if (sta->sa_query_count > 0 &&
993             ap_check_sa_query_timeout(hapd, sta))
994                 return;
995
996         nbuf = os_realloc_array(sta->sa_query_trans_id,
997                                 sta->sa_query_count + 1,
998                                 WLAN_SA_QUERY_TR_ID_LEN);
999         if (nbuf == NULL)
1000                 return;
1001         if (sta->sa_query_count == 0) {
1002                 /* Starting a new SA Query procedure */
1003                 os_get_reltime(&sta->sa_query_start);
1004         }
1005         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1006         sta->sa_query_trans_id = nbuf;
1007         sta->sa_query_count++;
1008
1009         if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1010                 /*
1011                  * We don't really care which ID is used here, so simply
1012                  * hardcode this if the mostly theoretical os_get_random()
1013                  * failure happens.
1014                  */
1015                 trans_id[0] = 0x12;
1016                 trans_id[1] = 0x34;
1017         }
1018
1019         timeout = hapd->conf->assoc_sa_query_retry_timeout;
1020         sec = ((timeout / 1000) * 1024) / 1000;
1021         usec = (timeout % 1000) * 1024;
1022         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1023
1024         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1025                        HOSTAPD_LEVEL_DEBUG,
1026                        "association SA Query attempt %d", sta->sa_query_count);
1027
1028         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
1029 }
1030
1031
1032 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1033 {
1034         ap_sa_query_timer(hapd, sta);
1035 }
1036
1037
1038 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1039 {
1040         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1041         os_free(sta->sa_query_trans_id);
1042         sta->sa_query_trans_id = NULL;
1043         sta->sa_query_count = 0;
1044 }
1045
1046 #endif /* CONFIG_IEEE80211W */
1047
1048
1049 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1050                            int authorized)
1051 {
1052         const u8 *dev_addr = NULL;
1053         char buf[100];
1054 #ifdef CONFIG_P2P
1055         u8 addr[ETH_ALEN];
1056         u8 ip_addr_buf[4];
1057 #endif /* CONFIG_P2P */
1058
1059         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1060                 return;
1061
1062         if (authorized)
1063                 sta->flags |= WLAN_STA_AUTHORIZED;
1064         else
1065                 sta->flags &= ~WLAN_STA_AUTHORIZED;
1066
1067 #ifdef CONFIG_P2P
1068         if (hapd->p2p_group == NULL) {
1069                 if (sta->p2p_ie != NULL &&
1070                     p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1071                         dev_addr = addr;
1072         } else
1073                 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1074
1075         if (dev_addr)
1076                 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1077                             MAC2STR(sta->addr), MAC2STR(dev_addr));
1078         else
1079 #endif /* CONFIG_P2P */
1080                 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1081
1082         if (hapd->sta_authorized_cb)
1083                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1084                                         sta->addr, authorized, dev_addr);
1085
1086         if (authorized) {
1087                 char ip_addr[100];
1088                 ip_addr[0] = '\0';
1089 #ifdef CONFIG_P2P
1090                 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1091                         os_snprintf(ip_addr, sizeof(ip_addr),
1092                                     " ip_addr=%u.%u.%u.%u",
1093                                     ip_addr_buf[0], ip_addr_buf[1],
1094                                     ip_addr_buf[2], ip_addr_buf[3]);
1095                 }
1096 #endif /* CONFIG_P2P */
1097
1098                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1099                         buf, ip_addr);
1100
1101                 if (hapd->msg_ctx_parent &&
1102                     hapd->msg_ctx_parent != hapd->msg_ctx)
1103                         wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1104                                           AP_STA_CONNECTED "%s%s",
1105                                           buf, ip_addr);
1106         } else {
1107                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1108
1109                 if (hapd->msg_ctx_parent &&
1110                     hapd->msg_ctx_parent != hapd->msg_ctx)
1111                         wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1112                                           AP_STA_DISCONNECTED "%s", buf);
1113         }
1114
1115 #ifdef CONFIG_FST
1116         if (hapd->iface->fst) {
1117                 if (authorized)
1118                         fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1119                 else
1120                         fst_notify_peer_disconnected(hapd->iface->fst,
1121                                                      sta->addr);
1122         }
1123 #endif /* CONFIG_FST */
1124 }
1125
1126
1127 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1128                        const u8 *addr, u16 reason)
1129 {
1130         if (sta)
1131                 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1132                            hapd->conf->iface, __func__, MAC2STR(sta->addr),
1133                            reason);
1134         else if (addr)
1135                 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1136                            hapd->conf->iface, __func__, MAC2STR(addr),
1137                            reason);
1138
1139         if (sta == NULL && addr)
1140                 sta = ap_get_sta(hapd, addr);
1141
1142         if (addr)
1143                 hostapd_drv_sta_deauth(hapd, addr, reason);
1144
1145         if (sta == NULL)
1146                 return;
1147         ap_sta_set_authorized(hapd, sta, 0);
1148         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1149         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1150         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1151         wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
1152                    "for " MACSTR " (%d seconds - "
1153                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1154                    hapd->conf->iface, __func__, MAC2STR(sta->addr),
1155                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
1156         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1157         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1158                                ap_handle_timer, hapd, sta);
1159         sta->timeout_next = STA_REMOVE;
1160
1161         sta->deauth_reason = reason;
1162         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1163         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1164         eloop_register_timeout(hapd->iface->drv_flags &
1165                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1166                                ap_sta_deauth_cb_timeout, hapd, sta);
1167 }
1168
1169
1170 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1171 {
1172         if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1173                 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1174                 return;
1175         }
1176         sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1177         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1178         ap_sta_deauth_cb_timeout(hapd, sta);
1179 }
1180
1181
1182 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1183 {
1184         if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1185                 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1186                 return;
1187         }
1188         sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1189         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1190         ap_sta_disassoc_cb_timeout(hapd, sta);
1191 }
1192
1193
1194 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1195                                       struct sta_info *sta)
1196 {
1197         if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1198                 wpa_printf(MSG_DEBUG,
1199                            "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1200                            MACSTR,
1201                            hapd->conf->iface, MAC2STR(sta->addr));
1202         if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1203                 wpa_printf(MSG_DEBUG,
1204                            "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1205                            MACSTR,
1206                            hapd->conf->iface, MAC2STR(sta->addr));
1207 }
1208
1209
1210 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1211 {
1212         int res;
1213
1214         buf[0] = '\0';
1215         res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1216                           (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1217                           (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1218                           (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1219                           (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1220                            ""),
1221                           (flags & WLAN_STA_SHORT_PREAMBLE ?
1222                            "[SHORT_PREAMBLE]" : ""),
1223                           (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1224                           (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1225                           (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1226                           (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1227                           (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1228                           (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1229                           (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1230                           (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1231                           (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1232                           (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1233                           (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1234                           (flags & WLAN_STA_WNM_SLEEP_MODE ?
1235                            "[WNM_SLEEP_MODE]" : ""));
1236         if (os_snprintf_error(buflen, res))
1237                 res = -1;
1238
1239         return res;
1240 }