Keep and use list of PSKs per station for RADIUS-based PSK
[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         while (sta->psk) {
239                 struct hostapd_sta_wpa_psk_short *prev = sta->psk;
240                 sta->psk = sta->psk->next;
241                 os_free(prev);
242         }
243         os_free(sta->identity);
244         os_free(sta->radius_cui);
245
246         os_free(sta);
247 }
248
249
250 void hostapd_free_stas(struct hostapd_data *hapd)
251 {
252         struct sta_info *sta, *prev;
253
254         sta = hapd->sta_list;
255
256         while (sta) {
257                 prev = sta;
258                 if (sta->flags & WLAN_STA_AUTH) {
259                         mlme_deauthenticate_indication(
260                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
261                 }
262                 sta = sta->next;
263                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
264                            MAC2STR(prev->addr));
265                 ap_free_sta(hapd, prev);
266         }
267 }
268
269
270 /**
271  * ap_handle_timer - Per STA timer handler
272  * @eloop_ctx: struct hostapd_data *
273  * @timeout_ctx: struct sta_info *
274  *
275  * This function is called to check station activity and to remove inactive
276  * stations.
277  */
278 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
279 {
280         struct hostapd_data *hapd = eloop_ctx;
281         struct sta_info *sta = timeout_ctx;
282         unsigned long next_time = 0;
283
284         wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
285                    __func__, MAC2STR(sta->addr), sta->flags,
286                    sta->timeout_next);
287         if (sta->timeout_next == STA_REMOVE) {
288                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
289                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
290                                "local deauth request");
291                 ap_free_sta(hapd, sta);
292                 return;
293         }
294
295         if ((sta->flags & WLAN_STA_ASSOC) &&
296             (sta->timeout_next == STA_NULLFUNC ||
297              sta->timeout_next == STA_DISASSOC)) {
298                 int inactive_sec;
299                 /*
300                  * Add random value to timeout so that we don't end up bouncing
301                  * all stations at the same time if we have lots of associated
302                  * stations that are idle (but keep re-associating).
303                  */
304                 int fuzz = os_random() % 20;
305                 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
306                 if (inactive_sec == -1) {
307                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
308                                 "Check inactivity: Could not "
309                                 "get station info from kernel driver for "
310                                 MACSTR, MAC2STR(sta->addr));
311                         /*
312                          * The driver may not support this functionality.
313                          * Anyway, try again after the next inactivity timeout,
314                          * but do not disconnect the station now.
315                          */
316                         next_time = hapd->conf->ap_max_inactivity + fuzz;
317                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
318                            sta->flags & WLAN_STA_ASSOC) {
319                         /* station activity detected; reset timeout state */
320                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
321                                 "Station " MACSTR " has been active %is ago",
322                                 MAC2STR(sta->addr), inactive_sec);
323                         sta->timeout_next = STA_NULLFUNC;
324                         next_time = hapd->conf->ap_max_inactivity + fuzz -
325                                 inactive_sec;
326                 } else {
327                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
328                                 "Station " MACSTR " has been "
329                                 "inactive too long: %d sec, max allowed: %d",
330                                 MAC2STR(sta->addr), inactive_sec,
331                                 hapd->conf->ap_max_inactivity);
332
333                         if (hapd->conf->skip_inactivity_poll)
334                                 sta->timeout_next = STA_DISASSOC;
335                 }
336         }
337
338         if ((sta->flags & WLAN_STA_ASSOC) &&
339             sta->timeout_next == STA_DISASSOC &&
340             !(sta->flags & WLAN_STA_PENDING_POLL) &&
341             !hapd->conf->skip_inactivity_poll) {
342                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
343                         " has ACKed data poll", MAC2STR(sta->addr));
344                 /* data nullfunc frame poll did not produce TX errors; assume
345                  * station ACKed it */
346                 sta->timeout_next = STA_NULLFUNC;
347                 next_time = hapd->conf->ap_max_inactivity;
348         }
349
350         if (next_time) {
351                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
352                            "for " MACSTR " (%lu seconds)",
353                            __func__, MAC2STR(sta->addr), next_time);
354                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
355                                        sta);
356                 return;
357         }
358
359         if (sta->timeout_next == STA_NULLFUNC &&
360             (sta->flags & WLAN_STA_ASSOC)) {
361                 wpa_printf(MSG_DEBUG, "  Polling STA");
362                 sta->flags |= WLAN_STA_PENDING_POLL;
363                 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
364                                         sta->flags & WLAN_STA_WMM);
365         } else if (sta->timeout_next != STA_REMOVE) {
366                 int deauth = sta->timeout_next == STA_DEAUTH;
367
368                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
369                         "Timeout, sending %s info to STA " MACSTR,
370                         deauth ? "deauthentication" : "disassociation",
371                         MAC2STR(sta->addr));
372
373                 if (deauth) {
374                         hostapd_drv_sta_deauth(
375                                 hapd, sta->addr,
376                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
377                 } else {
378                         hostapd_drv_sta_disassoc(
379                                 hapd, sta->addr,
380                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
381                 }
382         }
383
384         switch (sta->timeout_next) {
385         case STA_NULLFUNC:
386                 sta->timeout_next = STA_DISASSOC;
387                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
388                            "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
389                            __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
390                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
391                                        hapd, sta);
392                 break;
393         case STA_DISASSOC:
394                 ap_sta_set_authorized(hapd, sta, 0);
395                 sta->flags &= ~WLAN_STA_ASSOC;
396                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
397                 if (!sta->acct_terminate_cause)
398                         sta->acct_terminate_cause =
399                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
400                 accounting_sta_stop(hapd, sta);
401                 ieee802_1x_free_station(sta);
402                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
403                                HOSTAPD_LEVEL_INFO, "disassociated due to "
404                                "inactivity");
405                 sta->timeout_next = STA_DEAUTH;
406                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
407                            "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
408                            __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
409                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
410                                        hapd, sta);
411                 mlme_disassociate_indication(
412                         hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
413                 break;
414         case STA_DEAUTH:
415         case STA_REMOVE:
416                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
417                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
418                                "inactivity (timer DEAUTH/REMOVE)");
419                 if (!sta->acct_terminate_cause)
420                         sta->acct_terminate_cause =
421                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
422                 mlme_deauthenticate_indication(
423                         hapd, sta,
424                         WLAN_REASON_PREV_AUTH_NOT_VALID);
425                 ap_free_sta(hapd, sta);
426                 break;
427         }
428 }
429
430
431 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
432 {
433         struct hostapd_data *hapd = eloop_ctx;
434         struct sta_info *sta = timeout_ctx;
435         u8 addr[ETH_ALEN];
436
437         if (!(sta->flags & WLAN_STA_AUTH)) {
438                 if (sta->flags & WLAN_STA_GAS) {
439                         wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
440                                    "entry " MACSTR, MAC2STR(sta->addr));
441                         ap_free_sta(hapd, sta);
442                 }
443                 return;
444         }
445
446         mlme_deauthenticate_indication(hapd, sta,
447                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
448         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
449                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
450                        "session timeout");
451         sta->acct_terminate_cause =
452                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
453         os_memcpy(addr, sta->addr, ETH_ALEN);
454         ap_free_sta(hapd, sta);
455         hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
456 }
457
458
459 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
460                             u32 session_timeout)
461 {
462         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
463                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
464                        "seconds", session_timeout);
465         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
466         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
467                                hapd, sta);
468 }
469
470
471 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
472 {
473         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
474 }
475
476
477 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
478 {
479         struct sta_info *sta;
480
481         sta = ap_get_sta(hapd, addr);
482         if (sta)
483                 return sta;
484
485         wpa_printf(MSG_DEBUG, "  New STA");
486         if (hapd->num_sta >= hapd->conf->max_num_sta) {
487                 /* FIX: might try to remove some old STAs first? */
488                 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
489                            hapd->num_sta, hapd->conf->max_num_sta);
490                 return NULL;
491         }
492
493         sta = os_zalloc(sizeof(struct sta_info));
494         if (sta == NULL) {
495                 wpa_printf(MSG_ERROR, "malloc failed");
496                 return NULL;
497         }
498         sta->acct_interim_interval = hapd->conf->acct_interim_interval;
499
500         /* initialize STA info data */
501         wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
502                    "for " MACSTR " (%d seconds - ap_max_inactivity)",
503                    __func__, MAC2STR(addr),
504                    hapd->conf->ap_max_inactivity);
505         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
506                                ap_handle_timer, hapd, sta);
507         os_memcpy(sta->addr, addr, ETH_ALEN);
508         sta->next = hapd->sta_list;
509         hapd->sta_list = sta;
510         hapd->num_sta++;
511         ap_sta_hash_add(hapd, sta);
512         sta->ssid = &hapd->conf->ssid;
513         ap_sta_remove_in_other_bss(hapd, sta);
514
515         return sta;
516 }
517
518
519 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
520 {
521         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
522
523         wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
524                    MAC2STR(sta->addr));
525         if (hostapd_drv_sta_remove(hapd, sta->addr) &&
526             sta->flags & WLAN_STA_ASSOC) {
527                 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
528                            " from kernel driver.", MAC2STR(sta->addr));
529                 return -1;
530         }
531         return 0;
532 }
533
534
535 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
536                                        struct sta_info *sta)
537 {
538         struct hostapd_iface *iface = hapd->iface;
539         size_t i;
540
541         for (i = 0; i < iface->num_bss; i++) {
542                 struct hostapd_data *bss = iface->bss[i];
543                 struct sta_info *sta2;
544                 /* bss should always be set during operation, but it may be
545                  * NULL during reconfiguration. Assume the STA is not
546                  * associated to another BSS in that case to avoid NULL pointer
547                  * dereferences. */
548                 if (bss == hapd || bss == NULL)
549                         continue;
550                 sta2 = ap_get_sta(bss, sta->addr);
551                 if (!sta2)
552                         continue;
553
554                 ap_sta_disconnect(bss, sta2, sta2->addr,
555                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
556         }
557 }
558
559
560 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
561 {
562         struct hostapd_data *hapd = eloop_ctx;
563         struct sta_info *sta = timeout_ctx;
564
565         ap_sta_remove(hapd, sta);
566         mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
567 }
568
569
570 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
571                          u16 reason)
572 {
573         wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
574                    hapd->conf->iface, MAC2STR(sta->addr));
575         sta->flags &= ~WLAN_STA_ASSOC;
576         ap_sta_set_authorized(hapd, sta, 0);
577         sta->timeout_next = STA_DEAUTH;
578         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
579                    "for " MACSTR " (%d seconds - "
580                    "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
581                    __func__, MAC2STR(sta->addr),
582                    AP_MAX_INACTIVITY_AFTER_DISASSOC);
583         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
584         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
585                                ap_handle_timer, hapd, sta);
586         accounting_sta_stop(hapd, sta);
587         ieee802_1x_free_station(sta);
588
589         sta->disassoc_reason = reason;
590         sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
591         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
592         eloop_register_timeout(hapd->iface->drv_flags &
593                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
594                                ap_sta_disassoc_cb_timeout, hapd, sta);
595 }
596
597
598 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
599 {
600         struct hostapd_data *hapd = eloop_ctx;
601         struct sta_info *sta = timeout_ctx;
602
603         ap_sta_remove(hapd, sta);
604         mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
605 }
606
607
608 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
609                            u16 reason)
610 {
611         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
612                    hapd->conf->iface, MAC2STR(sta->addr));
613         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
614         ap_sta_set_authorized(hapd, sta, 0);
615         sta->timeout_next = STA_REMOVE;
616         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
617                    "for " MACSTR " (%d seconds - "
618                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
619                    __func__, MAC2STR(sta->addr),
620                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
621         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
622         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
623                                ap_handle_timer, hapd, sta);
624         accounting_sta_stop(hapd, sta);
625         ieee802_1x_free_station(sta);
626
627         sta->deauth_reason = reason;
628         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
629         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
630         eloop_register_timeout(hapd->iface->drv_flags &
631                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
632                                ap_sta_deauth_cb_timeout, hapd, sta);
633 }
634
635
636 #ifdef CONFIG_WPS
637 int ap_sta_wps_cancel(struct hostapd_data *hapd,
638                       struct sta_info *sta, void *ctx)
639 {
640         if (sta && (sta->flags & WLAN_STA_WPS)) {
641                 ap_sta_deauthenticate(hapd, sta,
642                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
643                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
644                            __func__, MAC2STR(sta->addr));
645                 return 1;
646         }
647
648         return 0;
649 }
650 #endif /* CONFIG_WPS */
651
652
653 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
654                      int old_vlanid)
655 {
656 #ifndef CONFIG_NO_VLAN
657         const char *iface;
658         struct hostapd_vlan *vlan = NULL;
659         int ret;
660
661         /*
662          * Do not proceed furthur if the vlan id remains same. We do not want
663          * duplicate dynamic vlan entries.
664          */
665         if (sta->vlan_id == old_vlanid)
666                 return 0;
667
668         /*
669          * During 1x reauth, if the vlan id changes, then remove the old id and
670          * proceed furthur to add the new one.
671          */
672         if (old_vlanid > 0)
673                 vlan_remove_dynamic(hapd, old_vlanid);
674
675         iface = hapd->conf->iface;
676         if (sta->ssid->vlan[0])
677                 iface = sta->ssid->vlan;
678
679         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
680                 sta->vlan_id = 0;
681         else if (sta->vlan_id > 0) {
682                 vlan = hapd->conf->vlan;
683                 while (vlan) {
684                         if (vlan->vlan_id == sta->vlan_id ||
685                             vlan->vlan_id == VLAN_ID_WILDCARD) {
686                                 iface = vlan->ifname;
687                                 break;
688                         }
689                         vlan = vlan->next;
690                 }
691         }
692
693         if (sta->vlan_id > 0 && vlan == NULL) {
694                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
695                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
696                                "binding station to (vlan_id=%d)",
697                                sta->vlan_id);
698                 return -1;
699         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
700                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
701                 if (vlan == NULL) {
702                         hostapd_logger(hapd, sta->addr,
703                                        HOSTAPD_MODULE_IEEE80211,
704                                        HOSTAPD_LEVEL_DEBUG, "could not add "
705                                        "dynamic VLAN interface for vlan_id=%d",
706                                        sta->vlan_id);
707                         return -1;
708                 }
709
710                 iface = vlan->ifname;
711                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
712                         hostapd_logger(hapd, sta->addr,
713                                        HOSTAPD_MODULE_IEEE80211,
714                                        HOSTAPD_LEVEL_DEBUG, "could not "
715                                        "configure encryption for dynamic VLAN "
716                                        "interface for vlan_id=%d",
717                                        sta->vlan_id);
718                 }
719
720                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
721                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
722                                "interface '%s'", iface);
723         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
724                 if (sta->vlan_id > 0) {
725                         vlan->dynamic_vlan++;
726                         hostapd_logger(hapd, sta->addr,
727                                        HOSTAPD_MODULE_IEEE80211,
728                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
729                                        "dynamic VLAN interface '%s'", iface);
730                 }
731
732                 /*
733                  * Update encryption configuration for statically generated
734                  * VLAN interface. This is only used for static WEP
735                  * configuration for the case where hostapd did not yet know
736                  * which keys are to be used when the interface was added.
737                  */
738                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
739                         hostapd_logger(hapd, sta->addr,
740                                        HOSTAPD_MODULE_IEEE80211,
741                                        HOSTAPD_LEVEL_DEBUG, "could not "
742                                        "configure encryption for VLAN "
743                                        "interface for vlan_id=%d",
744                                        sta->vlan_id);
745                 }
746         }
747
748         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
749                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
750                        "'%s'", iface);
751
752         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
753                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
754
755         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
756         if (ret < 0) {
757                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
758                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
759                                "entry to vlan_id=%d", sta->vlan_id);
760         }
761         return ret;
762 #else /* CONFIG_NO_VLAN */
763         return 0;
764 #endif /* CONFIG_NO_VLAN */
765 }
766
767
768 #ifdef CONFIG_IEEE80211W
769
770 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
771 {
772         u32 tu;
773         struct os_time now, passed;
774         os_get_time(&now);
775         os_time_sub(&now, &sta->sa_query_start, &passed);
776         tu = (passed.sec * 1000000 + passed.usec) / 1024;
777         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
778                 hostapd_logger(hapd, sta->addr,
779                                HOSTAPD_MODULE_IEEE80211,
780                                HOSTAPD_LEVEL_DEBUG,
781                                "association SA Query timed out");
782                 sta->sa_query_timed_out = 1;
783                 os_free(sta->sa_query_trans_id);
784                 sta->sa_query_trans_id = NULL;
785                 sta->sa_query_count = 0;
786                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
787                 return 1;
788         }
789
790         return 0;
791 }
792
793
794 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
795 {
796         struct hostapd_data *hapd = eloop_ctx;
797         struct sta_info *sta = timeout_ctx;
798         unsigned int timeout, sec, usec;
799         u8 *trans_id, *nbuf;
800
801         if (sta->sa_query_count > 0 &&
802             ap_check_sa_query_timeout(hapd, sta))
803                 return;
804
805         nbuf = os_realloc_array(sta->sa_query_trans_id,
806                                 sta->sa_query_count + 1,
807                                 WLAN_SA_QUERY_TR_ID_LEN);
808         if (nbuf == NULL)
809                 return;
810         if (sta->sa_query_count == 0) {
811                 /* Starting a new SA Query procedure */
812                 os_get_time(&sta->sa_query_start);
813         }
814         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
815         sta->sa_query_trans_id = nbuf;
816         sta->sa_query_count++;
817
818         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
819
820         timeout = hapd->conf->assoc_sa_query_retry_timeout;
821         sec = ((timeout / 1000) * 1024) / 1000;
822         usec = (timeout % 1000) * 1024;
823         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
824
825         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
826                        HOSTAPD_LEVEL_DEBUG,
827                        "association SA Query attempt %d", sta->sa_query_count);
828
829         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
830 }
831
832
833 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
834 {
835         ap_sa_query_timer(hapd, sta);
836 }
837
838
839 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
840 {
841         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
842         os_free(sta->sa_query_trans_id);
843         sta->sa_query_trans_id = NULL;
844         sta->sa_query_count = 0;
845 }
846
847 #endif /* CONFIG_IEEE80211W */
848
849
850 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
851                            int authorized)
852 {
853         const u8 *dev_addr = NULL;
854 #ifdef CONFIG_P2P
855         u8 addr[ETH_ALEN];
856 #endif /* CONFIG_P2P */
857
858         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
859                 return;
860
861 #ifdef CONFIG_P2P
862         if (hapd->p2p_group == NULL) {
863                 if (sta->p2p_ie != NULL &&
864                     p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
865                         dev_addr = addr;
866         } else
867                 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
868 #endif /* CONFIG_P2P */
869
870         if (authorized) {
871                 if (dev_addr)
872                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
873                                 MACSTR " p2p_dev_addr=" MACSTR,
874                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
875                 else
876                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
877                                 MACSTR, MAC2STR(sta->addr));
878                 if (hapd->msg_ctx_parent &&
879                     hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
880                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
881                                 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
882                                 MACSTR,
883                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
884                 else if (hapd->msg_ctx_parent &&
885                          hapd->msg_ctx_parent != hapd->msg_ctx)
886                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
887                                 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
888
889                 sta->flags |= WLAN_STA_AUTHORIZED;
890         } else {
891                 if (dev_addr)
892                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
893                                 MACSTR " p2p_dev_addr=" MACSTR,
894                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
895                 else
896                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
897                                 MACSTR, MAC2STR(sta->addr));
898                 if (hapd->msg_ctx_parent &&
899                     hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
900                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
901                                 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
902                                 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
903                 else if (hapd->msg_ctx_parent &&
904                          hapd->msg_ctx_parent != hapd->msg_ctx)
905                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
906                                 AP_STA_DISCONNECTED MACSTR,
907                                 MAC2STR(sta->addr));
908                 sta->flags &= ~WLAN_STA_AUTHORIZED;
909         }
910
911         if (hapd->sta_authorized_cb)
912                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
913                                         sta->addr, authorized, dev_addr);
914 }
915
916
917 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
918                        const u8 *addr, u16 reason)
919 {
920
921         if (sta == NULL && addr)
922                 sta = ap_get_sta(hapd, addr);
923
924         if (addr)
925                 hostapd_drv_sta_deauth(hapd, addr, reason);
926
927         if (sta == NULL)
928                 return;
929         ap_sta_set_authorized(hapd, sta, 0);
930         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
931         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
932         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
933         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
934                    "for " MACSTR " (%d seconds - "
935                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
936                    __func__, MAC2STR(sta->addr),
937                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
938         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
939         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
940                                ap_handle_timer, hapd, sta);
941         sta->timeout_next = STA_REMOVE;
942
943         sta->deauth_reason = reason;
944         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
945         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
946         eloop_register_timeout(hapd->iface->drv_flags &
947                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
948                                ap_sta_deauth_cb_timeout, hapd, sta);
949 }
950
951
952 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
953 {
954         if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
955                 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
956                 return;
957         }
958         sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
959         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
960         ap_sta_deauth_cb_timeout(hapd, sta);
961 }
962
963
964 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
965 {
966         if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
967                 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
968                 return;
969         }
970         sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
971         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
972         ap_sta_disassoc_cb_timeout(hapd, sta);
973 }