VLAN: Add per-STA vif option
[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 static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
809 {
810         struct hostapd_vlan *vlan;
811         int vlan_id = MAX_VLAN_ID + 2;
812
813 retry:
814         for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
815                 if (vlan->vlan_id == vlan_id) {
816                         vlan_id++;
817                         goto retry;
818                 }
819         }
820         return vlan_id;
821 }
822
823
824 int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
825                     struct vlan_description *vlan_desc)
826 {
827         struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
828         int old_vlan_id, vlan_id = 0, ret = 0;
829
830         if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
831                 vlan_desc = NULL;
832
833         /* Check if there is something to do */
834         if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
835                 /* This sta is lacking its own vif */
836         } else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
837                    !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
838                 /* sta->vlan_id needs to be reset */
839         } else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
840                 return 0; /* nothing to change */
841         }
842
843         /* Now the real VLAN changed or the STA just needs its own vif */
844         if (hapd->conf->ssid.per_sta_vif) {
845                 /* Assign a new vif, always */
846                 /* find a free vlan_id sufficiently big */
847                 vlan_id = ap_sta_get_free_vlan_id(hapd);
848                 /* Get wildcard VLAN */
849                 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
850                         if (vlan->vlan_id == VLAN_ID_WILDCARD)
851                                 break;
852                 }
853                 if (!vlan) {
854                         hostapd_logger(hapd, sta->addr,
855                                        HOSTAPD_MODULE_IEEE80211,
856                                        HOSTAPD_LEVEL_DEBUG,
857                                        "per_sta_vif missing wildcard");
858                         vlan_id = 0;
859                         ret = -1;
860                         goto done;
861                 }
862         } else if (vlan_desc && vlan_desc->notempty) {
863                 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
864                         if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
865                                 break;
866                         if (vlan->vlan_id == VLAN_ID_WILDCARD)
867                                 wildcard_vlan = vlan;
868                 }
869                 if (vlan) {
870                         vlan_id = vlan->vlan_id;
871                 } else if (wildcard_vlan) {
872                         vlan = wildcard_vlan;
873                         vlan_id = vlan_desc->untagged;
874                         if (vlan_desc->tagged[0]) {
875                                 /* Tagged VLAN configuration */
876                                 vlan_id = ap_sta_get_free_vlan_id(hapd);
877                         }
878                 } else {
879                         hostapd_logger(hapd, sta->addr,
880                                        HOSTAPD_MODULE_IEEE80211,
881                                        HOSTAPD_LEVEL_DEBUG,
882                                        "missing vlan and wildcard for vlan=%d%s",
883                                        vlan_desc->untagged,
884                                        vlan_desc->tagged[0] ? "+" : "");
885                         vlan_id = 0;
886                         ret = -1;
887                         goto done;
888                 }
889         }
890
891         if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
892                 vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
893                 if (vlan == NULL) {
894                         hostapd_logger(hapd, sta->addr,
895                                        HOSTAPD_MODULE_IEEE80211,
896                                        HOSTAPD_LEVEL_DEBUG,
897                                        "could not add dynamic VLAN interface for vlan=%d%s",
898                                        vlan_desc->untagged,
899                                        vlan_desc->tagged[0] ? "+" : "");
900                         vlan_id = 0;
901                         ret = -1;
902                         goto done;
903                 }
904
905                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
906                                HOSTAPD_LEVEL_DEBUG,
907                                "added new dynamic VLAN interface '%s'",
908                                vlan->ifname);
909         } else if (vlan && vlan->dynamic_vlan > 0) {
910                 vlan->dynamic_vlan++;
911                 hostapd_logger(hapd, sta->addr,
912                                HOSTAPD_MODULE_IEEE80211,
913                                HOSTAPD_LEVEL_DEBUG,
914                                "updated existing dynamic VLAN interface '%s'",
915                                vlan->ifname);
916         }
917 done:
918         old_vlan_id = sta->vlan_id;
919         sta->vlan_id = vlan_id;
920         sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
921
922         if (vlan_id != old_vlan_id && old_vlan_id)
923                 vlan_remove_dynamic(hapd, old_vlan_id);
924
925         return ret;
926 }
927
928
929 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
930 {
931 #ifndef CONFIG_NO_VLAN
932         const char *iface;
933         struct hostapd_vlan *vlan = NULL;
934         int ret;
935         int old_vlanid = sta->vlan_id_bound;
936
937         iface = hapd->conf->iface;
938         if (hapd->conf->ssid.vlan[0])
939                 iface = hapd->conf->ssid.vlan;
940
941         if (sta->vlan_id > 0) {
942                 for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
943                         if (vlan->vlan_id == sta->vlan_id)
944                                 break;
945                 }
946                 if (vlan)
947                         iface = vlan->ifname;
948         }
949
950         /*
951          * Do not increment ref counters if the VLAN ID remains same, but do
952          * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
953          * have been called before.
954          */
955         if (sta->vlan_id == old_vlanid)
956                 goto skip_counting;
957
958         if (sta->vlan_id > 0 && vlan == NULL) {
959                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
960                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
961                                "binding station to (vlan_id=%d)",
962                                sta->vlan_id);
963                 ret = -1;
964                 goto done;
965         } else if (vlan && vlan->dynamic_vlan > 0) {
966                 vlan->dynamic_vlan++;
967                 hostapd_logger(hapd, sta->addr,
968                                HOSTAPD_MODULE_IEEE80211,
969                                HOSTAPD_LEVEL_DEBUG,
970                                "updated existing dynamic VLAN interface '%s'",
971                                iface);
972         }
973
974         /* ref counters have been increased, so mark the station */
975         sta->vlan_id_bound = sta->vlan_id;
976
977 skip_counting:
978         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
979                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
980                        "'%s'", iface);
981
982         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
983                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
984
985         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
986         if (ret < 0) {
987                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
988                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
989                                "entry to vlan_id=%d", sta->vlan_id);
990         }
991
992         /* During 1x reauth, if the vlan id changes, then remove the old id. */
993         if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
994                 vlan_remove_dynamic(hapd, old_vlanid);
995 done:
996
997         return ret;
998 #else /* CONFIG_NO_VLAN */
999         return 0;
1000 #endif /* CONFIG_NO_VLAN */
1001 }
1002
1003
1004 #ifdef CONFIG_IEEE80211W
1005
1006 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1007 {
1008         u32 tu;
1009         struct os_reltime now, passed;
1010         os_get_reltime(&now);
1011         os_reltime_sub(&now, &sta->sa_query_start, &passed);
1012         tu = (passed.sec * 1000000 + passed.usec) / 1024;
1013         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1014                 hostapd_logger(hapd, sta->addr,
1015                                HOSTAPD_MODULE_IEEE80211,
1016                                HOSTAPD_LEVEL_DEBUG,
1017                                "association SA Query timed out");
1018                 sta->sa_query_timed_out = 1;
1019                 os_free(sta->sa_query_trans_id);
1020                 sta->sa_query_trans_id = NULL;
1021                 sta->sa_query_count = 0;
1022                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1023                 return 1;
1024         }
1025
1026         return 0;
1027 }
1028
1029
1030 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1031 {
1032         struct hostapd_data *hapd = eloop_ctx;
1033         struct sta_info *sta = timeout_ctx;
1034         unsigned int timeout, sec, usec;
1035         u8 *trans_id, *nbuf;
1036
1037         wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
1038                    " (count=%d)",
1039                    hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
1040
1041         if (sta->sa_query_count > 0 &&
1042             ap_check_sa_query_timeout(hapd, sta))
1043                 return;
1044
1045         nbuf = os_realloc_array(sta->sa_query_trans_id,
1046                                 sta->sa_query_count + 1,
1047                                 WLAN_SA_QUERY_TR_ID_LEN);
1048         if (nbuf == NULL)
1049                 return;
1050         if (sta->sa_query_count == 0) {
1051                 /* Starting a new SA Query procedure */
1052                 os_get_reltime(&sta->sa_query_start);
1053         }
1054         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1055         sta->sa_query_trans_id = nbuf;
1056         sta->sa_query_count++;
1057
1058         if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1059                 /*
1060                  * We don't really care which ID is used here, so simply
1061                  * hardcode this if the mostly theoretical os_get_random()
1062                  * failure happens.
1063                  */
1064                 trans_id[0] = 0x12;
1065                 trans_id[1] = 0x34;
1066         }
1067
1068         timeout = hapd->conf->assoc_sa_query_retry_timeout;
1069         sec = ((timeout / 1000) * 1024) / 1000;
1070         usec = (timeout % 1000) * 1024;
1071         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1072
1073         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1074                        HOSTAPD_LEVEL_DEBUG,
1075                        "association SA Query attempt %d", sta->sa_query_count);
1076
1077         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
1078 }
1079
1080
1081 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1082 {
1083         ap_sa_query_timer(hapd, sta);
1084 }
1085
1086
1087 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1088 {
1089         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1090         os_free(sta->sa_query_trans_id);
1091         sta->sa_query_trans_id = NULL;
1092         sta->sa_query_count = 0;
1093 }
1094
1095 #endif /* CONFIG_IEEE80211W */
1096
1097
1098 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1099                            int authorized)
1100 {
1101         const u8 *dev_addr = NULL;
1102         char buf[100];
1103 #ifdef CONFIG_P2P
1104         u8 addr[ETH_ALEN];
1105         u8 ip_addr_buf[4];
1106 #endif /* CONFIG_P2P */
1107
1108         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1109                 return;
1110
1111         if (authorized)
1112                 sta->flags |= WLAN_STA_AUTHORIZED;
1113         else
1114                 sta->flags &= ~WLAN_STA_AUTHORIZED;
1115
1116 #ifdef CONFIG_P2P
1117         if (hapd->p2p_group == NULL) {
1118                 if (sta->p2p_ie != NULL &&
1119                     p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1120                         dev_addr = addr;
1121         } else
1122                 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1123
1124         if (dev_addr)
1125                 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1126                             MAC2STR(sta->addr), MAC2STR(dev_addr));
1127         else
1128 #endif /* CONFIG_P2P */
1129                 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1130
1131         if (hapd->sta_authorized_cb)
1132                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1133                                         sta->addr, authorized, dev_addr);
1134
1135         if (authorized) {
1136                 char ip_addr[100];
1137                 ip_addr[0] = '\0';
1138 #ifdef CONFIG_P2P
1139                 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1140                         os_snprintf(ip_addr, sizeof(ip_addr),
1141                                     " ip_addr=%u.%u.%u.%u",
1142                                     ip_addr_buf[0], ip_addr_buf[1],
1143                                     ip_addr_buf[2], ip_addr_buf[3]);
1144                 }
1145 #endif /* CONFIG_P2P */
1146
1147                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1148                         buf, ip_addr);
1149
1150                 if (hapd->msg_ctx_parent &&
1151                     hapd->msg_ctx_parent != hapd->msg_ctx)
1152                         wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1153                                           AP_STA_CONNECTED "%s%s",
1154                                           buf, ip_addr);
1155         } else {
1156                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1157
1158                 if (hapd->msg_ctx_parent &&
1159                     hapd->msg_ctx_parent != hapd->msg_ctx)
1160                         wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1161                                           AP_STA_DISCONNECTED "%s", buf);
1162         }
1163
1164 #ifdef CONFIG_FST
1165         if (hapd->iface->fst) {
1166                 if (authorized)
1167                         fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1168                 else
1169                         fst_notify_peer_disconnected(hapd->iface->fst,
1170                                                      sta->addr);
1171         }
1172 #endif /* CONFIG_FST */
1173 }
1174
1175
1176 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1177                        const u8 *addr, u16 reason)
1178 {
1179         if (sta)
1180                 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1181                            hapd->conf->iface, __func__, MAC2STR(sta->addr),
1182                            reason);
1183         else if (addr)
1184                 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1185                            hapd->conf->iface, __func__, MAC2STR(addr),
1186                            reason);
1187
1188         if (sta == NULL && addr)
1189                 sta = ap_get_sta(hapd, addr);
1190
1191         if (addr)
1192                 hostapd_drv_sta_deauth(hapd, addr, reason);
1193
1194         if (sta == NULL)
1195                 return;
1196         ap_sta_set_authorized(hapd, sta, 0);
1197         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1198         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1199         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1200         wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
1201                    "for " MACSTR " (%d seconds - "
1202                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1203                    hapd->conf->iface, __func__, MAC2STR(sta->addr),
1204                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
1205         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1206         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1207                                ap_handle_timer, hapd, sta);
1208         sta->timeout_next = STA_REMOVE;
1209
1210         sta->deauth_reason = reason;
1211         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1212         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1213         eloop_register_timeout(hapd->iface->drv_flags &
1214                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1215                                ap_sta_deauth_cb_timeout, hapd, sta);
1216 }
1217
1218
1219 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1220 {
1221         if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1222                 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1223                 return;
1224         }
1225         sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1226         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1227         ap_sta_deauth_cb_timeout(hapd, sta);
1228 }
1229
1230
1231 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1232 {
1233         if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1234                 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1235                 return;
1236         }
1237         sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1238         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1239         ap_sta_disassoc_cb_timeout(hapd, sta);
1240 }
1241
1242
1243 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1244                                       struct sta_info *sta)
1245 {
1246         if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1247                 wpa_printf(MSG_DEBUG,
1248                            "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1249                            MACSTR,
1250                            hapd->conf->iface, MAC2STR(sta->addr));
1251         if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1252                 wpa_printf(MSG_DEBUG,
1253                            "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1254                            MACSTR,
1255                            hapd->conf->iface, MAC2STR(sta->addr));
1256 }
1257
1258
1259 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1260 {
1261         int res;
1262
1263         buf[0] = '\0';
1264         res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1265                           (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1266                           (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1267                           (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1268                           (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1269                            ""),
1270                           (flags & WLAN_STA_SHORT_PREAMBLE ?
1271                            "[SHORT_PREAMBLE]" : ""),
1272                           (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1273                           (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1274                           (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1275                           (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1276                           (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1277                           (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1278                           (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1279                           (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1280                           (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1281                           (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1282                           (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1283                           (flags & WLAN_STA_WNM_SLEEP_MODE ?
1284                            "[WNM_SLEEP_MODE]" : ""));
1285         if (os_snprintf_error(buflen, res))
1286                 res = -1;
1287
1288         return res;
1289 }