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