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