]>
Commit | Line | Data |
---|---|---|
6fc6879b JM |
1 | /* |
2 | * hostapd / Station table | |
bdee6fce | 3 | * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi> |
6fc6879b JM |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License version 2 as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * Alternatively, this software may be distributed under the terms of BSD | |
10 | * license. | |
11 | * | |
12 | * See README and COPYING for more details. | |
13 | */ | |
14 | ||
6226e38d | 15 | #include "utils/includes.h" |
6fc6879b | 16 | |
6226e38d JM |
17 | #include "utils/common.h" |
18 | #include "utils/eloop.h" | |
81f4f619 | 19 | #include "common/ieee802_11_defs.h" |
bdee6fce JM |
20 | #include "radius/radius.h" |
21 | #include "radius/radius_client.h" | |
22 | #include "drivers/driver.h" | |
8ccbe415 | 23 | #include "p2p/p2p.h" |
6fc6879b | 24 | #include "hostapd.h" |
6fc6879b JM |
25 | #include "accounting.h" |
26 | #include "ieee802_1x.h" | |
27 | #include "ieee802_11.h" | |
6226e38d JM |
28 | #include "wpa_auth.h" |
29 | #include "preauth_auth.h" | |
30 | #include "ap_config.h" | |
6fc6879b | 31 | #include "beacon.h" |
6226e38d | 32 | #include "ap_mlme.h" |
6fc6879b | 33 | #include "vlan_init.h" |
aefb53bd | 34 | #include "p2p_hostapd.h" |
cee7d66b | 35 | #include "ap_drv_ops.h" |
6226e38d | 36 | #include "sta_info.h" |
6fc6879b | 37 | |
53f3d6f3 FF |
38 | static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd, |
39 | struct sta_info *sta); | |
6fc6879b | 40 | static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx); |
5d22a1d5 | 41 | #ifdef CONFIG_IEEE80211W |
93b76319 | 42 | static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx); |
5d22a1d5 | 43 | #endif /* CONFIG_IEEE80211W */ |
6fc6879b JM |
44 | |
45 | int ap_for_each_sta(struct hostapd_data *hapd, | |
46 | int (*cb)(struct hostapd_data *hapd, struct sta_info *sta, | |
47 | void *ctx), | |
48 | void *ctx) | |
49 | { | |
50 | struct sta_info *sta; | |
51 | ||
52 | for (sta = hapd->sta_list; sta; sta = sta->next) { | |
53 | if (cb(hapd, sta, ctx)) | |
54 | return 1; | |
55 | } | |
56 | ||
57 | return 0; | |
58 | } | |
59 | ||
60 | ||
61 | struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta) | |
62 | { | |
63 | struct sta_info *s; | |
64 | ||
65 | s = hapd->sta_hash[STA_HASH(sta)]; | |
66 | while (s != NULL && os_memcmp(s->addr, sta, 6) != 0) | |
67 | s = s->hnext; | |
68 | return s; | |
69 | } | |
70 | ||
71 | ||
72 | static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta) | |
73 | { | |
74 | struct sta_info *tmp; | |
75 | ||
76 | if (hapd->sta_list == sta) { | |
77 | hapd->sta_list = sta->next; | |
78 | return; | |
79 | } | |
80 | ||
81 | tmp = hapd->sta_list; | |
82 | while (tmp != NULL && tmp->next != sta) | |
83 | tmp = tmp->next; | |
84 | if (tmp == NULL) { | |
85 | wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from " | |
86 | "list.", MAC2STR(sta->addr)); | |
87 | } else | |
88 | tmp->next = sta->next; | |
89 | } | |
90 | ||
91 | ||
92 | void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta) | |
93 | { | |
94 | sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)]; | |
95 | hapd->sta_hash[STA_HASH(sta->addr)] = sta; | |
96 | } | |
97 | ||
98 | ||
99 | static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta) | |
100 | { | |
101 | struct sta_info *s; | |
102 | ||
103 | s = hapd->sta_hash[STA_HASH(sta->addr)]; | |
104 | if (s == NULL) return; | |
105 | if (os_memcmp(s->addr, sta->addr, 6) == 0) { | |
106 | hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext; | |
107 | return; | |
108 | } | |
109 | ||
110 | while (s->hnext != NULL && | |
111 | os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0) | |
112 | s = s->hnext; | |
113 | if (s->hnext != NULL) | |
114 | s->hnext = s->hnext->hnext; | |
115 | else | |
116 | wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR | |
117 | " from hash table", MAC2STR(sta->addr)); | |
118 | } | |
119 | ||
120 | ||
121 | void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta) | |
122 | { | |
123 | int set_beacon = 0; | |
124 | ||
125 | accounting_sta_stop(hapd, sta); | |
126 | ||
39f42d11 FF |
127 | if (sta->flags & WLAN_STA_WDS) |
128 | hapd->drv.set_wds_sta(hapd, sta->addr, sta->aid, 0); | |
53f3d6f3 FF |
129 | |
130 | if (!(sta->flags & WLAN_STA_PREAUTH)) | |
bdee6fce | 131 | hapd->drv.sta_remove(hapd, sta->addr); |
6fc6879b JM |
132 | |
133 | ap_sta_hash_del(hapd, sta); | |
134 | ap_sta_list_del(hapd, sta); | |
135 | ||
136 | if (sta->aid > 0) | |
2991469c JM |
137 | hapd->sta_aid[(sta->aid - 1) / 32] &= |
138 | ~BIT((sta->aid - 1) % 32); | |
6fc6879b JM |
139 | |
140 | hapd->num_sta--; | |
141 | if (sta->nonerp_set) { | |
142 | sta->nonerp_set = 0; | |
143 | hapd->iface->num_sta_non_erp--; | |
144 | if (hapd->iface->num_sta_non_erp == 0) | |
145 | set_beacon++; | |
146 | } | |
147 | ||
148 | if (sta->no_short_slot_time_set) { | |
149 | sta->no_short_slot_time_set = 0; | |
150 | hapd->iface->num_sta_no_short_slot_time--; | |
151 | if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G | |
152 | && hapd->iface->num_sta_no_short_slot_time == 0) | |
153 | set_beacon++; | |
154 | } | |
155 | ||
156 | if (sta->no_short_preamble_set) { | |
157 | sta->no_short_preamble_set = 0; | |
158 | hapd->iface->num_sta_no_short_preamble--; | |
159 | if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G | |
160 | && hapd->iface->num_sta_no_short_preamble == 0) | |
161 | set_beacon++; | |
162 | } | |
163 | ||
e8ff1e59 JM |
164 | if (sta->no_ht_gf_set) { |
165 | sta->no_ht_gf_set = 0; | |
166 | hapd->iface->num_sta_ht_no_gf--; | |
167 | } | |
168 | ||
169 | if (sta->no_ht_set) { | |
170 | sta->no_ht_set = 0; | |
de9289c8 | 171 | hapd->iface->num_sta_no_ht--; |
e8ff1e59 JM |
172 | } |
173 | ||
174 | if (sta->ht_20mhz_set) { | |
175 | sta->ht_20mhz_set = 0; | |
176 | hapd->iface->num_sta_ht_20mhz--; | |
177 | } | |
de9289c8 | 178 | |
aefb53bd JM |
179 | #ifdef CONFIG_P2P |
180 | if (sta->no_p2p_set) { | |
181 | sta->no_p2p_set = 0; | |
182 | hapd->num_sta_no_p2p--; | |
183 | if (hapd->num_sta_no_p2p == 0) | |
184 | hostapd_p2p_non_p2p_sta_disconnected(hapd); | |
185 | } | |
186 | #endif /* CONFIG_P2P */ | |
187 | ||
d45354be | 188 | #if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N) |
de9289c8 JM |
189 | if (hostapd_ht_operation_update(hapd->iface) > 0) |
190 | set_beacon++; | |
d45354be | 191 | #endif /* NEED_AP_MLME && CONFIG_IEEE80211N */ |
de9289c8 | 192 | |
6fc6879b JM |
193 | if (set_beacon) |
194 | ieee802_11_set_beacons(hapd->iface); | |
195 | ||
196 | eloop_cancel_timeout(ap_handle_timer, hapd, sta); | |
197 | eloop_cancel_timeout(ap_handle_session_timer, hapd, sta); | |
198 | ||
199 | ieee802_1x_free_station(sta); | |
200 | wpa_auth_sta_deinit(sta->wpa_sm); | |
201 | rsn_preauth_free_station(hapd, sta); | |
74784010 | 202 | #ifndef CONFIG_NO_RADIUS |
6fc6879b | 203 | radius_client_flush_auth(hapd->radius, sta->addr); |
74784010 | 204 | #endif /* CONFIG_NO_RADIUS */ |
6fc6879b JM |
205 | |
206 | os_free(sta->last_assoc_req); | |
207 | os_free(sta->challenge); | |
5d22a1d5 JM |
208 | |
209 | #ifdef CONFIG_IEEE80211W | |
93b76319 JM |
210 | os_free(sta->sa_query_trans_id); |
211 | eloop_cancel_timeout(ap_sa_query_timer, hapd, sta); | |
5d22a1d5 JM |
212 | #endif /* CONFIG_IEEE80211W */ |
213 | ||
8ccbe415 JM |
214 | #ifdef CONFIG_P2P |
215 | p2p_group_notif_disassoc(hapd->p2p_group, sta->addr); | |
216 | #endif /* CONFIG_P2P */ | |
217 | ||
eb76b7e3 | 218 | wpabuf_free(sta->wps_ie); |
b305c684 | 219 | wpabuf_free(sta->p2p_ie); |
eb76b7e3 | 220 | |
df84268a JM |
221 | os_free(sta->ht_capabilities); |
222 | ||
6fc6879b JM |
223 | os_free(sta); |
224 | } | |
225 | ||
226 | ||
227 | void hostapd_free_stas(struct hostapd_data *hapd) | |
228 | { | |
229 | struct sta_info *sta, *prev; | |
230 | ||
231 | sta = hapd->sta_list; | |
232 | ||
233 | while (sta) { | |
234 | prev = sta; | |
235 | if (sta->flags & WLAN_STA_AUTH) { | |
236 | mlme_deauthenticate_indication( | |
237 | hapd, sta, WLAN_REASON_UNSPECIFIED); | |
238 | } | |
239 | sta = sta->next; | |
240 | wpa_printf(MSG_DEBUG, "Removing station " MACSTR, | |
241 | MAC2STR(prev->addr)); | |
242 | ap_free_sta(hapd, prev); | |
243 | } | |
244 | } | |
245 | ||
246 | ||
1c6e69cc JM |
247 | /** |
248 | * ap_handle_timer - Per STA timer handler | |
249 | * @eloop_ctx: struct hostapd_data * | |
250 | * @timeout_ctx: struct sta_info * | |
251 | * | |
252 | * This function is called to check station activity and to remove inactive | |
253 | * stations. | |
254 | */ | |
6fc6879b JM |
255 | void ap_handle_timer(void *eloop_ctx, void *timeout_ctx) |
256 | { | |
257 | struct hostapd_data *hapd = eloop_ctx; | |
258 | struct sta_info *sta = timeout_ctx; | |
259 | unsigned long next_time = 0; | |
260 | ||
261 | if (sta->timeout_next == STA_REMOVE) { | |
262 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
263 | HOSTAPD_LEVEL_INFO, "deauthenticated due to " | |
264 | "local deauth request"); | |
265 | ap_free_sta(hapd, sta); | |
266 | return; | |
267 | } | |
268 | ||
269 | if ((sta->flags & WLAN_STA_ASSOC) && | |
270 | (sta->timeout_next == STA_NULLFUNC || | |
271 | sta->timeout_next == STA_DISASSOC)) { | |
272 | int inactive_sec; | |
273 | wpa_printf(MSG_DEBUG, "Checking STA " MACSTR " inactivity:", | |
274 | MAC2STR(sta->addr)); | |
bdee6fce | 275 | inactive_sec = hapd->drv.get_inact_sec(hapd, sta->addr); |
6fc6879b JM |
276 | if (inactive_sec == -1) { |
277 | wpa_printf(MSG_DEBUG, "Could not get station info " | |
278 | "from kernel driver for " MACSTR ".", | |
279 | MAC2STR(sta->addr)); | |
280 | } else if (inactive_sec < hapd->conf->ap_max_inactivity && | |
281 | sta->flags & WLAN_STA_ASSOC) { | |
282 | /* station activity detected; reset timeout state */ | |
283 | wpa_printf(MSG_DEBUG, " Station has been active"); | |
284 | sta->timeout_next = STA_NULLFUNC; | |
285 | next_time = hapd->conf->ap_max_inactivity - | |
286 | inactive_sec; | |
287 | } | |
288 | } | |
289 | ||
290 | if ((sta->flags & WLAN_STA_ASSOC) && | |
291 | sta->timeout_next == STA_DISASSOC && | |
292 | !(sta->flags & WLAN_STA_PENDING_POLL)) { | |
293 | wpa_printf(MSG_DEBUG, " Station has ACKed data poll"); | |
294 | /* data nullfunc frame poll did not produce TX errors; assume | |
295 | * station ACKed it */ | |
296 | sta->timeout_next = STA_NULLFUNC; | |
297 | next_time = hapd->conf->ap_max_inactivity; | |
298 | } | |
299 | ||
300 | if (next_time) { | |
301 | eloop_register_timeout(next_time, 0, ap_handle_timer, hapd, | |
302 | sta); | |
303 | return; | |
304 | } | |
305 | ||
306 | if (sta->timeout_next == STA_NULLFUNC && | |
307 | (sta->flags & WLAN_STA_ASSOC)) { | |
08230317 | 308 | #ifndef CONFIG_NATIVE_WINDOWS |
6fc6879b JM |
309 | /* send data frame to poll STA and check whether this frame |
310 | * is ACKed */ | |
311 | struct ieee80211_hdr hdr; | |
312 | ||
313 | wpa_printf(MSG_DEBUG, " Polling STA with data frame"); | |
314 | sta->flags |= WLAN_STA_PENDING_POLL; | |
315 | ||
6fc6879b | 316 | os_memset(&hdr, 0, sizeof(hdr)); |
1e145265 JM |
317 | if (hapd->driver && |
318 | os_strcmp(hapd->driver->name, "hostap") == 0) { | |
319 | /* | |
320 | * WLAN_FC_STYPE_NULLFUNC would be more appropriate, | |
321 | * but it is apparently not retried so TX Exc events | |
322 | * are not received for it. | |
323 | */ | |
324 | hdr.frame_control = | |
325 | IEEE80211_FC(WLAN_FC_TYPE_DATA, | |
326 | WLAN_FC_STYPE_DATA); | |
327 | } else { | |
328 | hdr.frame_control = | |
329 | IEEE80211_FC(WLAN_FC_TYPE_DATA, | |
330 | WLAN_FC_STYPE_NULLFUNC); | |
331 | } | |
332 | ||
6fc6879b JM |
333 | hdr.frame_control |= host_to_le16(WLAN_FC_FROMDS); |
334 | os_memcpy(hdr.IEEE80211_DA_FROMDS, sta->addr, ETH_ALEN); | |
335 | os_memcpy(hdr.IEEE80211_BSSID_FROMDS, hapd->own_addr, | |
336 | ETH_ALEN); | |
337 | os_memcpy(hdr.IEEE80211_SA_FROMDS, hapd->own_addr, ETH_ALEN); | |
338 | ||
cee7d66b | 339 | if (hostapd_drv_send_mlme(hapd, &hdr, sizeof(hdr)) < 0) |
6fc6879b JM |
340 | perror("ap_handle_timer: send"); |
341 | #endif /* CONFIG_NATIVE_WINDOWS */ | |
342 | } else if (sta->timeout_next != STA_REMOVE) { | |
343 | int deauth = sta->timeout_next == STA_DEAUTH; | |
344 | ||
345 | wpa_printf(MSG_DEBUG, "Sending %s info to STA " MACSTR, | |
346 | deauth ? "deauthentication" : "disassociation", | |
347 | MAC2STR(sta->addr)); | |
348 | ||
349 | if (deauth) { | |
bdee6fce JM |
350 | hapd->drv.sta_deauth(hapd, sta->addr, |
351 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
6fc6879b | 352 | } else { |
bdee6fce | 353 | hapd->drv.sta_disassoc( |
6fc6879b JM |
354 | hapd, sta->addr, |
355 | WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY); | |
356 | } | |
357 | } | |
358 | ||
359 | switch (sta->timeout_next) { | |
360 | case STA_NULLFUNC: | |
361 | sta->timeout_next = STA_DISASSOC; | |
362 | eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer, | |
363 | hapd, sta); | |
364 | break; | |
365 | case STA_DISASSOC: | |
366 | sta->flags &= ~WLAN_STA_ASSOC; | |
367 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 0); | |
368 | if (!sta->acct_terminate_cause) | |
369 | sta->acct_terminate_cause = | |
370 | RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT; | |
371 | accounting_sta_stop(hapd, sta); | |
372 | ieee802_1x_free_station(sta); | |
373 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
374 | HOSTAPD_LEVEL_INFO, "disassociated due to " | |
375 | "inactivity"); | |
376 | sta->timeout_next = STA_DEAUTH; | |
377 | eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer, | |
378 | hapd, sta); | |
379 | mlme_disassociate_indication( | |
380 | hapd, sta, WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY); | |
381 | break; | |
382 | case STA_DEAUTH: | |
383 | case STA_REMOVE: | |
384 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
385 | HOSTAPD_LEVEL_INFO, "deauthenticated due to " | |
386 | "inactivity"); | |
387 | if (!sta->acct_terminate_cause) | |
388 | sta->acct_terminate_cause = | |
389 | RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT; | |
390 | mlme_deauthenticate_indication( | |
391 | hapd, sta, | |
392 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
393 | ap_free_sta(hapd, sta); | |
394 | break; | |
395 | } | |
396 | } | |
397 | ||
398 | ||
399 | static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx) | |
400 | { | |
401 | struct hostapd_data *hapd = eloop_ctx; | |
402 | struct sta_info *sta = timeout_ctx; | |
403 | u8 addr[ETH_ALEN]; | |
404 | ||
405 | if (!(sta->flags & WLAN_STA_AUTH)) | |
406 | return; | |
407 | ||
408 | mlme_deauthenticate_indication(hapd, sta, | |
409 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
410 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
411 | HOSTAPD_LEVEL_INFO, "deauthenticated due to " | |
412 | "session timeout"); | |
413 | sta->acct_terminate_cause = | |
414 | RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT; | |
415 | os_memcpy(addr, sta->addr, ETH_ALEN); | |
416 | ap_free_sta(hapd, sta); | |
bdee6fce | 417 | hapd->drv.sta_deauth(hapd, addr, WLAN_REASON_PREV_AUTH_NOT_VALID); |
6fc6879b JM |
418 | } |
419 | ||
420 | ||
421 | void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta, | |
422 | u32 session_timeout) | |
423 | { | |
424 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
425 | HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d " | |
426 | "seconds", session_timeout); | |
427 | eloop_cancel_timeout(ap_handle_session_timer, hapd, sta); | |
428 | eloop_register_timeout(session_timeout, 0, ap_handle_session_timer, | |
429 | hapd, sta); | |
430 | } | |
431 | ||
432 | ||
433 | void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta) | |
434 | { | |
435 | eloop_cancel_timeout(ap_handle_session_timer, hapd, sta); | |
436 | } | |
437 | ||
438 | ||
439 | struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr) | |
440 | { | |
441 | struct sta_info *sta; | |
442 | ||
443 | sta = ap_get_sta(hapd, addr); | |
444 | if (sta) | |
445 | return sta; | |
446 | ||
447 | wpa_printf(MSG_DEBUG, " New STA"); | |
448 | if (hapd->num_sta >= hapd->conf->max_num_sta) { | |
449 | /* FIX: might try to remove some old STAs first? */ | |
450 | wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)", | |
451 | hapd->num_sta, hapd->conf->max_num_sta); | |
452 | return NULL; | |
453 | } | |
454 | ||
455 | sta = os_zalloc(sizeof(struct sta_info)); | |
456 | if (sta == NULL) { | |
457 | wpa_printf(MSG_ERROR, "malloc failed"); | |
458 | return NULL; | |
459 | } | |
5843e1c9 | 460 | sta->acct_interim_interval = hapd->conf->acct_interim_interval; |
6fc6879b JM |
461 | |
462 | /* initialize STA info data */ | |
463 | eloop_register_timeout(hapd->conf->ap_max_inactivity, 0, | |
464 | ap_handle_timer, hapd, sta); | |
465 | os_memcpy(sta->addr, addr, ETH_ALEN); | |
466 | sta->next = hapd->sta_list; | |
467 | hapd->sta_list = sta; | |
468 | hapd->num_sta++; | |
469 | ap_sta_hash_add(hapd, sta); | |
470 | sta->ssid = &hapd->conf->ssid; | |
53f3d6f3 | 471 | ap_sta_remove_in_other_bss(hapd, sta); |
6fc6879b JM |
472 | |
473 | return sta; | |
474 | } | |
475 | ||
476 | ||
477 | static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta) | |
478 | { | |
479 | ieee802_1x_notify_port_enabled(sta->eapol_sm, 0); | |
480 | ||
481 | wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver", | |
482 | MAC2STR(sta->addr)); | |
bdee6fce | 483 | if (hapd->drv.sta_remove(hapd, sta->addr) && |
6fc6879b JM |
484 | sta->flags & WLAN_STA_ASSOC) { |
485 | wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR | |
486 | " from kernel driver.", MAC2STR(sta->addr)); | |
487 | return -1; | |
488 | } | |
489 | return 0; | |
490 | } | |
491 | ||
492 | ||
53f3d6f3 FF |
493 | static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd, |
494 | struct sta_info *sta) | |
6fc6879b JM |
495 | { |
496 | struct hostapd_iface *iface = hapd->iface; | |
497 | size_t i; | |
498 | ||
499 | for (i = 0; i < iface->num_bss; i++) { | |
500 | struct hostapd_data *bss = iface->bss[i]; | |
501 | struct sta_info *sta2; | |
502 | /* bss should always be set during operation, but it may be | |
503 | * NULL during reconfiguration. Assume the STA is not | |
504 | * associated to another BSS in that case to avoid NULL pointer | |
505 | * dereferences. */ | |
506 | if (bss == hapd || bss == NULL) | |
507 | continue; | |
508 | sta2 = ap_get_sta(bss, sta->addr); | |
53f3d6f3 FF |
509 | if (!sta2) |
510 | continue; | |
6fc6879b | 511 | |
53f3d6f3 FF |
512 | ap_sta_disconnect(bss, sta2, sta2->addr, |
513 | WLAN_REASON_PREV_AUTH_NOT_VALID); | |
514 | } | |
6fc6879b JM |
515 | } |
516 | ||
517 | ||
518 | void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta, | |
519 | u16 reason) | |
520 | { | |
521 | wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR, | |
522 | hapd->conf->iface, MAC2STR(sta->addr)); | |
523 | sta->flags &= ~WLAN_STA_ASSOC; | |
53f3d6f3 | 524 | ap_sta_remove(hapd, sta); |
6fc6879b JM |
525 | sta->timeout_next = STA_DEAUTH; |
526 | eloop_cancel_timeout(ap_handle_timer, hapd, sta); | |
527 | eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0, | |
528 | ap_handle_timer, hapd, sta); | |
529 | accounting_sta_stop(hapd, sta); | |
530 | ieee802_1x_free_station(sta); | |
531 | ||
532 | mlme_disassociate_indication(hapd, sta, reason); | |
533 | } | |
534 | ||
535 | ||
536 | void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta, | |
537 | u16 reason) | |
538 | { | |
539 | wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR, | |
540 | hapd->conf->iface, MAC2STR(sta->addr)); | |
541 | sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC); | |
53f3d6f3 | 542 | ap_sta_remove(hapd, sta); |
6fc6879b JM |
543 | sta->timeout_next = STA_REMOVE; |
544 | eloop_cancel_timeout(ap_handle_timer, hapd, sta); | |
545 | eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0, | |
546 | ap_handle_timer, hapd, sta); | |
547 | accounting_sta_stop(hapd, sta); | |
548 | ieee802_1x_free_station(sta); | |
549 | ||
550 | mlme_deauthenticate_indication(hapd, sta, reason); | |
551 | } | |
552 | ||
553 | ||
554 | int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta, | |
555 | int old_vlanid) | |
556 | { | |
30b32314 | 557 | #ifndef CONFIG_NO_VLAN |
6fc6879b JM |
558 | const char *iface; |
559 | struct hostapd_vlan *vlan = NULL; | |
4254100d | 560 | int ret; |
6fc6879b JM |
561 | |
562 | /* | |
563 | * Do not proceed furthur if the vlan id remains same. We do not want | |
564 | * duplicate dynamic vlan entries. | |
565 | */ | |
566 | if (sta->vlan_id == old_vlanid) | |
567 | return 0; | |
568 | ||
569 | /* | |
570 | * During 1x reauth, if the vlan id changes, then remove the old id and | |
571 | * proceed furthur to add the new one. | |
572 | */ | |
573 | if (old_vlanid > 0) | |
574 | vlan_remove_dynamic(hapd, old_vlanid); | |
575 | ||
576 | iface = hapd->conf->iface; | |
577 | if (sta->ssid->vlan[0]) | |
578 | iface = sta->ssid->vlan; | |
579 | ||
580 | if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED) | |
581 | sta->vlan_id = 0; | |
582 | else if (sta->vlan_id > 0) { | |
583 | vlan = hapd->conf->vlan; | |
584 | while (vlan) { | |
585 | if (vlan->vlan_id == sta->vlan_id || | |
586 | vlan->vlan_id == VLAN_ID_WILDCARD) { | |
587 | iface = vlan->ifname; | |
588 | break; | |
589 | } | |
590 | vlan = vlan->next; | |
591 | } | |
592 | } | |
593 | ||
594 | if (sta->vlan_id > 0 && vlan == NULL) { | |
595 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
596 | HOSTAPD_LEVEL_DEBUG, "could not find VLAN for " | |
597 | "binding station to (vlan_id=%d)", | |
598 | sta->vlan_id); | |
599 | return -1; | |
600 | } else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) { | |
601 | vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id); | |
602 | if (vlan == NULL) { | |
603 | hostapd_logger(hapd, sta->addr, | |
604 | HOSTAPD_MODULE_IEEE80211, | |
605 | HOSTAPD_LEVEL_DEBUG, "could not add " | |
606 | "dynamic VLAN interface for vlan_id=%d", | |
607 | sta->vlan_id); | |
608 | return -1; | |
609 | } | |
610 | ||
611 | iface = vlan->ifname; | |
612 | if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) { | |
613 | hostapd_logger(hapd, sta->addr, | |
614 | HOSTAPD_MODULE_IEEE80211, | |
615 | HOSTAPD_LEVEL_DEBUG, "could not " | |
616 | "configure encryption for dynamic VLAN " | |
617 | "interface for vlan_id=%d", | |
618 | sta->vlan_id); | |
619 | } | |
620 | ||
621 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
622 | HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN " | |
623 | "interface '%s'", iface); | |
624 | } else if (vlan && vlan->vlan_id == sta->vlan_id) { | |
625 | if (sta->vlan_id > 0) { | |
626 | vlan->dynamic_vlan++; | |
627 | hostapd_logger(hapd, sta->addr, | |
628 | HOSTAPD_MODULE_IEEE80211, | |
629 | HOSTAPD_LEVEL_DEBUG, "updated existing " | |
630 | "dynamic VLAN interface '%s'", iface); | |
631 | } | |
632 | ||
633 | /* | |
634 | * Update encryption configuration for statically generated | |
635 | * VLAN interface. This is only used for static WEP | |
636 | * configuration for the case where hostapd did not yet know | |
637 | * which keys are to be used when the interface was added. | |
638 | */ | |
639 | if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) { | |
640 | hostapd_logger(hapd, sta->addr, | |
641 | HOSTAPD_MODULE_IEEE80211, | |
642 | HOSTAPD_LEVEL_DEBUG, "could not " | |
643 | "configure encryption for VLAN " | |
644 | "interface for vlan_id=%d", | |
645 | sta->vlan_id); | |
646 | } | |
647 | } | |
648 | ||
649 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
650 | HOSTAPD_LEVEL_DEBUG, "binding station to interface " | |
651 | "'%s'", iface); | |
652 | ||
653 | if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0) | |
654 | wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA"); | |
655 | ||
4254100d JM |
656 | ret = hapd->drv.set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id); |
657 | if (ret < 0) { | |
658 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, | |
659 | HOSTAPD_LEVEL_DEBUG, "could not bind the STA " | |
660 | "entry to vlan_id=%d", sta->vlan_id); | |
661 | } | |
662 | return ret; | |
30b32314 JM |
663 | #else /* CONFIG_NO_VLAN */ |
664 | return 0; | |
665 | #endif /* CONFIG_NO_VLAN */ | |
6fc6879b | 666 | } |
5d22a1d5 JM |
667 | |
668 | ||
669 | #ifdef CONFIG_IEEE80211W | |
670 | ||
45c94154 | 671 | int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta) |
5d22a1d5 | 672 | { |
45c94154 JM |
673 | u32 tu; |
674 | struct os_time now, passed; | |
675 | os_get_time(&now); | |
676 | os_time_sub(&now, &sta->sa_query_start, &passed); | |
677 | tu = (passed.sec * 1000000 + passed.usec) / 1024; | |
678 | if (hapd->conf->assoc_sa_query_max_timeout < tu) { | |
679 | hostapd_logger(hapd, sta->addr, | |
680 | HOSTAPD_MODULE_IEEE80211, | |
5d22a1d5 | 681 | HOSTAPD_LEVEL_DEBUG, |
93b76319 JM |
682 | "association SA Query timed out"); |
683 | sta->sa_query_timed_out = 1; | |
684 | os_free(sta->sa_query_trans_id); | |
685 | sta->sa_query_trans_id = NULL; | |
686 | sta->sa_query_count = 0; | |
45c94154 JM |
687 | eloop_cancel_timeout(ap_sa_query_timer, hapd, sta); |
688 | return 1; | |
5d22a1d5 JM |
689 | } |
690 | ||
45c94154 JM |
691 | return 0; |
692 | } | |
693 | ||
694 | ||
695 | static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx) | |
696 | { | |
697 | struct hostapd_data *hapd = eloop_ctx; | |
698 | struct sta_info *sta = timeout_ctx; | |
699 | unsigned int timeout, sec, usec; | |
700 | u8 *trans_id, *nbuf; | |
701 | ||
702 | if (sta->sa_query_count > 0 && | |
703 | ap_check_sa_query_timeout(hapd, sta)) | |
704 | return; | |
705 | ||
93b76319 JM |
706 | nbuf = os_realloc(sta->sa_query_trans_id, |
707 | (sta->sa_query_count + 1) * WLAN_SA_QUERY_TR_ID_LEN); | |
5d22a1d5 JM |
708 | if (nbuf == NULL) |
709 | return; | |
45c94154 JM |
710 | if (sta->sa_query_count == 0) { |
711 | /* Starting a new SA Query procedure */ | |
712 | os_get_time(&sta->sa_query_start); | |
713 | } | |
93b76319 JM |
714 | trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN; |
715 | sta->sa_query_trans_id = nbuf; | |
716 | sta->sa_query_count++; | |
5d22a1d5 | 717 | |
93b76319 | 718 | os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN); |
5d22a1d5 | 719 | |
45c94154 JM |
720 | timeout = hapd->conf->assoc_sa_query_retry_timeout; |
721 | sec = ((timeout / 1000) * 1024) / 1000; | |
722 | usec = (timeout % 1000) * 1024; | |
723 | eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta); | |
724 | ||
5d22a1d5 JM |
725 | hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211, |
726 | HOSTAPD_LEVEL_DEBUG, | |
93b76319 | 727 | "association SA Query attempt %d", sta->sa_query_count); |
5d22a1d5 | 728 | |
fe6bdb77 | 729 | #ifdef NEED_AP_MLME |
93b76319 | 730 | ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id); |
fe6bdb77 | 731 | #endif /* NEED_AP_MLME */ |
5d22a1d5 JM |
732 | } |
733 | ||
734 | ||
93b76319 | 735 | void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta) |
5d22a1d5 | 736 | { |
93b76319 | 737 | ap_sa_query_timer(hapd, sta); |
5d22a1d5 JM |
738 | } |
739 | ||
740 | ||
93b76319 | 741 | void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta) |
5d22a1d5 | 742 | { |
93b76319 JM |
743 | eloop_cancel_timeout(ap_sa_query_timer, hapd, sta); |
744 | os_free(sta->sa_query_trans_id); | |
745 | sta->sa_query_trans_id = NULL; | |
746 | sta->sa_query_count = 0; | |
5d22a1d5 JM |
747 | } |
748 | ||
749 | #endif /* CONFIG_IEEE80211W */ | |
45cefa0b JM |
750 | |
751 | ||
752 | void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta, | |
753 | const u8 *addr, u16 reason) | |
754 | { | |
755 | ||
756 | if (sta == NULL && addr) | |
757 | sta = ap_get_sta(hapd, addr); | |
758 | ||
759 | if (addr) | |
bdee6fce | 760 | hapd->drv.sta_deauth(hapd, addr, reason); |
45cefa0b JM |
761 | |
762 | if (sta == NULL) | |
763 | return; | |
764 | sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_AUTHORIZED); | |
765 | eloop_cancel_timeout(ap_handle_timer, hapd, sta); | |
766 | eloop_register_timeout(0, 0, ap_handle_timer, hapd, sta); | |
767 | sta->timeout_next = STA_REMOVE; | |
768 | } |