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