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