blob: 8bba73cca769f4fe0e0778ca6a8947179f4de87c [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Station table
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
12#include "utils/eloop.h"
13#include "common/ieee802_11_defs.h"
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080014#include "common/wpa_ctrl.h"
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -080015#include "common/sae.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070016#include "radius/radius.h"
17#include "radius/radius_client.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "p2p/p2p.h"
Dmitry Shmidtd80a4012015-11-05 16:35:40 -080019#include "fst/fst.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070020#include "hostapd.h"
21#include "accounting.h"
22#include "ieee802_1x.h"
23#include "ieee802_11.h"
Dmitry Shmidtd5e49232012-12-03 15:08:10 -080024#include "ieee802_11_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070025#include "wpa_auth.h"
26#include "preauth_auth.h"
27#include "ap_config.h"
28#include "beacon.h"
29#include "ap_mlme.h"
30#include "vlan_init.h"
31#include "p2p_hostapd.h"
32#include "ap_drv_ops.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070033#include "gas_serv.h"
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080034#include "wnm_ap.h"
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -080035#include "ndisc_snoop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036#include "sta_info.h"
37
38static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
39 struct sta_info *sta);
40static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080041static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080042static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
43static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070044#ifdef CONFIG_IEEE80211W
45static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
46#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080047static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49int ap_for_each_sta(struct hostapd_data *hapd,
50 int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
51 void *ctx),
52 void *ctx)
53{
54 struct sta_info *sta;
55
56 for (sta = hapd->sta_list; sta; sta = sta->next) {
57 if (cb(hapd, sta, ctx))
58 return 1;
59 }
60
61 return 0;
62}
63
64
65struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
66{
67 struct sta_info *s;
68
69 s = hapd->sta_hash[STA_HASH(sta)];
70 while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
71 s = s->hnext;
72 return s;
73}
74
75
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070076#ifdef CONFIG_P2P
77struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
78{
79 struct sta_info *sta;
80
81 for (sta = hapd->sta_list; sta; sta = sta->next) {
82 const u8 *p2p_dev_addr;
83
84 if (sta->p2p_ie == NULL)
85 continue;
86
87 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
88 if (p2p_dev_addr == NULL)
89 continue;
90
91 if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
92 return sta;
93 }
94
95 return NULL;
96}
97#endif /* CONFIG_P2P */
98
99
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700100static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
101{
102 struct sta_info *tmp;
103
104 if (hapd->sta_list == sta) {
105 hapd->sta_list = sta->next;
106 return;
107 }
108
109 tmp = hapd->sta_list;
110 while (tmp != NULL && tmp->next != sta)
111 tmp = tmp->next;
112 if (tmp == NULL) {
113 wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
114 "list.", MAC2STR(sta->addr));
115 } else
116 tmp->next = sta->next;
117}
118
119
120void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
121{
122 sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
123 hapd->sta_hash[STA_HASH(sta->addr)] = sta;
124}
125
126
127static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
128{
129 struct sta_info *s;
130
131 s = hapd->sta_hash[STA_HASH(sta->addr)];
132 if (s == NULL) return;
133 if (os_memcmp(s->addr, sta->addr, 6) == 0) {
134 hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
135 return;
136 }
137
138 while (s->hnext != NULL &&
139 os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
140 s = s->hnext;
141 if (s->hnext != NULL)
142 s->hnext = s->hnext->hnext;
143 else
144 wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
145 " from hash table", MAC2STR(sta->addr));
146}
147
148
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800149void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
150{
151 sta_ip6addr_del(hapd, sta);
152}
153
154
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700155void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
156{
157 int set_beacon = 0;
158
159 accounting_sta_stop(hapd, sta);
160
161 /* just in case */
162 ap_sta_set_authorized(hapd, sta, 0);
163
164 if (sta->flags & WLAN_STA_WDS)
Dmitry Shmidtc2ebb4b2013-07-24 12:57:51 -0700165 hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700166
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800167 if (sta->ipaddr)
168 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
169 ap_sta_ip6addr_del(hapd, sta);
170
Dmitry Shmidta38abf92014-03-06 13:38:44 -0800171 if (!hapd->iface->driver_ap_teardown &&
172 !(sta->flags & WLAN_STA_PREAUTH))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700173 hostapd_drv_sta_remove(hapd, sta->addr);
174
175 ap_sta_hash_del(hapd, sta);
176 ap_sta_list_del(hapd, sta);
177
178 if (sta->aid > 0)
179 hapd->sta_aid[(sta->aid - 1) / 32] &=
180 ~BIT((sta->aid - 1) % 32);
181
182 hapd->num_sta--;
183 if (sta->nonerp_set) {
184 sta->nonerp_set = 0;
185 hapd->iface->num_sta_non_erp--;
186 if (hapd->iface->num_sta_non_erp == 0)
187 set_beacon++;
188 }
189
190 if (sta->no_short_slot_time_set) {
191 sta->no_short_slot_time_set = 0;
192 hapd->iface->num_sta_no_short_slot_time--;
193 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
194 && hapd->iface->num_sta_no_short_slot_time == 0)
195 set_beacon++;
196 }
197
198 if (sta->no_short_preamble_set) {
199 sta->no_short_preamble_set = 0;
200 hapd->iface->num_sta_no_short_preamble--;
201 if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
202 && hapd->iface->num_sta_no_short_preamble == 0)
203 set_beacon++;
204 }
205
206 if (sta->no_ht_gf_set) {
207 sta->no_ht_gf_set = 0;
208 hapd->iface->num_sta_ht_no_gf--;
209 }
210
211 if (sta->no_ht_set) {
212 sta->no_ht_set = 0;
213 hapd->iface->num_sta_no_ht--;
214 }
215
216 if (sta->ht_20mhz_set) {
217 sta->ht_20mhz_set = 0;
218 hapd->iface->num_sta_ht_20mhz--;
219 }
220
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700221#ifdef CONFIG_IEEE80211N
222 ht40_intolerant_remove(hapd->iface, sta);
223#endif /* CONFIG_IEEE80211N */
224
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700225#ifdef CONFIG_P2P
226 if (sta->no_p2p_set) {
227 sta->no_p2p_set = 0;
228 hapd->num_sta_no_p2p--;
229 if (hapd->num_sta_no_p2p == 0)
230 hostapd_p2p_non_p2p_sta_disconnected(hapd);
231 }
232#endif /* CONFIG_P2P */
233
234#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
235 if (hostapd_ht_operation_update(hapd->iface) > 0)
236 set_beacon++;
237#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
238
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800239#ifdef CONFIG_MESH
240 if (hapd->mesh_sta_free_cb)
241 hapd->mesh_sta_free_cb(sta);
242#endif /* CONFIG_MESH */
243
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700244 if (set_beacon)
245 ieee802_11_set_beacons(hapd->iface);
246
Dmitry Shmidt04949592012-07-19 12:16:46 -0700247 wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
248 __func__, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700249 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
250 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800251 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800252 ap_sta_clear_disconnect_timeouts(hapd, sta);
Dmitry Shmidtff787d52015-01-12 13:01:47 -0800253 sae_clear_retransmit_timer(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800255 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700256 wpa_auth_sta_deinit(sta->wpa_sm);
257 rsn_preauth_free_station(hapd, sta);
258#ifndef CONFIG_NO_RADIUS
Dmitry Shmidt96571392013-10-14 12:54:46 -0700259 if (hapd->radius)
260 radius_client_flush_auth(hapd->radius, sta->addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700261#endif /* CONFIG_NO_RADIUS */
262
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800263#ifndef CONFIG_NO_VLAN
264 /*
265 * sta->wpa_sm->group needs to be released before so that
266 * vlan_remove_dynamic() can check that no stations are left on the
267 * AP_VLAN netdev.
268 */
269 if (sta->vlan_id_bound) {
270 /*
271 * Need to remove the STA entry before potentially removing the
272 * VLAN.
273 */
274 if (hapd->iface->driver_ap_teardown &&
275 !(sta->flags & WLAN_STA_PREAUTH))
276 hostapd_drv_sta_remove(hapd, sta->addr);
277 vlan_remove_dynamic(hapd, sta->vlan_id_bound);
278 }
279#endif /* CONFIG_NO_VLAN */
280
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 os_free(sta->challenge);
282
283#ifdef CONFIG_IEEE80211W
284 os_free(sta->sa_query_trans_id);
285 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
286#endif /* CONFIG_IEEE80211W */
287
288#ifdef CONFIG_P2P
289 p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
290#endif /* CONFIG_P2P */
291
Dmitry Shmidt04949592012-07-19 12:16:46 -0700292#ifdef CONFIG_INTERWORKING
293 if (sta->gas_dialog) {
294 int i;
295 for (i = 0; i < GAS_DIALOG_MAX; i++)
296 gas_serv_dialog_clear(&sta->gas_dialog[i]);
297 os_free(sta->gas_dialog);
298 }
299#endif /* CONFIG_INTERWORKING */
300
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700301 wpabuf_free(sta->wps_ie);
302 wpabuf_free(sta->p2p_ie);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800303 wpabuf_free(sta->hs20_ie);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800304#ifdef CONFIG_FST
305 wpabuf_free(sta->mb_ies);
306#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700307
308 os_free(sta->ht_capabilities);
Dmitry Shmidt292b0c32013-11-22 12:54:42 -0800309 os_free(sta->vht_capabilities);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800310 hostapd_free_psk_list(sta->psk);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700311 os_free(sta->identity);
312 os_free(sta->radius_cui);
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800313 os_free(sta->remediation_url);
314 wpabuf_free(sta->hs20_deauth_req);
315 os_free(sta->hs20_session_info_url);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800317#ifdef CONFIG_SAE
318 sae_clear_data(sta->sae);
319 os_free(sta->sae);
320#endif /* CONFIG_SAE */
321
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700322 os_free(sta);
323}
324
325
326void hostapd_free_stas(struct hostapd_data *hapd)
327{
328 struct sta_info *sta, *prev;
329
330 sta = hapd->sta_list;
331
332 while (sta) {
333 prev = sta;
334 if (sta->flags & WLAN_STA_AUTH) {
335 mlme_deauthenticate_indication(
336 hapd, sta, WLAN_REASON_UNSPECIFIED);
337 }
338 sta = sta->next;
339 wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
340 MAC2STR(prev->addr));
341 ap_free_sta(hapd, prev);
342 }
343}
344
345
346/**
347 * ap_handle_timer - Per STA timer handler
348 * @eloop_ctx: struct hostapd_data *
349 * @timeout_ctx: struct sta_info *
350 *
351 * This function is called to check station activity and to remove inactive
352 * stations.
353 */
354void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
355{
356 struct hostapd_data *hapd = eloop_ctx;
357 struct sta_info *sta = timeout_ctx;
358 unsigned long next_time = 0;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700359 int reason;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800361 wpa_printf(MSG_DEBUG, "%s: %s: " MACSTR " flags=0x%x timeout_next=%d",
362 hapd->conf->iface, __func__, MAC2STR(sta->addr), sta->flags,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700363 sta->timeout_next);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700364 if (sta->timeout_next == STA_REMOVE) {
365 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
366 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
367 "local deauth request");
368 ap_free_sta(hapd, sta);
369 return;
370 }
371
372 if ((sta->flags & WLAN_STA_ASSOC) &&
373 (sta->timeout_next == STA_NULLFUNC ||
374 sta->timeout_next == STA_DISASSOC)) {
375 int inactive_sec;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700376 /*
377 * Add random value to timeout so that we don't end up bouncing
378 * all stations at the same time if we have lots of associated
379 * stations that are idle (but keep re-associating).
380 */
381 int fuzz = os_random() % 20;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700382 inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
383 if (inactive_sec == -1) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800384 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
385 "Check inactivity: Could not "
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800386 "get station info from kernel driver for "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700387 MACSTR, MAC2STR(sta->addr));
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -0800388 /*
389 * The driver may not support this functionality.
390 * Anyway, try again after the next inactivity timeout,
391 * but do not disconnect the station now.
392 */
Dmitry Shmidt04949592012-07-19 12:16:46 -0700393 next_time = hapd->conf->ap_max_inactivity + fuzz;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800394 } else if (inactive_sec == -ENOENT) {
395 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
396 "Station " MACSTR " has lost its driver entry",
397 MAC2STR(sta->addr));
398
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800399 /* Avoid sending client probe on removed client */
400 sta->timeout_next = STA_DISASSOC;
401 goto skip_poll;
Dmitry Shmidt2f74e362015-01-21 13:19:05 -0800402 } else if (inactive_sec < hapd->conf->ap_max_inactivity) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700403 /* station activity detected; reset timeout state */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800404 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
405 "Station " MACSTR " has been active %is ago",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700406 MAC2STR(sta->addr), inactive_sec);
407 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700408 next_time = hapd->conf->ap_max_inactivity + fuzz -
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700409 inactive_sec;
410 } else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800411 wpa_msg(hapd->msg_ctx, MSG_DEBUG,
412 "Station " MACSTR " has been "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 "inactive too long: %d sec, max allowed: %d",
414 MAC2STR(sta->addr), inactive_sec,
415 hapd->conf->ap_max_inactivity);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800416
417 if (hapd->conf->skip_inactivity_poll)
418 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700419 }
420 }
421
422 if ((sta->flags & WLAN_STA_ASSOC) &&
423 sta->timeout_next == STA_DISASSOC &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800424 !(sta->flags & WLAN_STA_PENDING_POLL) &&
425 !hapd->conf->skip_inactivity_poll) {
426 wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
427 " has ACKed data poll", MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700428 /* data nullfunc frame poll did not produce TX errors; assume
429 * station ACKed it */
430 sta->timeout_next = STA_NULLFUNC;
431 next_time = hapd->conf->ap_max_inactivity;
432 }
433
Dmitry Shmidt7f656022015-02-25 14:36:37 -0800434skip_poll:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700435 if (next_time) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700436 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
437 "for " MACSTR " (%lu seconds)",
438 __func__, MAC2STR(sta->addr), next_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700439 eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
440 sta);
441 return;
442 }
443
444 if (sta->timeout_next == STA_NULLFUNC &&
445 (sta->flags & WLAN_STA_ASSOC)) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800446 wpa_printf(MSG_DEBUG, " Polling STA");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700447 sta->flags |= WLAN_STA_PENDING_POLL;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800448 hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
449 sta->flags & WLAN_STA_WMM);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700450 } else if (sta->timeout_next != STA_REMOVE) {
451 int deauth = sta->timeout_next == STA_DEAUTH;
452
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800453 wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
454 "Timeout, sending %s info to STA " MACSTR,
455 deauth ? "deauthentication" : "disassociation",
456 MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700457
458 if (deauth) {
459 hostapd_drv_sta_deauth(
460 hapd, sta->addr,
461 WLAN_REASON_PREV_AUTH_NOT_VALID);
462 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700463 reason = (sta->timeout_next == STA_DISASSOC) ?
464 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
465 WLAN_REASON_PREV_AUTH_NOT_VALID;
466
467 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700468 }
469 }
470
471 switch (sta->timeout_next) {
472 case STA_NULLFUNC:
473 sta->timeout_next = STA_DISASSOC;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700474 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
475 "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
476 __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700477 eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
478 hapd, sta);
479 break;
480 case STA_DISASSOC:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700481 case STA_DISASSOC_FROM_CLI:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800482 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700483 sta->flags &= ~WLAN_STA_ASSOC;
484 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
485 if (!sta->acct_terminate_cause)
486 sta->acct_terminate_cause =
487 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
488 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800489 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700490 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
491 HOSTAPD_LEVEL_INFO, "disassociated due to "
492 "inactivity");
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700493 reason = (sta->timeout_next == STA_DISASSOC) ?
494 WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
495 WLAN_REASON_PREV_AUTH_NOT_VALID;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700496 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700497 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
498 "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
499 __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
501 hapd, sta);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700502 mlme_disassociate_indication(hapd, sta, reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700503 break;
504 case STA_DEAUTH:
505 case STA_REMOVE:
506 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
507 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800508 "inactivity (timer DEAUTH/REMOVE)");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700509 if (!sta->acct_terminate_cause)
510 sta->acct_terminate_cause =
511 RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
512 mlme_deauthenticate_indication(
513 hapd, sta,
514 WLAN_REASON_PREV_AUTH_NOT_VALID);
515 ap_free_sta(hapd, sta);
516 break;
517 }
518}
519
520
521static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
522{
523 struct hostapd_data *hapd = eloop_ctx;
524 struct sta_info *sta = timeout_ctx;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800526 wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
527 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt04949592012-07-19 12:16:46 -0700528 if (!(sta->flags & WLAN_STA_AUTH)) {
529 if (sta->flags & WLAN_STA_GAS) {
530 wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
531 "entry " MACSTR, MAC2STR(sta->addr));
532 ap_free_sta(hapd, sta);
533 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700534 return;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700535 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536
Dmitry Shmidt818ea482014-03-10 13:15:21 -0700537 hostapd_drv_sta_deauth(hapd, sta->addr,
538 WLAN_REASON_PREV_AUTH_NOT_VALID);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700539 mlme_deauthenticate_indication(hapd, sta,
540 WLAN_REASON_PREV_AUTH_NOT_VALID);
541 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
542 HOSTAPD_LEVEL_INFO, "deauthenticated due to "
543 "session timeout");
544 sta->acct_terminate_cause =
545 RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700546 ap_free_sta(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700547}
548
549
Dmitry Shmidt54605472013-11-08 11:10:19 -0800550void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
551 u32 session_timeout)
552{
553 if (eloop_replenish_timeout(session_timeout, 0,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800554 ap_handle_session_timer, hapd, sta) == 1) {
Dmitry Shmidt54605472013-11-08 11:10:19 -0800555 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
556 HOSTAPD_LEVEL_DEBUG, "setting session timeout "
557 "to %d seconds", session_timeout);
558 }
559}
560
561
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700562void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
563 u32 session_timeout)
564{
565 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
566 HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
567 "seconds", session_timeout);
568 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
569 eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
570 hapd, sta);
571}
572
573
574void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
575{
576 eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
577}
578
579
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800580static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
581{
582#ifdef CONFIG_WNM
583 struct hostapd_data *hapd = eloop_ctx;
584 struct sta_info *sta = timeout_ctx;
585
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800586 wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
587 MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800588 if (sta->hs20_session_info_url == NULL)
589 return;
590
591 wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
592 sta->hs20_disassoc_timer);
593#endif /* CONFIG_WNM */
594}
595
596
597void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
598 struct sta_info *sta, int warning_time)
599{
600 eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
601 eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
602 hapd, sta);
603}
604
605
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
607{
608 struct sta_info *sta;
609
610 sta = ap_get_sta(hapd, addr);
611 if (sta)
612 return sta;
613
614 wpa_printf(MSG_DEBUG, " New STA");
615 if (hapd->num_sta >= hapd->conf->max_num_sta) {
616 /* FIX: might try to remove some old STAs first? */
617 wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
618 hapd->num_sta, hapd->conf->max_num_sta);
619 return NULL;
620 }
621
622 sta = os_zalloc(sizeof(struct sta_info));
623 if (sta == NULL) {
624 wpa_printf(MSG_ERROR, "malloc failed");
625 return NULL;
626 }
627 sta->acct_interim_interval = hapd->conf->acct_interim_interval;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800628 accounting_sta_get_id(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700629
Dmitry Shmidt01904cf2013-12-05 11:08:35 -0800630 if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
631 wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
632 "for " MACSTR " (%d seconds - ap_max_inactivity)",
633 __func__, MAC2STR(addr),
634 hapd->conf->ap_max_inactivity);
635 eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
636 ap_handle_timer, hapd, sta);
637 }
638
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700639 /* initialize STA info data */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 os_memcpy(sta->addr, addr, ETH_ALEN);
641 sta->next = hapd->sta_list;
642 hapd->sta_list = sta;
643 hapd->num_sta++;
644 ap_sta_hash_add(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700645 ap_sta_remove_in_other_bss(hapd, sta);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800646 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
647 dl_list_init(&sta->ip6addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700648
649 return sta;
650}
651
652
653static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
654{
655 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
656
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800657 if (sta->ipaddr)
658 hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
659 ap_sta_ip6addr_del(hapd, sta);
660
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800661 wpa_printf(MSG_DEBUG, "%s: Removing STA " MACSTR " from kernel driver",
662 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663 if (hostapd_drv_sta_remove(hapd, sta->addr) &&
664 sta->flags & WLAN_STA_ASSOC) {
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800665 wpa_printf(MSG_DEBUG, "%s: Could not remove station " MACSTR
666 " from kernel driver",
667 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700668 return -1;
669 }
670 return 0;
671}
672
673
674static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
675 struct sta_info *sta)
676{
677 struct hostapd_iface *iface = hapd->iface;
678 size_t i;
679
680 for (i = 0; i < iface->num_bss; i++) {
681 struct hostapd_data *bss = iface->bss[i];
682 struct sta_info *sta2;
683 /* bss should always be set during operation, but it may be
684 * NULL during reconfiguration. Assume the STA is not
685 * associated to another BSS in that case to avoid NULL pointer
686 * dereferences. */
687 if (bss == hapd || bss == NULL)
688 continue;
689 sta2 = ap_get_sta(bss, sta->addr);
690 if (!sta2)
691 continue;
692
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800693 wpa_printf(MSG_DEBUG, "%s: disconnect old STA " MACSTR
694 " association from another BSS %s",
695 hapd->conf->iface, MAC2STR(sta2->addr),
696 bss->conf->iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700697 ap_sta_disconnect(bss, sta2, sta2->addr,
698 WLAN_REASON_PREV_AUTH_NOT_VALID);
699 }
700}
701
702
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800703static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
704{
705 struct hostapd_data *hapd = eloop_ctx;
706 struct sta_info *sta = timeout_ctx;
707
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800708 wpa_printf(MSG_DEBUG, "%s: Disassociation callback for STA " MACSTR,
709 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800710 ap_sta_remove(hapd, sta);
711 mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
712}
713
714
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700715void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
716 u16 reason)
717{
718 wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
719 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800720 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
Dmitry Shmidt700a1372013-03-15 14:14:44 -0700721 sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800722 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 sta->timeout_next = STA_DEAUTH;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700724 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
725 "for " MACSTR " (%d seconds - "
726 "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
727 __func__, MAC2STR(sta->addr),
728 AP_MAX_INACTIVITY_AFTER_DISASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700729 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
730 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
731 ap_handle_timer, hapd, sta);
732 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800733 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700734
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800735 sta->disassoc_reason = reason;
736 sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
737 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
738 eloop_register_timeout(hapd->iface->drv_flags &
739 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
740 ap_sta_disassoc_cb_timeout, hapd, sta);
741}
742
743
744static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
745{
746 struct hostapd_data *hapd = eloop_ctx;
747 struct sta_info *sta = timeout_ctx;
748
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800749 wpa_printf(MSG_DEBUG, "%s: Deauthentication callback for STA " MACSTR,
750 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800751 ap_sta_remove(hapd, sta);
752 mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700753}
754
755
756void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
757 u16 reason)
758{
759 wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
760 hapd->conf->iface, MAC2STR(sta->addr));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800761 sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
762 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800763 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700765 wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
766 "for " MACSTR " (%d seconds - "
767 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
768 __func__, MAC2STR(sta->addr),
769 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
771 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
772 ap_handle_timer, hapd, sta);
773 accounting_sta_stop(hapd, sta);
Dmitry Shmidtde47be72016-01-07 12:52:55 -0800774 ieee802_1x_free_station(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700775
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800776 sta->deauth_reason = reason;
777 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
778 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
779 eloop_register_timeout(hapd->iface->drv_flags &
780 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
781 ap_sta_deauth_cb_timeout, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782}
783
784
Dmitry Shmidt04949592012-07-19 12:16:46 -0700785#ifdef CONFIG_WPS
786int ap_sta_wps_cancel(struct hostapd_data *hapd,
787 struct sta_info *sta, void *ctx)
788{
789 if (sta && (sta->flags & WLAN_STA_WPS)) {
790 ap_sta_deauthenticate(hapd, sta,
791 WLAN_REASON_PREV_AUTH_NOT_VALID);
792 wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
793 __func__, MAC2STR(sta->addr));
794 return 1;
795 }
796
797 return 0;
798}
799#endif /* CONFIG_WPS */
800
801
Dmitry Shmidt83474442015-04-15 13:47:09 -0700802int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803{
804#ifndef CONFIG_NO_VLAN
805 const char *iface;
806 struct hostapd_vlan *vlan = NULL;
807 int ret;
Dmitry Shmidt83474442015-04-15 13:47:09 -0700808 int old_vlanid = sta->vlan_id_bound;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700809
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700810 iface = hapd->conf->iface;
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700811 if (hapd->conf->ssid.vlan[0])
812 iface = hapd->conf->ssid.vlan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700813
Dmitry Shmidt9d9e6022015-04-23 10:34:55 -0700814 if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 sta->vlan_id = 0;
816 else if (sta->vlan_id > 0) {
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -0700817 struct hostapd_vlan *wildcard_vlan = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818 vlan = hapd->conf->vlan;
819 while (vlan) {
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -0700820 if (vlan->vlan_id == sta->vlan_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700821 break;
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -0700822 if (vlan->vlan_id == VLAN_ID_WILDCARD)
823 wildcard_vlan = vlan;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700824 vlan = vlan->next;
825 }
Dmitry Shmidtd5c075b2013-08-05 14:36:10 -0700826 if (!vlan)
827 vlan = wildcard_vlan;
828 if (vlan)
829 iface = vlan->ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700830 }
831
Dmitry Shmidt83474442015-04-15 13:47:09 -0700832 /*
833 * Do not increment ref counters if the VLAN ID remains same, but do
834 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
835 * have been called before.
836 */
837 if (sta->vlan_id == old_vlanid)
838 goto skip_counting;
839
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 if (sta->vlan_id > 0 && vlan == NULL) {
841 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
842 HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
843 "binding station to (vlan_id=%d)",
844 sta->vlan_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800845 ret = -1;
846 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700847 } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
848 vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
849 if (vlan == NULL) {
850 hostapd_logger(hapd, sta->addr,
851 HOSTAPD_MODULE_IEEE80211,
852 HOSTAPD_LEVEL_DEBUG, "could not add "
853 "dynamic VLAN interface for vlan_id=%d",
854 sta->vlan_id);
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800855 ret = -1;
856 goto done;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857 }
858
859 iface = vlan->ifname;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700860 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
861 HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
862 "interface '%s'", iface);
Dmitry Shmidtd80a4012015-11-05 16:35:40 -0800863 } else if (vlan && vlan->vlan_id == sta->vlan_id &&
864 vlan->dynamic_vlan > 0) {
865 vlan->dynamic_vlan++;
866 hostapd_logger(hapd, sta->addr,
867 HOSTAPD_MODULE_IEEE80211,
868 HOSTAPD_LEVEL_DEBUG,
869 "updated existing dynamic VLAN interface '%s'",
870 iface);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 }
872
Dmitry Shmidt83474442015-04-15 13:47:09 -0700873 /* ref counters have been increased, so mark the station */
874 sta->vlan_id_bound = sta->vlan_id;
875
876skip_counting:
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700877 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
878 HOSTAPD_LEVEL_DEBUG, "binding station to interface "
879 "'%s'", iface);
880
881 if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
882 wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
883
884 ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
885 if (ret < 0) {
886 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
887 HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
888 "entry to vlan_id=%d", sta->vlan_id);
889 }
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800890
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800891 /* During 1x reauth, if the vlan id changes, then remove the old id. */
Dmitry Shmidt83474442015-04-15 13:47:09 -0700892 if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800893 vlan_remove_dynamic(hapd, old_vlanid);
Dmitry Shmidt83474442015-04-15 13:47:09 -0700894done:
Dmitry Shmidt216983b2015-02-06 10:50:36 -0800895
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 return ret;
897#else /* CONFIG_NO_VLAN */
898 return 0;
899#endif /* CONFIG_NO_VLAN */
900}
901
902
903#ifdef CONFIG_IEEE80211W
904
905int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
906{
907 u32 tu;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800908 struct os_reltime now, passed;
909 os_get_reltime(&now);
910 os_reltime_sub(&now, &sta->sa_query_start, &passed);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700911 tu = (passed.sec * 1000000 + passed.usec) / 1024;
912 if (hapd->conf->assoc_sa_query_max_timeout < tu) {
913 hostapd_logger(hapd, sta->addr,
914 HOSTAPD_MODULE_IEEE80211,
915 HOSTAPD_LEVEL_DEBUG,
916 "association SA Query timed out");
917 sta->sa_query_timed_out = 1;
918 os_free(sta->sa_query_trans_id);
919 sta->sa_query_trans_id = NULL;
920 sta->sa_query_count = 0;
921 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
922 return 1;
923 }
924
925 return 0;
926}
927
928
929static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
930{
931 struct hostapd_data *hapd = eloop_ctx;
932 struct sta_info *sta = timeout_ctx;
933 unsigned int timeout, sec, usec;
934 u8 *trans_id, *nbuf;
935
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -0800936 wpa_printf(MSG_DEBUG, "%s: SA Query timer for STA " MACSTR
937 " (count=%d)",
938 hapd->conf->iface, MAC2STR(sta->addr), sta->sa_query_count);
939
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700940 if (sta->sa_query_count > 0 &&
941 ap_check_sa_query_timeout(hapd, sta))
942 return;
943
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700944 nbuf = os_realloc_array(sta->sa_query_trans_id,
945 sta->sa_query_count + 1,
946 WLAN_SA_QUERY_TR_ID_LEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 if (nbuf == NULL)
948 return;
949 if (sta->sa_query_count == 0) {
950 /* Starting a new SA Query procedure */
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800951 os_get_reltime(&sta->sa_query_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700952 }
953 trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
954 sta->sa_query_trans_id = nbuf;
955 sta->sa_query_count++;
956
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800957 if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
958 /*
959 * We don't really care which ID is used here, so simply
960 * hardcode this if the mostly theoretical os_get_random()
961 * failure happens.
962 */
963 trans_id[0] = 0x12;
964 trans_id[1] = 0x34;
965 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700966
967 timeout = hapd->conf->assoc_sa_query_retry_timeout;
968 sec = ((timeout / 1000) * 1024) / 1000;
969 usec = (timeout % 1000) * 1024;
970 eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
971
972 hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
973 HOSTAPD_LEVEL_DEBUG,
974 "association SA Query attempt %d", sta->sa_query_count);
975
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700977}
978
979
980void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
981{
982 ap_sa_query_timer(hapd, sta);
983}
984
985
986void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
987{
988 eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
989 os_free(sta->sa_query_trans_id);
990 sta->sa_query_trans_id = NULL;
991 sta->sa_query_count = 0;
992}
993
994#endif /* CONFIG_IEEE80211W */
995
996
997void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
998 int authorized)
999{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001000 const u8 *dev_addr = NULL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001001 char buf[100];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001002#ifdef CONFIG_P2P
1003 u8 addr[ETH_ALEN];
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001004 u8 ip_addr_buf[4];
Dmitry Shmidt04949592012-07-19 12:16:46 -07001005#endif /* CONFIG_P2P */
1006
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001007 if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1008 return;
1009
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001010 if (authorized)
1011 sta->flags |= WLAN_STA_AUTHORIZED;
1012 else
1013 sta->flags &= ~WLAN_STA_AUTHORIZED;
1014
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001015#ifdef CONFIG_P2P
Dmitry Shmidt04949592012-07-19 12:16:46 -07001016 if (hapd->p2p_group == NULL) {
1017 if (sta->p2p_ie != NULL &&
1018 p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1019 dev_addr = addr;
1020 } else
1021 dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001022
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001023 if (dev_addr)
1024 os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1025 MAC2STR(sta->addr), MAC2STR(dev_addr));
1026 else
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001027#endif /* CONFIG_P2P */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001028 os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1029
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001030 if (hapd->sta_authorized_cb)
1031 hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1032 sta->addr, authorized, dev_addr);
1033
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001034 if (authorized) {
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001035 char ip_addr[100];
1036 ip_addr[0] = '\0';
1037#ifdef CONFIG_P2P
1038 if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1039 os_snprintf(ip_addr, sizeof(ip_addr),
1040 " ip_addr=%u.%u.%u.%u",
1041 ip_addr_buf[0], ip_addr_buf[1],
1042 ip_addr_buf[2], ip_addr_buf[3]);
1043 }
1044#endif /* CONFIG_P2P */
1045
1046 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
1047 buf, ip_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001049 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001050 hapd->msg_ctx_parent != hapd->msg_ctx)
1051 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001052 AP_STA_CONNECTED "%s%s",
1053 buf, ip_addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001054 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001055 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1056
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001057 if (hapd->msg_ctx_parent &&
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001058 hapd->msg_ctx_parent != hapd->msg_ctx)
1059 wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1060 AP_STA_DISCONNECTED "%s", buf);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001061 }
Dmitry Shmidtd80a4012015-11-05 16:35:40 -08001062
1063#ifdef CONFIG_FST
1064 if (hapd->iface->fst) {
1065 if (authorized)
1066 fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1067 else
1068 fst_notify_peer_disconnected(hapd->iface->fst,
1069 sta->addr);
1070 }
1071#endif /* CONFIG_FST */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072}
1073
1074
1075void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1076 const u8 *addr, u16 reason)
1077{
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001078 if (sta)
1079 wpa_printf(MSG_DEBUG, "%s: %s STA " MACSTR " reason=%u",
1080 hapd->conf->iface, __func__, MAC2STR(sta->addr),
1081 reason);
1082 else if (addr)
1083 wpa_printf(MSG_DEBUG, "%s: %s addr " MACSTR " reason=%u",
1084 hapd->conf->iface, __func__, MAC2STR(addr),
1085 reason);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001086
1087 if (sta == NULL && addr)
1088 sta = ap_get_sta(hapd, addr);
1089
1090 if (addr)
1091 hostapd_drv_sta_deauth(hapd, addr, reason);
1092
1093 if (sta == NULL)
1094 return;
1095 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001096 wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1097 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001099 wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
Dmitry Shmidt04949592012-07-19 12:16:46 -07001100 "for " MACSTR " (%d seconds - "
1101 "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001102 hapd->conf->iface, __func__, MAC2STR(sta->addr),
Dmitry Shmidt04949592012-07-19 12:16:46 -07001103 AP_MAX_INACTIVITY_AFTER_DEAUTH);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001104 eloop_cancel_timeout(ap_handle_timer, hapd, sta);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001105 eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1106 ap_handle_timer, hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001107 sta->timeout_next = STA_REMOVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001108
1109 sta->deauth_reason = reason;
1110 sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1111 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1112 eloop_register_timeout(hapd->iface->drv_flags &
1113 WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1114 ap_sta_deauth_cb_timeout, hapd, sta);
1115}
1116
1117
1118void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1119{
1120 if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1121 wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1122 return;
1123 }
1124 sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1125 eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1126 ap_sta_deauth_cb_timeout(hapd, sta);
1127}
1128
1129
1130void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1131{
1132 if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1133 wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1134 return;
1135 }
1136 sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1137 eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1138 ap_sta_disassoc_cb_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001139}
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001140
1141
Dmitry Shmidt1d6bf422016-01-19 15:51:35 -08001142void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1143 struct sta_info *sta)
1144{
1145 if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0)
1146 wpa_printf(MSG_DEBUG,
1147 "%s: Removed ap_sta_deauth_cb_timeout timeout for "
1148 MACSTR,
1149 hapd->conf->iface, MAC2STR(sta->addr));
1150 if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0)
1151 wpa_printf(MSG_DEBUG,
1152 "%s: Removed ap_sta_disassoc_cb_timeout timeout for "
1153 MACSTR,
1154 hapd->conf->iface, MAC2STR(sta->addr));
1155}
1156
1157
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001158int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1159{
1160 int res;
1161
1162 buf[0] = '\0';
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001163 res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001164 (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1165 (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1166 (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1167 (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1168 ""),
1169 (flags & WLAN_STA_SHORT_PREAMBLE ?
1170 "[SHORT_PREAMBLE]" : ""),
1171 (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1172 (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1173 (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1174 (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1175 (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1176 (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1177 (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1178 (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1179 (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1180 (flags & WLAN_STA_VHT ? "[VHT]" : ""),
Dmitry Shmidt2f74e362015-01-21 13:19:05 -08001181 (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001182 (flags & WLAN_STA_WNM_SLEEP_MODE ?
1183 "[WNM_SLEEP_MODE]" : ""));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001184 if (os_snprintf_error(buflen, res))
1185 res = -1;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001186
1187 return res;
1188}