HS 2.0R2 AP: Use Subscr Remediation request from RADIUS server
[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
270 #ifdef CONFIG_SAE
271         sae_clear_data(sta->sae);
272         os_free(sta->sae);
273 #endif /* CONFIG_SAE */
274
275         os_free(sta);
276 }
277
278
279 void hostapd_free_stas(struct hostapd_data *hapd)
280 {
281         struct sta_info *sta, *prev;
282
283         sta = hapd->sta_list;
284
285         while (sta) {
286                 prev = sta;
287                 if (sta->flags & WLAN_STA_AUTH) {
288                         mlme_deauthenticate_indication(
289                                 hapd, sta, WLAN_REASON_UNSPECIFIED);
290                 }
291                 sta = sta->next;
292                 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
293                            MAC2STR(prev->addr));
294                 ap_free_sta(hapd, prev);
295         }
296 }
297
298
299 /**
300  * ap_handle_timer - Per STA timer handler
301  * @eloop_ctx: struct hostapd_data *
302  * @timeout_ctx: struct sta_info *
303  *
304  * This function is called to check station activity and to remove inactive
305  * stations.
306  */
307 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
308 {
309         struct hostapd_data *hapd = eloop_ctx;
310         struct sta_info *sta = timeout_ctx;
311         unsigned long next_time = 0;
312         int reason;
313
314         wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
315                    __func__, MAC2STR(sta->addr), sta->flags,
316                    sta->timeout_next);
317         if (sta->timeout_next == STA_REMOVE) {
318                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
319                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
320                                "local deauth request");
321                 ap_free_sta(hapd, sta);
322                 return;
323         }
324
325         if ((sta->flags & WLAN_STA_ASSOC) &&
326             (sta->timeout_next == STA_NULLFUNC ||
327              sta->timeout_next == STA_DISASSOC)) {
328                 int inactive_sec;
329                 /*
330                  * Add random value to timeout so that we don't end up bouncing
331                  * all stations at the same time if we have lots of associated
332                  * stations that are idle (but keep re-associating).
333                  */
334                 int fuzz = os_random() % 20;
335                 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
336                 if (inactive_sec == -1) {
337                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
338                                 "Check inactivity: Could not "
339                                 "get station info from kernel driver for "
340                                 MACSTR, MAC2STR(sta->addr));
341                         /*
342                          * The driver may not support this functionality.
343                          * Anyway, try again after the next inactivity timeout,
344                          * but do not disconnect the station now.
345                          */
346                         next_time = hapd->conf->ap_max_inactivity + fuzz;
347                 } else if (inactive_sec < hapd->conf->ap_max_inactivity &&
348                            sta->flags & WLAN_STA_ASSOC) {
349                         /* station activity detected; reset timeout state */
350                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
351                                 "Station " MACSTR " has been active %is ago",
352                                 MAC2STR(sta->addr), inactive_sec);
353                         sta->timeout_next = STA_NULLFUNC;
354                         next_time = hapd->conf->ap_max_inactivity + fuzz -
355                                 inactive_sec;
356                 } else {
357                         wpa_msg(hapd->msg_ctx, MSG_DEBUG,
358                                 "Station " MACSTR " has been "
359                                 "inactive too long: %d sec, max allowed: %d",
360                                 MAC2STR(sta->addr), inactive_sec,
361                                 hapd->conf->ap_max_inactivity);
362
363                         if (hapd->conf->skip_inactivity_poll)
364                                 sta->timeout_next = STA_DISASSOC;
365                 }
366         }
367
368         if ((sta->flags & WLAN_STA_ASSOC) &&
369             sta->timeout_next == STA_DISASSOC &&
370             !(sta->flags & WLAN_STA_PENDING_POLL) &&
371             !hapd->conf->skip_inactivity_poll) {
372                 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
373                         " has ACKed data poll", MAC2STR(sta->addr));
374                 /* data nullfunc frame poll did not produce TX errors; assume
375                  * station ACKed it */
376                 sta->timeout_next = STA_NULLFUNC;
377                 next_time = hapd->conf->ap_max_inactivity;
378         }
379
380         if (next_time) {
381                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
382                            "for " MACSTR " (%lu seconds)",
383                            __func__, MAC2STR(sta->addr), next_time);
384                 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
385                                        sta);
386                 return;
387         }
388
389         if (sta->timeout_next == STA_NULLFUNC &&
390             (sta->flags & WLAN_STA_ASSOC)) {
391                 wpa_printf(MSG_DEBUG, "  Polling STA");
392                 sta->flags |= WLAN_STA_PENDING_POLL;
393                 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
394                                         sta->flags & WLAN_STA_WMM);
395         } else if (sta->timeout_next != STA_REMOVE) {
396                 int deauth = sta->timeout_next == STA_DEAUTH;
397
398                 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
399                         "Timeout, sending %s info to STA " MACSTR,
400                         deauth ? "deauthentication" : "disassociation",
401                         MAC2STR(sta->addr));
402
403                 if (deauth) {
404                         hostapd_drv_sta_deauth(
405                                 hapd, sta->addr,
406                                 WLAN_REASON_PREV_AUTH_NOT_VALID);
407                 } else {
408                         reason = (sta->timeout_next == STA_DISASSOC) ?
409                                 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
410                                 WLAN_REASON_PREV_AUTH_NOT_VALID;
411
412                         hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
413                 }
414         }
415
416         switch (sta->timeout_next) {
417         case STA_NULLFUNC:
418                 sta->timeout_next = STA_DISASSOC;
419                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
420                            "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
421                            __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
422                 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
423                                        hapd, sta);
424                 break;
425         case STA_DISASSOC:
426         case STA_DISASSOC_FROM_CLI:
427                 ap_sta_set_authorized(hapd, sta, 0);
428                 sta->flags &= ~WLAN_STA_ASSOC;
429                 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
430                 if (!sta->acct_terminate_cause)
431                         sta->acct_terminate_cause =
432                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
433                 accounting_sta_stop(hapd, sta);
434                 ieee802_1x_free_station(sta);
435                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
436                                HOSTAPD_LEVEL_INFO, "disassociated due to "
437                                "inactivity");
438                 reason = (sta->timeout_next == STA_DISASSOC) ?
439                         WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
440                         WLAN_REASON_PREV_AUTH_NOT_VALID;
441                 sta->timeout_next = STA_DEAUTH;
442                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
443                            "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
444                            __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
445                 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
446                                        hapd, sta);
447                 mlme_disassociate_indication(hapd, sta, reason);
448                 break;
449         case STA_DEAUTH:
450         case STA_REMOVE:
451                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
452                                HOSTAPD_LEVEL_INFO, "deauthenticated due to "
453                                "inactivity (timer DEAUTH/REMOVE)");
454                 if (!sta->acct_terminate_cause)
455                         sta->acct_terminate_cause =
456                                 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
457                 mlme_deauthenticate_indication(
458                         hapd, sta,
459                         WLAN_REASON_PREV_AUTH_NOT_VALID);
460                 ap_free_sta(hapd, sta);
461                 break;
462         }
463 }
464
465
466 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
467 {
468         struct hostapd_data *hapd = eloop_ctx;
469         struct sta_info *sta = timeout_ctx;
470         u8 addr[ETH_ALEN];
471
472         if (!(sta->flags & WLAN_STA_AUTH)) {
473                 if (sta->flags & WLAN_STA_GAS) {
474                         wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
475                                    "entry " MACSTR, MAC2STR(sta->addr));
476                         ap_free_sta(hapd, sta);
477                 }
478                 return;
479         }
480
481         mlme_deauthenticate_indication(hapd, sta,
482                                        WLAN_REASON_PREV_AUTH_NOT_VALID);
483         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
484                        HOSTAPD_LEVEL_INFO, "deauthenticated due to "
485                        "session timeout");
486         sta->acct_terminate_cause =
487                 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
488         os_memcpy(addr, sta->addr, ETH_ALEN);
489         ap_free_sta(hapd, sta);
490         hostapd_drv_sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID);
491 }
492
493
494 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
495                               u32 session_timeout)
496 {
497         if (eloop_replenish_timeout(session_timeout, 0,
498                                     ap_handle_session_timer, hapd, sta) == 1) {
499                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
500                                HOSTAPD_LEVEL_DEBUG, "setting session timeout "
501                                "to %d seconds", session_timeout);
502         }
503 }
504
505
506 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
507                             u32 session_timeout)
508 {
509         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
510                        HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
511                        "seconds", session_timeout);
512         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
513         eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
514                                hapd, sta);
515 }
516
517
518 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
519 {
520         eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
521 }
522
523
524 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
525 {
526         struct sta_info *sta;
527
528         sta = ap_get_sta(hapd, addr);
529         if (sta)
530                 return sta;
531
532         wpa_printf(MSG_DEBUG, "  New STA");
533         if (hapd->num_sta >= hapd->conf->max_num_sta) {
534                 /* FIX: might try to remove some old STAs first? */
535                 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
536                            hapd->num_sta, hapd->conf->max_num_sta);
537                 return NULL;
538         }
539
540         sta = os_zalloc(sizeof(struct sta_info));
541         if (sta == NULL) {
542                 wpa_printf(MSG_ERROR, "malloc failed");
543                 return NULL;
544         }
545         sta->acct_interim_interval = hapd->conf->acct_interim_interval;
546         accounting_sta_get_id(hapd, sta);
547
548         if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
549                 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
550                            "for " MACSTR " (%d seconds - ap_max_inactivity)",
551                            __func__, MAC2STR(addr),
552                            hapd->conf->ap_max_inactivity);
553                 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
554                                        ap_handle_timer, hapd, sta);
555         }
556
557         /* initialize STA info data */
558         os_memcpy(sta->addr, addr, ETH_ALEN);
559         sta->next = hapd->sta_list;
560         hapd->sta_list = sta;
561         hapd->num_sta++;
562         ap_sta_hash_add(hapd, sta);
563         sta->ssid = &hapd->conf->ssid;
564         ap_sta_remove_in_other_bss(hapd, sta);
565
566         return sta;
567 }
568
569
570 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
571 {
572         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
573
574         wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
575                    MAC2STR(sta->addr));
576         if (hostapd_drv_sta_remove(hapd, sta->addr) &&
577             sta->flags & WLAN_STA_ASSOC) {
578                 wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
579                            " from kernel driver.", MAC2STR(sta->addr));
580                 return -1;
581         }
582         return 0;
583 }
584
585
586 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
587                                        struct sta_info *sta)
588 {
589         struct hostapd_iface *iface = hapd->iface;
590         size_t i;
591
592         for (i = 0; i < iface->num_bss; i++) {
593                 struct hostapd_data *bss = iface->bss[i];
594                 struct sta_info *sta2;
595                 /* bss should always be set during operation, but it may be
596                  * NULL during reconfiguration. Assume the STA is not
597                  * associated to another BSS in that case to avoid NULL pointer
598                  * dereferences. */
599                 if (bss == hapd || bss == NULL)
600                         continue;
601                 sta2 = ap_get_sta(bss, sta->addr);
602                 if (!sta2)
603                         continue;
604
605                 ap_sta_disconnect(bss, sta2, sta2->addr,
606                                   WLAN_REASON_PREV_AUTH_NOT_VALID);
607         }
608 }
609
610
611 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
612 {
613         struct hostapd_data *hapd = eloop_ctx;
614         struct sta_info *sta = timeout_ctx;
615
616         ap_sta_remove(hapd, sta);
617         mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
618 }
619
620
621 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
622                          u16 reason)
623 {
624         wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
625                    hapd->conf->iface, MAC2STR(sta->addr));
626         sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
627         ap_sta_set_authorized(hapd, sta, 0);
628         sta->timeout_next = STA_DEAUTH;
629         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
630                    "for " MACSTR " (%d seconds - "
631                    "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
632                    __func__, MAC2STR(sta->addr),
633                    AP_MAX_INACTIVITY_AFTER_DISASSOC);
634         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
635         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
636                                ap_handle_timer, hapd, sta);
637         accounting_sta_stop(hapd, sta);
638         ieee802_1x_free_station(sta);
639
640         sta->disassoc_reason = reason;
641         sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
642         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
643         eloop_register_timeout(hapd->iface->drv_flags &
644                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
645                                ap_sta_disassoc_cb_timeout, hapd, sta);
646 }
647
648
649 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
650 {
651         struct hostapd_data *hapd = eloop_ctx;
652         struct sta_info *sta = timeout_ctx;
653
654         ap_sta_remove(hapd, sta);
655         mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
656 }
657
658
659 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
660                            u16 reason)
661 {
662         wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
663                    hapd->conf->iface, MAC2STR(sta->addr));
664         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
665         ap_sta_set_authorized(hapd, sta, 0);
666         sta->timeout_next = STA_REMOVE;
667         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
668                    "for " MACSTR " (%d seconds - "
669                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
670                    __func__, MAC2STR(sta->addr),
671                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
672         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
673         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
674                                ap_handle_timer, hapd, sta);
675         accounting_sta_stop(hapd, sta);
676         ieee802_1x_free_station(sta);
677
678         sta->deauth_reason = reason;
679         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
680         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
681         eloop_register_timeout(hapd->iface->drv_flags &
682                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
683                                ap_sta_deauth_cb_timeout, hapd, sta);
684 }
685
686
687 #ifdef CONFIG_WPS
688 int ap_sta_wps_cancel(struct hostapd_data *hapd,
689                       struct sta_info *sta, void *ctx)
690 {
691         if (sta && (sta->flags & WLAN_STA_WPS)) {
692                 ap_sta_deauthenticate(hapd, sta,
693                                       WLAN_REASON_PREV_AUTH_NOT_VALID);
694                 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
695                            __func__, MAC2STR(sta->addr));
696                 return 1;
697         }
698
699         return 0;
700 }
701 #endif /* CONFIG_WPS */
702
703
704 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
705                      int old_vlanid)
706 {
707 #ifndef CONFIG_NO_VLAN
708         const char *iface;
709         struct hostapd_vlan *vlan = NULL;
710         int ret;
711
712         /*
713          * Do not proceed furthur if the vlan id remains same. We do not want
714          * duplicate dynamic vlan entries.
715          */
716         if (sta->vlan_id == old_vlanid)
717                 return 0;
718
719         /*
720          * During 1x reauth, if the vlan id changes, then remove the old id and
721          * proceed furthur to add the new one.
722          */
723         if (old_vlanid > 0)
724                 vlan_remove_dynamic(hapd, old_vlanid);
725
726         iface = hapd->conf->iface;
727         if (sta->ssid->vlan[0])
728                 iface = sta->ssid->vlan;
729
730         if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
731                 sta->vlan_id = 0;
732         else if (sta->vlan_id > 0) {
733                 struct hostapd_vlan *wildcard_vlan = NULL;
734                 vlan = hapd->conf->vlan;
735                 while (vlan) {
736                         if (vlan->vlan_id == sta->vlan_id)
737                                 break;
738                         if (vlan->vlan_id == VLAN_ID_WILDCARD)
739                                 wildcard_vlan = vlan;
740                         vlan = vlan->next;
741                 }
742                 if (!vlan)
743                         vlan = wildcard_vlan;
744                 if (vlan)
745                         iface = vlan->ifname;
746         }
747
748         if (sta->vlan_id > 0 && vlan == NULL) {
749                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
750                                HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
751                                "binding station to (vlan_id=%d)",
752                                sta->vlan_id);
753                 return -1;
754         } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
755                 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
756                 if (vlan == NULL) {
757                         hostapd_logger(hapd, sta->addr,
758                                        HOSTAPD_MODULE_IEEE80211,
759                                        HOSTAPD_LEVEL_DEBUG, "could not add "
760                                        "dynamic VLAN interface for vlan_id=%d",
761                                        sta->vlan_id);
762                         return -1;
763                 }
764
765                 iface = vlan->ifname;
766                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
767                         hostapd_logger(hapd, sta->addr,
768                                        HOSTAPD_MODULE_IEEE80211,
769                                        HOSTAPD_LEVEL_DEBUG, "could not "
770                                        "configure encryption for dynamic VLAN "
771                                        "interface for vlan_id=%d",
772                                        sta->vlan_id);
773                 }
774
775                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
776                                HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
777                                "interface '%s'", iface);
778         } else if (vlan && vlan->vlan_id == sta->vlan_id) {
779                 if (sta->vlan_id > 0) {
780                         vlan->dynamic_vlan++;
781                         hostapd_logger(hapd, sta->addr,
782                                        HOSTAPD_MODULE_IEEE80211,
783                                        HOSTAPD_LEVEL_DEBUG, "updated existing "
784                                        "dynamic VLAN interface '%s'", iface);
785                 }
786
787                 /*
788                  * Update encryption configuration for statically generated
789                  * VLAN interface. This is only used for static WEP
790                  * configuration for the case where hostapd did not yet know
791                  * which keys are to be used when the interface was added.
792                  */
793                 if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
794                         hostapd_logger(hapd, sta->addr,
795                                        HOSTAPD_MODULE_IEEE80211,
796                                        HOSTAPD_LEVEL_DEBUG, "could not "
797                                        "configure encryption for VLAN "
798                                        "interface for vlan_id=%d",
799                                        sta->vlan_id);
800                 }
801         }
802
803         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
804                        HOSTAPD_LEVEL_DEBUG, "binding station to interface "
805                        "'%s'", iface);
806
807         if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
808                 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
809
810         ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
811         if (ret < 0) {
812                 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
813                                HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
814                                "entry to vlan_id=%d", sta->vlan_id);
815         }
816         return ret;
817 #else /* CONFIG_NO_VLAN */
818         return 0;
819 #endif /* CONFIG_NO_VLAN */
820 }
821
822
823 #ifdef CONFIG_IEEE80211W
824
825 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
826 {
827         u32 tu;
828         struct os_reltime now, passed;
829         os_get_reltime(&now);
830         os_reltime_sub(&now, &sta->sa_query_start, &passed);
831         tu = (passed.sec * 1000000 + passed.usec) / 1024;
832         if (hapd->conf->assoc_sa_query_max_timeout < tu) {
833                 hostapd_logger(hapd, sta->addr,
834                                HOSTAPD_MODULE_IEEE80211,
835                                HOSTAPD_LEVEL_DEBUG,
836                                "association SA Query timed out");
837                 sta->sa_query_timed_out = 1;
838                 os_free(sta->sa_query_trans_id);
839                 sta->sa_query_trans_id = NULL;
840                 sta->sa_query_count = 0;
841                 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
842                 return 1;
843         }
844
845         return 0;
846 }
847
848
849 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
850 {
851         struct hostapd_data *hapd = eloop_ctx;
852         struct sta_info *sta = timeout_ctx;
853         unsigned int timeout, sec, usec;
854         u8 *trans_id, *nbuf;
855
856         if (sta->sa_query_count > 0 &&
857             ap_check_sa_query_timeout(hapd, sta))
858                 return;
859
860         nbuf = os_realloc_array(sta->sa_query_trans_id,
861                                 sta->sa_query_count + 1,
862                                 WLAN_SA_QUERY_TR_ID_LEN);
863         if (nbuf == NULL)
864                 return;
865         if (sta->sa_query_count == 0) {
866                 /* Starting a new SA Query procedure */
867                 os_get_reltime(&sta->sa_query_start);
868         }
869         trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
870         sta->sa_query_trans_id = nbuf;
871         sta->sa_query_count++;
872
873         os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
874
875         timeout = hapd->conf->assoc_sa_query_retry_timeout;
876         sec = ((timeout / 1000) * 1024) / 1000;
877         usec = (timeout % 1000) * 1024;
878         eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
879
880         hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
881                        HOSTAPD_LEVEL_DEBUG,
882                        "association SA Query attempt %d", sta->sa_query_count);
883
884         ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
885 }
886
887
888 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
889 {
890         ap_sa_query_timer(hapd, sta);
891 }
892
893
894 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
895 {
896         eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
897         os_free(sta->sa_query_trans_id);
898         sta->sa_query_trans_id = NULL;
899         sta->sa_query_count = 0;
900 }
901
902 #endif /* CONFIG_IEEE80211W */
903
904
905 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
906                            int authorized)
907 {
908         const u8 *dev_addr = NULL;
909         char buf[100];
910 #ifdef CONFIG_P2P
911         u8 addr[ETH_ALEN];
912         u8 ip_addr_buf[4];
913 #endif /* CONFIG_P2P */
914
915         if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
916                 return;
917
918 #ifdef CONFIG_P2P
919         if (hapd->p2p_group == NULL) {
920                 if (sta->p2p_ie != NULL &&
921                     p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
922                         dev_addr = addr;
923         } else
924                 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
925 #endif /* CONFIG_P2P */
926
927         if (dev_addr)
928                 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
929                             MAC2STR(sta->addr), MAC2STR(dev_addr));
930         else
931                 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
932
933         if (authorized) {
934                 char ip_addr[100];
935                 ip_addr[0] = '\0';
936 #ifdef CONFIG_P2P
937                 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
938                         os_snprintf(ip_addr, sizeof(ip_addr),
939                                     " ip_addr=%u.%u.%u.%u",
940                                     ip_addr_buf[0], ip_addr_buf[1],
941                                     ip_addr_buf[2], ip_addr_buf[3]);
942                 }
943 #endif /* CONFIG_P2P */
944
945                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
946                         buf, ip_addr);
947
948                 if (hapd->msg_ctx_parent &&
949                     hapd->msg_ctx_parent != hapd->msg_ctx)
950                         wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
951                                           AP_STA_CONNECTED "%s%s",
952                                           buf, ip_addr);
953
954                 sta->flags |= WLAN_STA_AUTHORIZED;
955         } else {
956                 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
957
958                 if (hapd->msg_ctx_parent &&
959                     hapd->msg_ctx_parent != hapd->msg_ctx)
960                         wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
961                                           AP_STA_DISCONNECTED "%s", buf);
962
963                 sta->flags &= ~WLAN_STA_AUTHORIZED;
964         }
965
966         if (hapd->sta_authorized_cb)
967                 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
968                                         sta->addr, authorized, dev_addr);
969 }
970
971
972 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
973                        const u8 *addr, u16 reason)
974 {
975
976         if (sta == NULL && addr)
977                 sta = ap_get_sta(hapd, addr);
978
979         if (addr)
980                 hostapd_drv_sta_deauth(hapd, addr, reason);
981
982         if (sta == NULL)
983                 return;
984         ap_sta_set_authorized(hapd, sta, 0);
985         wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
986         ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
987         sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
988         wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
989                    "for " MACSTR " (%d seconds - "
990                    "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
991                    __func__, MAC2STR(sta->addr),
992                    AP_MAX_INACTIVITY_AFTER_DEAUTH);
993         eloop_cancel_timeout(ap_handle_timer, hapd, sta);
994         eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
995                                ap_handle_timer, hapd, sta);
996         sta->timeout_next = STA_REMOVE;
997
998         sta->deauth_reason = reason;
999         sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1000         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1001         eloop_register_timeout(hapd->iface->drv_flags &
1002                                WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1003                                ap_sta_deauth_cb_timeout, hapd, sta);
1004 }
1005
1006
1007 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1008 {
1009         if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1010                 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1011                 return;
1012         }
1013         sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1014         eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1015         ap_sta_deauth_cb_timeout(hapd, sta);
1016 }
1017
1018
1019 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1020 {
1021         if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1022                 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1023                 return;
1024         }
1025         sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1026         eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1027         ap_sta_disassoc_cb_timeout(hapd, sta);
1028 }
1029
1030
1031 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1032 {
1033         int res;
1034
1035         buf[0] = '\0';
1036         res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1037                           (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1038                           (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1039                           (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1040                           (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1041                            ""),
1042                           (flags & WLAN_STA_SHORT_PREAMBLE ?
1043                            "[SHORT_PREAMBLE]" : ""),
1044                           (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1045                           (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1046                           (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1047                           (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1048                           (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1049                           (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1050                           (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1051                           (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1052                           (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1053                           (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1054                           (flags & WLAN_STA_WNM_SLEEP_MODE ?
1055                            "[WNM_SLEEP_MODE]" : ""));
1056
1057         return res;
1058 }