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