P2P: Save a copy of P2P IE(s) data from (Re)Association Request
[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 "hostapd.h"
24 #include "accounting.h"
25 #include "ieee802_1x.h"
26 #include "ieee802_11.h"
27 #include "wpa_auth.h"
28 #include "preauth_auth.h"
29 #include "ap_config.h"
30 #include "beacon.h"
31 #include "ap_mlme.h"
32 #include "vlan_init.h"
33 #include "sta_info.h"
34
35 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
36                                        struct sta_info *sta);
37 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
38 #ifdef CONFIG_IEEE80211W
39 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
40 #endif /* CONFIG_IEEE80211W */
41
42 int ap_for_each_sta(struct hostapd_data *hapd,
43                     int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
44                               void *ctx),
45                     void *ctx)
46 {
47         struct sta_info *sta;
48
49         for (sta = hapd->sta_list; sta; sta = sta->next) {
50                 if (cb(hapd, sta, ctx))
51                         return 1;
52         }
53
54         return 0;
55 }
56
57
58 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
59 {
60         struct sta_info *s;
61
62         s = hapd->sta_hash[STA_HASH(sta)];
63         while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
64                 s = s->hnext;
65         return s;
66 }
67
68
69 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
70 {
71         struct sta_info *tmp;
72
73         if (hapd->sta_list == sta) {
74                 hapd->sta_list = sta->next;
75                 return;
76         }
77
78         tmp = hapd->sta_list;
79         while (tmp != NULL && tmp->next != sta)
80                 tmp = tmp->next;
81         if (tmp == NULL) {
82                 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
83                            "list.", MAC2STR(sta->addr));
84         } else
85                 tmp->next = sta->next;
86 }
87
88
89 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
90 {
91         sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
92         hapd->sta_hash[STA_HASH(sta->addr)] = sta;
93 }
94
95
96 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
97 {
98         struct sta_info *s;
99
100         s = hapd->sta_hash[STA_HASH(sta->addr)];
101         if (s == NULL) return;
102         if (os_memcmp(s->addr, sta->addr, 6) == 0) {
103                 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
104                 return;
105         }
106
107         while (s->hnext != NULL &&
108                os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
109                 s = s->hnext;
110         if (s->hnext != NULL)
111                 s->hnext = s->hnext->hnext;
112         else
113                 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
114                            " from hash table", MAC2STR(sta->addr));
115 }
116
117
118 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
119 {
120         int set_beacon = 0;
121
122         accounting_sta_stop(hapd, sta);
123
124         if (sta->flags & WLAN_STA_WDS)
125                 hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 0);
126
127         if (!(sta->flags & WLAN_STA_PREAUTH))
128                 hapd->drv.sta_remove(hapd, sta->addr);
129
130         ap_sta_hash_del(hapd, sta);
131         ap_sta_list_del(hapd, sta);
132
133         if (sta->aid > 0)
134                 hapd->sta_aid[(sta->aid - 1) / 32] &=
135                         ~BIT((sta->aid - 1) % 32);
136
137         hapd->num_sta--;
138         if (sta->nonerp_set) {
139                 sta->nonerp_set = 0;
140                 hapd->iface->num_sta_non_erp--;
141                 if (hapd->iface->num_sta_non_erp == 0)
142                         set_beacon++;
143         }
144
145         if (sta->no_short_slot_time_set) {
146                 sta->no_short_slot_time_set = 0;
147                 hapd->iface->num_sta_no_short_slot_time--;
148                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
149                     && hapd->iface->num_sta_no_short_slot_time == 0)
150                         set_beacon++;
151         }
152
153         if (sta->no_short_preamble_set) {
154                 sta->no_short_preamble_set = 0;
155                 hapd->iface->num_sta_no_short_preamble--;
156                 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
157                     && hapd->iface->num_sta_no_short_preamble == 0)
158                         set_beacon++;
159         }
160
161         if (sta->no_ht_gf_set) {
162                 sta->no_ht_gf_set = 0;
163                 hapd->iface->num_sta_ht_no_gf--;
164         }
165
166         if (sta->no_ht_set) {
167                 sta->no_ht_set = 0;
168                 hapd->iface->num_sta_no_ht--;
169         }
170
171         if (sta->ht_20mhz_set) {
172                 sta->ht_20mhz_set = 0;
173                 hapd->iface->num_sta_ht_20mhz--;
174         }
175
176 #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
177         if (hostapd_ht_operation_update(hapd->iface) > 0)
178                 set_beacon++;
179 #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
180
181         if (set_beacon)
182                 ieee802_11_set_beacons(hapd->iface);
183
184         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
185         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
186
187         ieee802_1x_free_station(sta);
188         wpa_auth_sta_deinit(sta->wpa_sm);
189         rsn_preauth_free_station(hapd, sta);
190 #ifndef CONFIG_NO_RADIUS
191         radius_client_flush_auth(hapd->radius, sta->addr);
192 #endif /* CONFIG_NO_RADIUS */
193
194         os_free(sta->last_assoc_req);
195         os_free(sta->challenge);
196
197 #ifdef CONFIG_IEEE80211W
198         os_free(sta->sa_query_trans_id);
199         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
200 #endif /* CONFIG_IEEE80211W */
201
202         wpabuf_free(sta->wps_ie);
203         wpabuf_free(sta->p2p_ie);
204
205         os_free(sta->ht_capabilities);
206
207         os_free(sta);
208 }
209
210
211 void hostapd_free_stas(struct hostapd_data *hapd)
212 {
213         struct sta_info *sta, *prev;
214
215         sta = hapd->sta_list;
216
217         while (sta) {
218                 prev = sta;
219                 if (sta->flags & WLAN_STA_AUTH) {
220                         mlme_deauthenticate_indication(
221                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
222                 }
223                 sta = sta->next;
224                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
225                            MAC2STR(prev->addr));
226                 ap_free_sta(hapd, prev);
227         }
228 }
229
230
231 /**
232  * ap_handle_timer - Per STA timer handler
233  * @eloop_ctx: struct hostapd_data *
234  * @timeout_ctx: struct sta_info *
235  *
236  * This function is called to check station activity and to remove inactive
237  * stations.
238  */
239 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
240 {
241         struct hostapd_data *hapd = eloop_ctx;
242         struct sta_info *sta = timeout_ctx;
243         unsigned long next_time = 0;
244
245         if (sta->timeout_next == STA_REMOVE) {
246                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
247                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
248                                "local deauth request");
249                 ap_free_sta(hapd, sta);
250                 return;
251         }
252
253         if ((sta->flags & WLAN_STA_ASSOC) &&
254             (sta->timeout_next == STA_NULLFUNC ||
255              sta->timeout_next == STA_DISASSOC)) {
256                 int inactive_sec;
257                 wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:",
258                            MAC2STR(sta->addr));
259                 inactive_sec = hapd->drv.get_inact_sec(hapd, sta->addr);
260                 if (inactive_sec == -1) {
261                         wpa_printf(MSG_DEBUG, "Could not get station info "
262                                    "from kernel driver for " MACSTR ".",
263                                    MAC2STR(sta->addr));
264                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
265                            sta->flags & WLAN_STA_ASSOC) {
266                         /* station activity detected; reset timeout state */
267                         wpa_printf(MSG_DEBUG, "  Station has been active");
268                         sta->timeout_next = STA_NULLFUNC;
269                         next_time = hapd->conf->ap_max_inactivity -
270                                 inactive_sec;
271                 }
272         }
273
274         if ((sta->flags & WLAN_STA_ASSOC) &&
275             sta->timeout_next == STA_DISASSOC &&
276             !(sta->flags & WLAN_STA_PENDING_POLL)) {
277                 wpa_printf(MSG_DEBUG, "  Station has ACKed data poll");
278                 /* data nullfunc frame poll did not produce TX errors; assume
279                  * station ACKed it */
280                 sta->timeout_next = STA_NULLFUNC;
281                 next_time = hapd->conf->ap_max_inactivity;
282         }
283
284         if (next_time) {
285                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
286                                        sta);
287                 return;
288         }
289
290         if (sta->timeout_next == STA_NULLFUNC &&
291             (sta->flags & WLAN_STA_ASSOC)) {
292 #ifndef CONFIG_NATIVE_WINDOWS
293                 /* send data frame to poll STA and check whether this frame
294                  * is ACKed */
295                 struct ieee80211_hdr hdr;
296
297                 wpa_printf(MSG_DEBUG, "  Polling STA with data frame");
298                 sta->flags |= WLAN_STA_PENDING_POLL;
299
300                 os_memset(&hdr, 0, sizeof(hdr));
301                 if (hapd->driver &&
302                     os_strcmp(hapd->driver->name, "hostap") == 0) {
303                         /*
304                          * WLAN_FC_STYPE_NULLFUNC would be more appropriate,
305                          * but it is apparently not retried so TX Exc events
306                          * are not received for it.
307                          */
308                         hdr.frame_control =
309                                 IEEE80211_FC(WLAN_FC_TYPE_DATA,
310                                              WLAN_FC_STYPE_DATA);
311                 } else {
312                         hdr.frame_control =
313                                 IEEE80211_FC(WLAN_FC_TYPE_DATA,
314                                              WLAN_FC_STYPE_NULLFUNC);
315                 }
316
317                 hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS);
318                 os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN);
319                 os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr,
320                           ETH_ALEN);
321                 os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN);
322
323                 if (hapd->drv.send_mgmt_frame(hapd, &hdr, sizeof(hdr)) < 0)
324                         perror("ap_handle_timer: send");
325 #endif /* CONFIG_NATIVE_WINDOWS */
326         } else if (sta->timeout_next != STA_REMOVE) {
327                 int deauth = sta->timeout_next == STA_DEAUTH;
328
329                 wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR,
330                            deauth ? "deauthentication" : "disassociation",
331                            MAC2STR(sta->addr));
332
333                 if (deauth) {
334                         hapd->drv.sta_deauth(hapd, sta->addr,
335                                              WLAN_REASON_PREV_AUTH_NOT_VALID);
336                 } else {
337                         hapd->drv.sta_disassoc(
338                                 hapd, sta->addr,
339                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
340                 }
341         }
342
343         switch (sta->timeout_next) {
344         case STA_NULLFUNC:
345                 sta->timeout_next = STA_DISASSOC;
346                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
347                                        hapd, sta);
348                 break;
349         case STA_DISASSOC:
350                 sta->flags &= ~WLAN_STA_ASSOC;
351                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
352                 if (!sta->acct_terminate_cause)
353                         sta->acct_terminate_cause =
354                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
355                 accounting_sta_stop(hapd, sta);
356                 ieee802_1x_free_station(sta);
357                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
358                                HOSTAPD_LEVEL_INFO, "disassociated due to "
359                                "inactivity");
360                 sta->timeout_next = STA_DEAUTH;
361                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
362                                        hapd, sta);
363                 mlme_disassociate_indication(
364                         hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY);
365                 break;
366         case STA_DEAUTH:
367         case STA_REMOVE:
368                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
369                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
370                                "inactivity");
371                 if (!sta->acct_terminate_cause)
372                         sta->acct_terminate_cause =
373                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
374                 mlme_deauthenticate_indication(
375                         hapd, sta,
376                         WLAN_REASON_PREV_AUTH_NOT_VALID);
377                 ap_free_sta(hapd, sta);
378                 break;
379         }
380 }
381
382
383 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
384 {
385         struct hostapd_data *hapd = eloop_ctx;
386         struct sta_info *sta = timeout_ctx;
387         u8 addr[ETH_ALEN];
388
389         if (!(sta->flags & WLAN_STA_AUTH))
390                 return;
391
392         mlme_deauthenticate_indication(hapd, sta,
393                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
394         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
395                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
396                        "session timeout");
397         sta->acct_terminate_cause =
398                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
399         os_memcpy(addr, sta->addr, ETH_ALEN);
400         ap_free_sta(hapd, sta);
401         hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
402 }
403
404
405 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
406                             u32 session_timeout)
407 {
408         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
409                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
410                        "seconds", session_timeout);
411         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
412         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
413                                hapd, sta);
414 }
415
416
417 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
418 {
419         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
420 }
421
422
423 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
424 {
425         struct sta_info *sta;
426
427         sta = ap_get_sta(hapd, addr);
428         if (sta)
429                 return sta;
430
431         wpa_printf(MSG_DEBUG, "  New STA");
432         if (hapd->num_sta >= hapd->conf->max_num_sta) {
433                 /* FIX: might try to remove some old STAs first? */
434                 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
435                            hapd->num_sta, hapd->conf->max_num_sta);
436                 return NULL;
437         }
438
439         sta = os_zalloc(sizeof(struct sta_info));
440         if (sta == NULL) {
441                 wpa_printf(MSG_ERROR, "malloc failed");
442                 return NULL;
443         }
444         sta->acct_interim_interval = hapd->conf->acct_interim_interval;
445
446         /* initialize STA info data */
447         eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
448                                ap_handle_timer, hapd, sta);
449         os_memcpy(sta->addr, addr, ETH_ALEN);
450         sta->next = hapd->sta_list;
451         hapd->sta_list = sta;
452         hapd->num_sta++;
453         ap_sta_hash_add(hapd, sta);
454         sta->ssid = &hapd->conf->ssid;
455         ap_sta_remove_in_other_bss(hapd, sta);
456
457         return sta;
458 }
459
460
461 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
462 {
463         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
464
465         wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
466                    MAC2STR(sta->addr));
467         if (hapd->drv.sta_remove(hapd, sta->addr) &&
468             sta->flags & WLAN_STA_ASSOC) {
469                 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
470                            " from kernel driver.", MAC2STR(sta->addr));
471                 return -1;
472         }
473         return 0;
474 }
475
476
477 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
478                                        struct sta_info *sta)
479 {
480         struct hostapd_iface *iface = hapd->iface;
481         size_t i;
482
483         for (i = 0; i < iface->num_bss; i++) {
484                 struct hostapd_data *bss = iface->bss[i];
485                 struct sta_info *sta2;
486                 /* bss should always be set during operation, but it may be
487                  * NULL during reconfiguration. Assume the STA is not
488                  * associated to another BSS in that case to avoid NULL pointer
489                  * dereferences. */
490                 if (bss == hapd || bss == NULL)
491                         continue;
492                 sta2 = ap_get_sta(bss, sta->addr);
493                 if (!sta2)
494                         continue;
495
496                 ap_sta_disconnect(bss, sta2, sta2->addr,
497                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
498         }
499 }
500
501
502 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
503                          u16 reason)
504 {
505         wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
506                    hapd->conf->iface, MAC2STR(sta->addr));
507         sta->flags &= ~WLAN_STA_ASSOC;
508         ap_sta_remove(hapd, sta);
509         sta->timeout_next = STA_DEAUTH;
510         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
511         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
512                                ap_handle_timer, hapd, sta);
513         accounting_sta_stop(hapd, sta);
514         ieee802_1x_free_station(sta);
515
516         mlme_disassociate_indication(hapd, sta, reason);
517 }
518
519
520 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
521                            u16 reason)
522 {
523         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
524                    hapd->conf->iface, MAC2STR(sta->addr));
525         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
526         ap_sta_remove(hapd, sta);
527         sta->timeout_next = STA_REMOVE;
528         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
529         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
530                                ap_handle_timer, hapd, sta);
531         accounting_sta_stop(hapd, sta);
532         ieee802_1x_free_station(sta);
533
534         mlme_deauthenticate_indication(hapd, sta, reason);
535 }
536
537
538 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
539                      int old_vlanid)
540 {
541 #ifndef CONFIG_NO_VLAN
542         const char *iface;
543         struct hostapd_vlan *vlan = NULL;
544         int ret;
545
546         /*
547          * Do not proceed furthur if the vlan id remains same. We do not want
548          * duplicate dynamic vlan entries.
549          */
550         if (sta->vlan_id == old_vlanid)
551                 return 0;
552
553         /*
554          * During 1x reauth, if the vlan id changes, then remove the old id and
555          * proceed furthur to add the new one.
556          */
557         if (old_vlanid > 0)
558                 vlan_remove_dynamic(hapd, old_vlanid);
559
560         iface = hapd->conf->iface;
561         if (sta->ssid->vlan[0])
562                 iface = sta->ssid->vlan;
563
564         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
565                 sta->vlan_id = 0;
566         else if (sta->vlan_id > 0) {
567                 vlan = hapd->conf->vlan;
568                 while (vlan) {
569                         if (vlan->vlan_id == sta->vlan_id ||
570                             vlan->vlan_id == VLAN_ID_WILDCARD) {
571                                 iface = vlan->ifname;
572                                 break;
573                         }
574                         vlan = vlan->next;
575                 }
576         }
577
578         if (sta->vlan_id > 0 && vlan == NULL) {
579                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
580                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
581                                "binding station to (vlan_id=%d)",
582                                sta->vlan_id);
583                 return -1;
584         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
585                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
586                 if (vlan == NULL) {
587                         hostapd_logger(hapd, sta->addr,
588                                        HOSTAPD_MODULE_IEEE80211,
589                                        HOSTAPD_LEVEL_DEBUG, "could not add "
590                                        "dynamic VLAN interface for vlan_id=%d",
591                                        sta->vlan_id);
592                         return -1;
593                 }
594
595                 iface = vlan->ifname;
596                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
597                         hostapd_logger(hapd, sta->addr,
598                                        HOSTAPD_MODULE_IEEE80211,
599                                        HOSTAPD_LEVEL_DEBUG, "could not "
600                                        "configure encryption for dynamic VLAN "
601                                        "interface for vlan_id=%d",
602                                        sta->vlan_id);
603                 }
604
605                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
606                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
607                                "interface '%s'", iface);
608         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
609                 if (sta->vlan_id > 0) {
610                         vlan->dynamic_vlan++;
611                         hostapd_logger(hapd, sta->addr,
612                                        HOSTAPD_MODULE_IEEE80211,
613                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
614                                        "dynamic VLAN interface '%s'", iface);
615                 }
616
617                 /*
618                  * Update encryption configuration for statically generated
619                  * VLAN interface. This is only used for static WEP
620                  * configuration for the case where hostapd did not yet know
621                  * which keys are to be used when the interface was added.
622                  */
623                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
624                         hostapd_logger(hapd, sta->addr,
625                                        HOSTAPD_MODULE_IEEE80211,
626                                        HOSTAPD_LEVEL_DEBUG, "could not "
627                                        "configure encryption for VLAN "
628                                        "interface for vlan_id=%d",
629                                        sta->vlan_id);
630                 }
631         }
632
633         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
634                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
635                        "'%s'", iface);
636
637         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
638                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
639
640         ret = hapd->drv.set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
641         if (ret < 0) {
642                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
643                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
644                                "entry to vlan_id=%d", sta->vlan_id);
645         }
646         return ret;
647 #else /* CONFIG_NO_VLAN */
648         return 0;
649 #endif /* CONFIG_NO_VLAN */
650 }
651
652
653 #ifdef CONFIG_IEEE80211W
654
655 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
656 {
657         u32 tu;
658         struct os_time now, passed;
659         os_get_time(&now);
660         os_time_sub(&now, &sta->sa_query_start, &passed);
661         tu = (passed.sec * 1000000 + passed.usec) / 1024;
662         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
663                 hostapd_logger(hapd, sta->addr,
664                                HOSTAPD_MODULE_IEEE80211,
665                                HOSTAPD_LEVEL_DEBUG,
666                                "association SA Query timed out");
667                 sta->sa_query_timed_out = 1;
668                 os_free(sta->sa_query_trans_id);
669                 sta->sa_query_trans_id = NULL;
670                 sta->sa_query_count = 0;
671                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
672                 return 1;
673         }
674
675         return 0;
676 }
677
678
679 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
680 {
681         struct hostapd_data *hapd = eloop_ctx;
682         struct sta_info *sta = timeout_ctx;
683         unsigned int timeout, sec, usec;
684         u8 *trans_id, *nbuf;
685
686         if (sta->sa_query_count > 0 &&
687             ap_check_sa_query_timeout(hapd, sta))
688                 return;
689
690         nbuf = os_realloc(sta->sa_query_trans_id,
691                           (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
692         if (nbuf == NULL)
693                 return;
694         if (sta->sa_query_count == 0) {
695                 /* Starting a new SA Query procedure */
696                 os_get_time(&sta->sa_query_start);
697         }
698         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
699         sta->sa_query_trans_id = nbuf;
700         sta->sa_query_count++;
701
702         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
703
704         timeout = hapd->conf->assoc_sa_query_retry_timeout;
705         sec = ((timeout / 1000) * 1024) / 1000;
706         usec = (timeout % 1000) * 1024;
707         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
708
709         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
710                        HOSTAPD_LEVEL_DEBUG,
711                        "association SA Query attempt %d", sta->sa_query_count);
712
713 #ifdef NEED_AP_MLME
714         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
715 #endif /* NEED_AP_MLME */
716 }
717
718
719 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
720 {
721         ap_sa_query_timer(hapd, sta);
722 }
723
724
725 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
726 {
727         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
728         os_free(sta->sa_query_trans_id);
729         sta->sa_query_trans_id = NULL;
730         sta->sa_query_count = 0;
731 }
732
733 #endif /* CONFIG_IEEE80211W */
734
735
736 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
737                        const u8 *addr, u16 reason)
738 {
739
740         if (sta == NULL && addr)
741                 sta = ap_get_sta(hapd, addr);
742
743         if (addr)
744                 hapd->drv.sta_deauth(hapd, addr, reason);
745
746         if (sta == NULL)
747                 return;
748         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED);
749         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
750         eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta);
751         sta->timeout_next = STA_REMOVE;
752 }