hostapd_driver_ops reduction
[mech_eap.git] / src / ap / sta_info.c
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2009, 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 "radius/radius.h"
21 #include "radius/radius_client.h"
22 #include "drivers/driver.h"
23 #include "p2p/p2p.h"
24 #include "hostapd.h"
25 #include "accounting.h"
26 #include "ieee802_1x.h"
27 #include "ieee802_11.h"
28 #include "wpa_auth.h"
29 #include "preauth_auth.h"
30 #include "ap_config.h"
31 #include "beacon.h"
32 #include "ap_mlme.h"
33 #include "vlan_init.h"
34 #include "p2p_hostapd.h"
35 #include "ap_drv_ops.h"
36 #include "sta_info.h"
37
38 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
39                                        struct sta_info *sta);
40 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
41 #ifdef CONFIG_IEEE80211W
42 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
43 #endif /* CONFIG_IEEE80211W */
44
45 int ap_for_each_sta(struct hostapd_data *hapd,
46                     int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
47                               void *ctx),
48                     void *ctx)
49 {
50         struct sta_info *sta;
51
52         for (sta = hapd->sta_list; sta; sta = sta->next) {
53                 if (cb(hapd, sta, ctx))
54                         return 1;
55         }
56
57         return 0;
58 }
59
60
61 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
62 {
63         struct sta_info *s;
64
65         s = hapd->sta_hash[STA_HASH(sta)];
66         while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
67                 s = s->hnext;
68         return s;
69 }
70
71
72 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
73 {
74         struct sta_info *tmp;
75
76         if (hapd->sta_list == sta) {
77                 hapd->sta_list = sta->next;
78                 return;
79         }
80
81         tmp = hapd->sta_list;
82         while (tmp != NULL && tmp->next != sta)
83                 tmp = tmp->next;
84         if (tmp == NULL) {
85                 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
86                            "list.", MAC2STR(sta->addr));
87         } else
88                 tmp->next = sta->next;
89 }
90
91
92 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
93 {
94         sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
95         hapd->sta_hash[STA_HASH(sta->addr)] = sta;
96 }
97
98
99 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
100 {
101         struct sta_info *s;
102
103         s = hapd->sta_hash[STA_HASH(sta->addr)];
104         if (s == NULL) return;
105         if (os_memcmp(s->addr, sta->addr, 6) == 0) {
106                 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
107                 return;
108         }
109
110         while (s->hnext != NULL &&
111                os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
112                 s = s->hnext;
113         if (s->hnext != NULL)
114                 s->hnext = s->hnext->hnext;
115         else
116                 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
117                            " from hash table", MAC2STR(sta->addr));
118 }
119
120
121 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
122 {
123         int set_beacon = 0;
124
125         accounting_sta_stop(hapd, sta);
126
127         if (sta->flags & WLAN_STA_WDS)
128                 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 0);
129
130         if (!(sta->flags & WLAN_STA_PREAUTH))
131                 hostapd_drv_sta_remove(hapd, sta->addr);
132
133         ap_sta_hash_del(hapd, sta);
134         ap_sta_list_del(hapd, sta);
135
136         if (sta->aid > 0)
137                 hapd->sta_aid[(sta->aid - 1) / 32] &=
138                         ~BIT((sta->aid - 1) % 32);
139
140         hapd->num_sta--;
141         if (sta->nonerp_set) {
142                 sta->nonerp_set = 0;
143                 hapd->iface->num_sta_non_erp--;
144                 if (hapd->iface->num_sta_non_erp == 0)
145                         set_beacon++;
146         }
147
148         if (sta->no_short_slot_time_set) {
149                 sta->no_short_slot_time_set = 0;
150                 hapd->iface->num_sta_no_short_slot_time--;
151                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
152                     && hapd->iface->num_sta_no_short_slot_time == 0)
153                         set_beacon++;
154         }
155
156         if (sta->no_short_preamble_set) {
157                 sta->no_short_preamble_set = 0;
158                 hapd->iface->num_sta_no_short_preamble--;
159                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
160                     && hapd->iface->num_sta_no_short_preamble == 0)
161                         set_beacon++;
162         }
163
164         if (sta->no_ht_gf_set) {
165                 sta->no_ht_gf_set = 0;
166                 hapd->iface->num_sta_ht_no_gf--;
167         }
168
169         if (sta->no_ht_set) {
170                 sta->no_ht_set = 0;
171                 hapd->iface->num_sta_no_ht--;
172         }
173
174         if (sta->ht_20mhz_set) {
175                 sta->ht_20mhz_set = 0;
176                 hapd->iface->num_sta_ht_20mhz--;
177         }
178
179 #ifdef CONFIG_P2P
180         if (sta->no_p2p_set) {
181                 sta->no_p2p_set = 0;
182                 hapd->num_sta_no_p2p--;
183                 if (hapd->num_sta_no_p2p == 0)
184                         hostapd_p2p_non_p2p_sta_disconnected(hapd);
185         }
186 #endif /* CONFIG_P2P */
187
188 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
189         if (hostapd_ht_operation_update(hapd->iface) > 0)
190                 set_beacon++;
191 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
192
193         if (set_beacon)
194                 ieee802_11_set_beacons(hapd->iface);
195
196         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
197         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
198
199         ieee802_1x_free_station(sta);
200         wpa_auth_sta_deinit(sta->wpa_sm);
201         rsn_preauth_free_station(hapd, sta);
202 #ifndef CONFIG_NO_RADIUS
203         radius_client_flush_auth(hapd->radius, sta->addr);
204 #endif /* CONFIG_NO_RADIUS */
205
206         os_free(sta->last_assoc_req);
207         os_free(sta->challenge);
208
209 #ifdef CONFIG_IEEE80211W
210         os_free(sta->sa_query_trans_id);
211         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
212 #endif /* CONFIG_IEEE80211W */
213
214 #ifdef CONFIG_P2P
215         p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
216 #endif /* CONFIG_P2P */
217
218         wpabuf_free(sta->wps_ie);
219         wpabuf_free(sta->p2p_ie);
220
221         os_free(sta->ht_capabilities);
222
223         os_free(sta);
224 }
225
226
227 void hostapd_free_stas(struct hostapd_data *hapd)
228 {
229         struct sta_info *sta, *prev;
230
231         sta = hapd->sta_list;
232
233         while (sta) {
234                 prev = sta;
235                 if (sta->flags & WLAN_STA_AUTH) {
236                         mlme_deauthenticate_indication(
237                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
238                 }
239                 sta = sta->next;
240                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
241                            MAC2STR(prev->addr));
242                 ap_free_sta(hapd, prev);
243         }
244 }
245
246
247 /**
248  * ap_handle_timer - Per STA timer handler
249  * @eloop_ctx: struct hostapd_data *
250  * @timeout_ctx: struct sta_info *
251  *
252  * This function is called to check station activity and to remove inactive
253  * stations.
254  */
255 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
256 {
257         struct hostapd_data *hapd = eloop_ctx;
258         struct sta_info *sta = timeout_ctx;
259         unsigned long next_time = 0;
260
261         if (sta->timeout_next == STA_REMOVE) {
262                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
263                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
264                                "local deauth request");
265                 ap_free_sta(hapd, sta);
266                 return;
267         }
268
269         if ((sta->flags & WLAN_STA_ASSOC) &&
270             (sta->timeout_next == STA_NULLFUNC ||
271              sta->timeout_next == STA_DISASSOC)) {
272                 int inactive_sec;
273                 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
274                            MAC2STR(sta->addr));
275                 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
276                 if (inactive_sec == -1) {
277                         wpa_printf(MSG_DEBUG, "Could not get station info "
278                                    "from kernel driver for " MACSTR ".",
279                                    MAC2STR(sta->addr));
280                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
281                            sta->flags & WLAN_STA_ASSOC) {
282                         /* station activity detected; reset timeout state */
283                         wpa_printf(MSG_DEBUG, "  Station has been active");
284                         sta->timeout_next = STA_NULLFUNC;
285                         next_time = hapd->conf->ap_max_inactivity -
286                                 inactive_sec;
287                 }
288         }
289
290         if ((sta->flags & WLAN_STA_ASSOC) &&
291             sta->timeout_next == STA_DISASSOC &&
292             !(sta->flags & WLAN_STA_PENDING_POLL)) {
293                 wpa_printf(MSG_DEBUG, "  Station has ACKed data poll");
294                 /* data nullfunc frame poll did not produce TX errors; assume
295                  * station ACKed it */
296                 sta->timeout_next = STA_NULLFUNC;
297                 next_time = hapd->conf->ap_max_inactivity;
298         }
299
300         if (next_time) {
301                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
302                                        sta);
303                 return;
304         }
305
306         if (sta->timeout_next == STA_NULLFUNC &&
307             (sta->flags & WLAN_STA_ASSOC)) {
308 #ifndef CONFIG_NATIVE_WINDOWS
309                 /* send data frame to poll STA and check whether this frame
310                  * is ACKed */
311                 struct ieee80211_hdr hdr;
312
313                 wpa_printf(MSG_DEBUG, "  Polling STA with data frame");
314                 sta->flags |= WLAN_STA_PENDING_POLL;
315
316                 os_memset(&hdr, 0, sizeof(hdr));
317                 if (hapd->driver &&
318                     os_strcmp(hapd->driver->name, "hostap") == 0) {
319                         /*
320                          * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
321                          * but it is apparently not retried so TX Exc events
322                          * are not received for it.
323                          */
324                         hdr.frame_control =
325                                 IEEE80211_FC(WLAN_FC_TYPE_DATA,
326                                              WLAN_FC_STYPE_DATA);
327                 } else {
328                         hdr.frame_control =
329                                 IEEE80211_FC(WLAN_FC_TYPE_DATA,
330                                              WLAN_FC_STYPE_NULLFUNC);
331                 }
332
333                 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
334                 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
335                 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
336                           ETH_ALEN);
337                 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
338
339                 if (hostapd_drv_send_mlme(hapd, &hdr, sizeof(hdr)) < 0)
340                         perror("ap_handle_timer: send");
341 #endif /* CONFIG_NATIVE_WINDOWS */
342         } else if (sta->timeout_next != STA_REMOVE) {
343                 int deauth = sta->timeout_next == STA_DEAUTH;
344
345                 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
346                            deauth ? "deauthentication" : "disassociation",
347                            MAC2STR(sta->addr));
348
349                 if (deauth) {
350                         hostapd_drv_sta_deauth(
351                                 hapd, sta->addr,
352                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
353                 } else {
354                         hostapd_drv_sta_disassoc(
355                                 hapd, sta->addr,
356                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
357                 }
358         }
359
360         switch (sta->timeout_next) {
361         case STA_NULLFUNC:
362                 sta->timeout_next = STA_DISASSOC;
363                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
364                                        hapd, sta);
365                 break;
366         case STA_DISASSOC:
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");
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 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_remove(hapd, sta);
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         mlme_disassociate_indication(hapd, sta, reason);
534 }
535
536
537 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
538                            u16 reason)
539 {
540         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
541                    hapd->conf->iface, MAC2STR(sta->addr));
542         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
543         ap_sta_remove(hapd, sta);
544         sta->timeout_next = STA_REMOVE;
545         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
546         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
547                                ap_handle_timer, hapd, sta);
548         accounting_sta_stop(hapd, sta);
549         ieee802_1x_free_station(sta);
550
551         mlme_deauthenticate_indication(hapd, sta, reason);
552 }
553
554
555 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
556                      int old_vlanid)
557 {
558 #ifndef CONFIG_NO_VLAN
559         const char *iface;
560         struct hostapd_vlan *vlan = NULL;
561         int ret;
562
563         /*
564          * Do not proceed furthur if the vlan id remains same. We do not want
565          * duplicate dynamic vlan entries.
566          */
567         if (sta->vlan_id == old_vlanid)
568                 return 0;
569
570         /*
571          * During 1x reauth, if the vlan id changes, then remove the old id and
572          * proceed furthur to add the new one.
573          */
574         if (old_vlanid > 0)
575                 vlan_remove_dynamic(hapd, old_vlanid);
576
577         iface = hapd->conf->iface;
578         if (sta->ssid->vlan[0])
579                 iface = sta->ssid->vlan;
580
581         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
582                 sta->vlan_id = 0;
583         else if (sta->vlan_id > 0) {
584                 vlan = hapd->conf->vlan;
585                 while (vlan) {
586                         if (vlan->vlan_id == sta->vlan_id ||
587                             vlan->vlan_id == VLAN_ID_WILDCARD) {
588                                 iface = vlan->ifname;
589                                 break;
590                         }
591                         vlan = vlan->next;
592                 }
593         }
594
595         if (sta->vlan_id > 0 && vlan == NULL) {
596                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
597                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
598                                "binding station to (vlan_id=%d)",
599                                sta->vlan_id);
600                 return -1;
601         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
602                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
603                 if (vlan == NULL) {
604                         hostapd_logger(hapd, sta->addr,
605                                        HOSTAPD_MODULE_IEEE80211,
606                                        HOSTAPD_LEVEL_DEBUG, "could not add "
607                                        "dynamic VLAN interface for vlan_id=%d",
608                                        sta->vlan_id);
609                         return -1;
610                 }
611
612                 iface = vlan->ifname;
613                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
614                         hostapd_logger(hapd, sta->addr,
615                                        HOSTAPD_MODULE_IEEE80211,
616                                        HOSTAPD_LEVEL_DEBUG, "could not "
617                                        "configure encryption for dynamic VLAN "
618                                        "interface for vlan_id=%d",
619                                        sta->vlan_id);
620                 }
621
622                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
623                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
624                                "interface '%s'", iface);
625         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
626                 if (sta->vlan_id > 0) {
627                         vlan->dynamic_vlan++;
628                         hostapd_logger(hapd, sta->addr,
629                                        HOSTAPD_MODULE_IEEE80211,
630                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
631                                        "dynamic VLAN interface '%s'", iface);
632                 }
633
634                 /*
635                  * Update encryption configuration for statically generated
636                  * VLAN interface. This is only used for static WEP
637                  * configuration for the case where hostapd did not yet know
638                  * which keys are to be used when the interface was added.
639                  */
640                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
641                         hostapd_logger(hapd, sta->addr,
642                                        HOSTAPD_MODULE_IEEE80211,
643                                        HOSTAPD_LEVEL_DEBUG, "could not "
644                                        "configure encryption for VLAN "
645                                        "interface for vlan_id=%d",
646                                        sta->vlan_id);
647                 }
648         }
649
650         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
651                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
652                        "'%s'", iface);
653
654         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
655                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
656
657         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
658         if (ret < 0) {
659                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
660                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
661                                "entry to vlan_id=%d", sta->vlan_id);
662         }
663         return ret;
664 #else /* CONFIG_NO_VLAN */
665         return 0;
666 #endif /* CONFIG_NO_VLAN */
667 }
668
669
670 #ifdef CONFIG_IEEE80211W
671
672 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
673 {
674         u32 tu;
675         struct os_time now, passed;
676         os_get_time(&now);
677         os_time_sub(&now, &sta->sa_query_start, &passed);
678         tu = (passed.sec * 1000000 + passed.usec) / 1024;
679         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
680                 hostapd_logger(hapd, sta->addr,
681                                HOSTAPD_MODULE_IEEE80211,
682                                HOSTAPD_LEVEL_DEBUG,
683                                "association SA Query timed out");
684                 sta->sa_query_timed_out = 1;
685                 os_free(sta->sa_query_trans_id);
686                 sta->sa_query_trans_id = NULL;
687                 sta->sa_query_count = 0;
688                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
689                 return 1;
690         }
691
692         return 0;
693 }
694
695
696 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
697 {
698         struct hostapd_data *hapd = eloop_ctx;
699         struct sta_info *sta = timeout_ctx;
700         unsigned int timeout, sec, usec;
701         u8 *trans_id, *nbuf;
702
703         if (sta->sa_query_count > 0 &&
704             ap_check_sa_query_timeout(hapd, sta))
705                 return;
706
707         nbuf = os_realloc(sta->sa_query_trans_id,
708                           (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
709         if (nbuf == NULL)
710                 return;
711         if (sta->sa_query_count == 0) {
712                 /* Starting a new SA Query procedure */
713                 os_get_time(&sta->sa_query_start);
714         }
715         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
716         sta->sa_query_trans_id = nbuf;
717         sta->sa_query_count++;
718
719         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
720
721         timeout = hapd->conf->assoc_sa_query_retry_timeout;
722         sec = ((timeout / 1000) * 1024) / 1000;
723         usec = (timeout % 1000) * 1024;
724         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
725
726         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
727                        HOSTAPD_LEVEL_DEBUG,
728                        "association SA Query attempt %d", sta->sa_query_count);
729
730 #ifdef NEED_AP_MLME
731         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
732 #endif /* NEED_AP_MLME */
733 }
734
735
736 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
737 {
738         ap_sa_query_timer(hapd, sta);
739 }
740
741
742 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
743 {
744         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
745         os_free(sta->sa_query_trans_id);
746         sta->sa_query_trans_id = NULL;
747         sta->sa_query_count = 0;
748 }
749
750 #endif /* CONFIG_IEEE80211W */
751
752
753 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
754                        const u8 *addr, u16 reason)
755 {
756
757         if (sta == NULL && addr)
758                 sta = ap_get_sta(hapd, addr);
759
760         if (addr)
761                 hostapd_drv_sta_deauth(hapd, addr, reason);
762
763         if (sta == NULL)
764                 return;
765         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
766         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
767         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
768         sta->timeout_next = STA_REMOVE;
769 }