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