Add wps_cancel for hostapd_cli
[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 #ifdef CONFIG_WPS
596 int ap_sta_wps_cancel(struct hostapd_data *hapd,
597                       struct sta_info *sta, void *ctx)
598 {
599         if (sta && (sta->flags & WLAN_STA_WPS)) {
600                 ap_sta_deauthenticate(hapd, sta,
601                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
602                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
603                            __func__, MAC2STR(sta->addr));
604                 return 1;
605         }
606
607         return 0;
608 }
609 #endif /* CONFIG_WPS */
610
611
612 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
613                      int old_vlanid)
614 {
615 #ifndef CONFIG_NO_VLAN
616         const char *iface;
617         struct hostapd_vlan *vlan = NULL;
618         int ret;
619
620         /*
621          * Do not proceed furthur if the vlan id remains same. We do not want
622          * duplicate dynamic vlan entries.
623          */
624         if (sta->vlan_id == old_vlanid)
625                 return 0;
626
627         /*
628          * During 1x reauth, if the vlan id changes, then remove the old id and
629          * proceed furthur to add the new one.
630          */
631         if (old_vlanid > 0)
632                 vlan_remove_dynamic(hapd, old_vlanid);
633
634         iface = hapd->conf->iface;
635         if (sta->ssid->vlan[0])
636                 iface = sta->ssid->vlan;
637
638         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
639                 sta->vlan_id = 0;
640         else if (sta->vlan_id > 0) {
641                 vlan = hapd->conf->vlan;
642                 while (vlan) {
643                         if (vlan->vlan_id == sta->vlan_id ||
644                             vlan->vlan_id == VLAN_ID_WILDCARD) {
645                                 iface = vlan->ifname;
646                                 break;
647                         }
648                         vlan = vlan->next;
649                 }
650         }
651
652         if (sta->vlan_id > 0 && vlan == NULL) {
653                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
654                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
655                                "binding station to (vlan_id=%d)",
656                                sta->vlan_id);
657                 return -1;
658         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
659                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
660                 if (vlan == NULL) {
661                         hostapd_logger(hapd, sta->addr,
662                                        HOSTAPD_MODULE_IEEE80211,
663                                        HOSTAPD_LEVEL_DEBUG, "could not add "
664                                        "dynamic VLAN interface for vlan_id=%d",
665                                        sta->vlan_id);
666                         return -1;
667                 }
668
669                 iface = vlan->ifname;
670                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
671                         hostapd_logger(hapd, sta->addr,
672                                        HOSTAPD_MODULE_IEEE80211,
673                                        HOSTAPD_LEVEL_DEBUG, "could not "
674                                        "configure encryption for dynamic VLAN "
675                                        "interface for vlan_id=%d",
676                                        sta->vlan_id);
677                 }
678
679                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
680                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
681                                "interface '%s'", iface);
682         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
683                 if (sta->vlan_id > 0) {
684                         vlan->dynamic_vlan++;
685                         hostapd_logger(hapd, sta->addr,
686                                        HOSTAPD_MODULE_IEEE80211,
687                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
688                                        "dynamic VLAN interface '%s'", iface);
689                 }
690
691                 /*
692                  * Update encryption configuration for statically generated
693                  * VLAN interface. This is only used for static WEP
694                  * configuration for the case where hostapd did not yet know
695                  * which keys are to be used when the interface was added.
696                  */
697                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
698                         hostapd_logger(hapd, sta->addr,
699                                        HOSTAPD_MODULE_IEEE80211,
700                                        HOSTAPD_LEVEL_DEBUG, "could not "
701                                        "configure encryption for VLAN "
702                                        "interface for vlan_id=%d",
703                                        sta->vlan_id);
704                 }
705         }
706
707         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
708                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
709                        "'%s'", iface);
710
711         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
712                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
713
714         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
715         if (ret < 0) {
716                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
717                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
718                                "entry to vlan_id=%d", sta->vlan_id);
719         }
720         return ret;
721 #else /* CONFIG_NO_VLAN */
722         return 0;
723 #endif /* CONFIG_NO_VLAN */
724 }
725
726
727 #ifdef CONFIG_IEEE80211W
728
729 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
730 {
731         u32 tu;
732         struct os_time now, passed;
733         os_get_time(&now);
734         os_time_sub(&now, &sta->sa_query_start, &passed);
735         tu = (passed.sec * 1000000 + passed.usec) / 1024;
736         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
737                 hostapd_logger(hapd, sta->addr,
738                                HOSTAPD_MODULE_IEEE80211,
739                                HOSTAPD_LEVEL_DEBUG,
740                                "association SA Query timed out");
741                 sta->sa_query_timed_out = 1;
742                 os_free(sta->sa_query_trans_id);
743                 sta->sa_query_trans_id = NULL;
744                 sta->sa_query_count = 0;
745                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
746                 return 1;
747         }
748
749         return 0;
750 }
751
752
753 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
754 {
755         struct hostapd_data *hapd = eloop_ctx;
756         struct sta_info *sta = timeout_ctx;
757         unsigned int timeout, sec, usec;
758         u8 *trans_id, *nbuf;
759
760         if (sta->sa_query_count > 0 &&
761             ap_check_sa_query_timeout(hapd, sta))
762                 return;
763
764         nbuf = os_realloc(sta->sa_query_trans_id,
765                           (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN);
766         if (nbuf == NULL)
767                 return;
768         if (sta->sa_query_count == 0) {
769                 /* Starting a new SA Query procedure */
770                 os_get_time(&sta->sa_query_start);
771         }
772         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
773         sta->sa_query_trans_id = nbuf;
774         sta->sa_query_count++;
775
776         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
777
778         timeout = hapd->conf->assoc_sa_query_retry_timeout;
779         sec = ((timeout / 1000) * 1024) / 1000;
780         usec = (timeout % 1000) * 1024;
781         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
782
783         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
784                        HOSTAPD_LEVEL_DEBUG,
785                        "association SA Query attempt %d", sta->sa_query_count);
786
787 #ifdef NEED_AP_MLME
788         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
789 #endif /* NEED_AP_MLME */
790 }
791
792
793 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
794 {
795         ap_sa_query_timer(hapd, sta);
796 }
797
798
799 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
800 {
801         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
802         os_free(sta->sa_query_trans_id);
803         sta->sa_query_trans_id = NULL;
804         sta->sa_query_count = 0;
805 }
806
807 #endif /* CONFIG_IEEE80211W */
808
809
810 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
811                            int authorized)
812 {
813         const u8 *dev_addr = NULL;
814         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
815                 return;
816
817 #ifdef CONFIG_P2P
818         dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
819 #endif /* CONFIG_P2P */
820
821         if (authorized) {
822                 if (dev_addr)
823                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
824                                 MACSTR " p2p_dev_addr=" MACSTR,
825                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
826                 else
827                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED
828                                 MACSTR, MAC2STR(sta->addr));
829                 if (hapd->msg_ctx_parent &&
830                     hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
831                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
832                                 AP_STA_CONNECTED MACSTR " p2p_dev_addr="
833                                 MACSTR,
834                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
835                 else if (hapd->msg_ctx_parent &&
836                          hapd->msg_ctx_parent != hapd->msg_ctx)
837                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
838                                 AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
839
840                 sta->flags |= WLAN_STA_AUTHORIZED;
841         } else {
842                 if (dev_addr)
843                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
844                                 MACSTR " p2p_dev_addr=" MACSTR,
845                                 MAC2STR(sta->addr), MAC2STR(dev_addr));
846                 else
847                         wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED
848                                 MACSTR, MAC2STR(sta->addr));
849                 if (hapd->msg_ctx_parent &&
850                     hapd->msg_ctx_parent != hapd->msg_ctx && dev_addr)
851                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
852                                 AP_STA_DISCONNECTED MACSTR " p2p_dev_addr="
853                                 MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
854                 else if (hapd->msg_ctx_parent &&
855                          hapd->msg_ctx_parent != hapd->msg_ctx)
856                         wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
857                                 AP_STA_DISCONNECTED MACSTR,
858                                 MAC2STR(sta->addr));
859                 sta->flags &= ~WLAN_STA_AUTHORIZED;
860         }
861
862         if (hapd->sta_authorized_cb)
863                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
864                                         sta->addr, authorized, dev_addr);
865 }
866
867
868 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
869                        const u8 *addr, u16 reason)
870 {
871
872         if (sta == NULL && addr)
873                 sta = ap_get_sta(hapd, addr);
874
875         if (addr)
876                 hostapd_drv_sta_deauth(hapd, addr, reason);
877
878         if (sta == NULL)
879                 return;
880         ap_sta_set_authorized(hapd, sta, 0);
881         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
882         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
883         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
884         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
885         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
886                                ap_handle_timer, hapd, sta);
887         sta->timeout_next = STA_REMOVE;
888
889         sta->deauth_reason = reason;
890         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
891         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
892         eloop_register_timeout(hapd->iface->drv_flags &
893                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
894                                ap_sta_deauth_cb_timeout, hapd, sta);
895 }
896
897
898 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
899 {
900         if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
901                 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
902                 return;
903         }
904         sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
905         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
906         ap_sta_deauth_cb_timeout(hapd, sta);
907 }
908
909
910 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
911 {
912         if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
913                 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
914                 return;
915         }
916         sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
917         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
918         ap_sta_disassoc_cb_timeout(hapd, sta);
919 }