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