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