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