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