• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * hostapd / Station table
3  * Copyright (c) 2002-2017, 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 "common/dpp.h"
17 #ifndef EXT_CODE_CROP
18 #include "radius/radius.h"
19 #include "radius/radius_client.h"
20 #endif /* EXT_CODE_CROP */
21 #include "p2p/p2p.h"
22 #include "fst/fst.h"
23 #include "crypto/crypto.h"
24 #include "hostapd.h"
25 #include "accounting.h"
26 #include "ieee802_1x.h"
27 #include "ieee802_11.h"
28 #ifndef EXT_CODE_CROP
29 #include "ieee802_11_auth.h"
30 #endif /* EXT_CODE_CROP */
31 #include "wpa_auth.h"
32 #include "preauth_auth.h"
33 #include "ap_config.h"
34 #include "beacon.h"
35 #include "ap_mlme.h"
36 #include "vlan_init.h"
37 #include "p2p_hostapd.h"
38 #include "ap_drv_ops.h"
39 #include "gas_serv.h"
40 #include "wnm_ap.h"
41 #include "mbo_ap.h"
42 #include "ndisc_snoop.h"
43 #include "sta_info.h"
44 #ifndef LOS_CONFIG_NO_VLAN
45 #include "vlan.h"
46 #endif
47 #include "wifi_api.h"
48 #include "wps_hostapd.h"
49 #ifdef LOS_CONFIG_WPS_AP
50 #include "wps/wps_i.h"
51 #endif
52 #ifdef LOS_CONFIG_MESH
53 #include "soc_mesh.h"
54 #endif /* LOS_CONFIG_MESH */
55 #include "driver_soc_at.h"
56 #include "wpa_supplicant_if.h"
57 
58 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
59 				       struct sta_info *sta);
60 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
61 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
62 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
63 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
64 #ifdef CONFIG_IEEE80211W_AP
65 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
66 #endif /* CONFIG_IEEE80211W_AP */
67 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
68 static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx);
69 
ap_for_each_sta(struct hostapd_data * hapd,int (* cb)(struct hostapd_data * hapd,struct sta_info * sta,void * ctx),void * ctx)70 int ap_for_each_sta(struct hostapd_data *hapd,
71 		    int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
72 			      void *ctx),
73 		    void *ctx)
74 {
75 	struct sta_info *sta;
76 
77 	for (sta = hapd->sta_list; sta; sta = sta->next) {
78 		if (cb(hapd, sta, ctx))
79 			return 1;
80 	}
81 
82 	return 0;
83 }
84 
85 
ap_get_sta(struct hostapd_data * hapd,const u8 * sta)86 struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
87 {
88 	struct sta_info *s;
89 
90 #ifndef EXT_CODE_CROP
91 	s = hapd->sta_hash[STA_HASH(sta)];
92 	while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
93 		s = s->hnext;
94 	return s;
95 #else
96 	for (s = hapd->sta_list; s; s = s->next) {
97 		if (os_memcmp(s->addr, sta, ETH_ALEN) == 0)
98 			return s;
99 }
100 	return NULL;
101 #endif /* EXT_CODE_CROP */
102 }
103 
104 #ifdef CONFIG_P2P
ap_get_sta_p2p(struct hostapd_data * hapd,const u8 * addr)105 struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
106 {
107 	struct sta_info *sta;
108 
109 	for (sta = hapd->sta_list; sta; sta = sta->next) {
110 		const u8 *p2p_dev_addr;
111 
112 		if (sta->p2p_ie == NULL)
113 			continue;
114 
115 		p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
116 		if (p2p_dev_addr == NULL)
117 			continue;
118 
119 		if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
120 			return sta;
121 	}
122 
123 	return NULL;
124 }
125 #endif /* CONFIG_P2P */
126 
127 
ap_sta_list_del(struct hostapd_data * hapd,struct sta_info * sta)128 static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
129 {
130 	struct sta_info *tmp;
131 
132 	if (hapd->sta_list == sta) {
133 		hapd->sta_list = sta->next;
134 		return;
135 	}
136 
137 	tmp = hapd->sta_list;
138 	while (tmp != NULL && tmp->next != sta)
139 		tmp = tmp->next;
140 	if (tmp == NULL) {
141 		wpa_warning_log4(MSG_DEBUG, "Could not remove STA " "%02x:xx:xx:%02x:%02x:%02x" " from "
142 			   "list.", (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
143 	} else
144 		tmp->next = sta->next;
145 }
146 
147 #ifndef EXT_CODE_CROP
ap_sta_hash_add(struct hostapd_data * hapd,struct sta_info * sta)148 void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
149 {
150 	sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
151 	hapd->sta_hash[STA_HASH(sta->addr)] = sta;
152 }
153 
154 
ap_sta_hash_del(struct hostapd_data * hapd,struct sta_info * sta)155 static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
156 {
157 	struct sta_info *s;
158 
159 	s = hapd->sta_hash[STA_HASH(sta->addr)];
160 	if (s == NULL) return;
161 	if (os_memcmp(s->addr, sta->addr, 6) == 0) {
162 		hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
163 		return;
164 	}
165 
166 	while (s->hnext != NULL &&
167 	       os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
168 		s = s->hnext;
169 	if (s->hnext != NULL)
170 		s->hnext = s->hnext->hnext;
171 	else
172 		wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
173 			   " from hash table", MAC2STR(sta->addr));
174 }
175 
176 
ap_sta_ip6addr_del(struct hostapd_data * hapd,struct sta_info * sta)177 void ap_sta_ip6addr_del(struct hostapd_data *hapd, struct sta_info *sta)
178 {
179 	sta_ip6addr_del(hapd, sta);
180 }
181 #endif /* EXT_CODE_CROP */
182 
183 #ifdef CONFIG_PASN
184 
ap_free_sta_pasn(struct hostapd_data * hapd,struct sta_info * sta)185 void ap_free_sta_pasn(struct hostapd_data *hapd, struct sta_info *sta)
186 {
187 	if (sta->pasn) {
188 		wpa_printf(MSG_DEBUG, "PASN: Free PASN context: " MACSTR,
189 			   MAC2STR(sta->addr));
190 
191 		if (sta->pasn->ecdh)
192 			crypto_ecdh_deinit(sta->pasn->ecdh);
193 
194 		wpabuf_free(sta->pasn->secret);
195 		sta->pasn->secret = NULL;
196 
197 #ifdef CONFIG_SAE
198 		sae_clear_data(&sta->pasn->sae);
199 #endif /* CONFIG_SAE */
200 
201 #ifdef CONFIG_FILS
202 		/* In practice this pointer should be NULL */
203 		wpabuf_free(sta->pasn->fils.erp_resp);
204 		sta->pasn->fils.erp_resp = NULL;
205 #endif /* CONFIG_FILS */
206 
207 		bin_clear_free(sta->pasn, sizeof(*sta->pasn));
208 		sta->pasn = NULL;
209 	}
210 }
211 
212 #endif /* CONFIG_PASN */
213 
ap_free_sta(struct hostapd_data * hapd,struct sta_info * sta)214 void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
215 {
216 	int set_beacon = 0;
217 
218 	accounting_sta_stop(hapd, sta);
219 
220 	/* just in case */
221 	ap_sta_set_authorized(hapd, sta, 0);
222 #ifndef EXT_CODE_CROP
223 	hostapd_set_sta_flags(hapd, sta);
224 #endif /* EXT_CODE_CROP */
225 #ifndef EXT_CODE_CROP
226 	if (sta->flags & (WLAN_STA_WDS | WLAN_STA_MULTI_AP))
227 		hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
228 #endif /* EXT_CODE_CROP */
229 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
230 	if (sta->ipaddr)
231 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
232 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
233 #ifndef EXT_CODE_CROP
234 	ap_sta_ip6addr_del(hapd, sta);
235 
236 	if (!hapd->iface->driver_ap_teardown &&
237 	    !(sta->flags & WLAN_STA_PREAUTH)) {
238 #endif /* EXT_CODE_CROP */
239 		hostapd_drv_sta_remove(hapd, sta->addr);
240 		sta->added_unassoc = 0;
241 #ifndef EXT_CODE_CROP
242 	}
243 
244 	ap_sta_hash_del(hapd, sta);
245 #endif /* EXT_CODE_CROP */
246 	ap_sta_list_del(hapd, sta);
247 
248 #ifndef EXT_CODE_CROP
249 	if (sta->aid > 0)
250 		hapd->sta_aid[(sta->aid - 1) / 32] &=
251 			~BIT((sta->aid - 1) % 32);
252 #else
253 	if (sta->aid > 0)
254 		hapd->sta_aid &= ~(BIT(sta->aid - 1));
255 #endif /* EXT_CODE_CROP */
256 
257 	hapd->num_sta--;
258 #ifndef EXT_CODE_CROP
259 	if (sta->nonerp_set) {
260 		sta->nonerp_set = 0;
261 		hapd->iface->num_sta_non_erp--;
262 		if (hapd->iface->num_sta_non_erp == 0)
263 			set_beacon++;
264 	}
265 
266 	if (sta->no_short_slot_time_set) {
267 		sta->no_short_slot_time_set = 0;
268 		hapd->iface->num_sta_no_short_slot_time--;
269 		if (hapd->iface->current_mode &&
270 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
271 		    && hapd->iface->num_sta_no_short_slot_time == 0)
272 			set_beacon++;
273 	}
274 
275 	if (sta->no_short_preamble_set) {
276 		sta->no_short_preamble_set = 0;
277 		hapd->iface->num_sta_no_short_preamble--;
278 		if (hapd->iface->current_mode &&
279 		    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
280 		    && hapd->iface->num_sta_no_short_preamble == 0)
281 			set_beacon++;
282 	}
283 
284 	if (sta->no_ht_gf_set) {
285 		sta->no_ht_gf_set = 0;
286 		hapd->iface->num_sta_ht_no_gf--;
287 	}
288 
289 	if (sta->no_ht_set) {
290 		sta->no_ht_set = 0;
291 		hapd->iface->num_sta_no_ht--;
292 	}
293 
294 	if (sta->ht_20mhz_set) {
295 		sta->ht_20mhz_set = 0;
296 		hapd->iface->num_sta_ht_20mhz--;
297 	}
298 #endif /* EXT_CODE_CROP */
299 
300 #ifdef CONFIG_TAXONOMY
301 	wpabuf_free(sta->probe_ie_taxonomy);
302 	sta->probe_ie_taxonomy = NULL;
303 	wpabuf_free(sta->assoc_ie_taxonomy);
304 	sta->assoc_ie_taxonomy = NULL;
305 #endif /* CONFIG_TAXONOMY */
306 
307 #ifndef EXT_CODE_CROP
308 	ht40_intolerant_remove(hapd->iface, sta);
309 #endif /* EXT_CODE_CROP */
310 
311 #ifdef CONFIG_P2P
312 	if (sta->no_p2p_set) {
313 		sta->no_p2p_set = 0;
314 		hapd->num_sta_no_p2p--;
315 		if (hapd->num_sta_no_p2p == 0)
316 			hostapd_p2p_non_p2p_sta_disconnected(hapd);
317 	}
318 #endif /* CONFIG_P2P */
319 #ifndef EXT_CODE_CROP
320 #ifdef NEED_AP_MLME
321 	if (hostapd_ht_operation_update(hapd->iface) > 0)
322 		set_beacon++;
323 #endif /* NEED_AP_MLME */
324 #endif /* EXT_CODE_CROP */
325 
326 #ifdef CONFIG_MESH
327 	if (hapd->mesh_sta_free_cb)
328 		hapd->mesh_sta_free_cb(hapd, sta);
329 #endif /* CONFIG_MESH */
330 
331 	if (set_beacon)
332 		ieee802_11_set_beacons(hapd->iface);
333 
334 	wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
335 		   __func__, MAC2STR(sta->addr));
336 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
337 	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
338 	eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
339 	ap_sta_clear_disconnect_timeouts(hapd, sta);
340 #if (defined(LOS_CONFIG_HOSTAPD_MGMT) && defined(CONFIG_SAE))
341 	sae_clear_retransmit_timer(hapd, sta);
342 #endif /* LOS_CONFIG_HOSTAPD_MGMT && CONFIG_SAE */
343 
344 	ieee802_1x_free_station(hapd, sta);
345 	wpa_auth_sta_deinit(sta->wpa_sm);
346 	rsn_preauth_free_station(hapd, sta);
347 #ifndef CONFIG_NO_RADIUS
348 	if (hapd->radius)
349 		radius_client_flush_auth(hapd->radius, sta->addr);
350 #endif /* CONFIG_NO_RADIUS */
351 
352 #ifndef CONFIG_NO_VLAN
353 	/*
354 	 * sta->wpa_sm->group needs to be released before so that
355 	 * vlan_remove_dynamic() can check that no stations are left on the
356 	 * AP_VLAN netdev.
357 	 */
358 	if (sta->vlan_id)
359 		vlan_remove_dynamic(hapd, sta->vlan_id);
360 	if (sta->vlan_id_bound) {
361 		/*
362 		 * Need to remove the STA entry before potentially removing the
363 		 * VLAN.
364 		 */
365 		if (hapd->iface->driver_ap_teardown &&
366 		    !(sta->flags & WLAN_STA_PREAUTH)) {
367 			hostapd_drv_sta_remove(hapd, sta->addr);
368 			sta->added_unassoc = 0;
369 		}
370 		vlan_remove_dynamic(hapd, sta->vlan_id_bound);
371 	}
372 #endif /* CONFIG_NO_VLAN */
373 
374 	os_free(sta->challenge);
375 
376 #ifdef CONFIG_IEEE80211W_AP
377 	os_free(sta->sa_query_trans_id);
378 	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
379 #endif /* CONFIG_IEEE80211W_AP */
380 
381 #ifdef CONFIG_P2P
382 	p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
383 #endif /* CONFIG_P2P */
384 #ifndef EXT_CODE_CROP
385 #ifdef CONFIG_INTERWORKING
386 	if (sta->gas_dialog) {
387 		int i;
388 		for (i = 0; i < GAS_DIALOG_MAX; i++)
389 			gas_serv_dialog_clear(&sta->gas_dialog[i]);
390 		os_free(sta->gas_dialog);
391 	}
392 #endif /* CONFIG_INTERWORKING */
393 #endif /* EXT_CODE_CROP */
394 #ifdef LOS_CONFIG_WPS_AP
395 	if (hapd->wps && hapd->wps->ap && hapd->wps->registrar) {
396 		wps_remove_device(hapd->wps->registrar, sta->addr);
397 	}
398 #endif
399 	wpabuf_free(sta->wps_ie);
400 #ifdef CONFIG_P2P
401 	wpabuf_free(sta->p2p_ie);
402 #endif /* CONFIG_P2P */
403 #ifndef EXT_CODE_CROP
404 	wpabuf_free(sta->hs20_ie);
405 	wpabuf_free(sta->roaming_consortium);
406 #endif /* EXT_CODE_CROP */
407 #ifdef CONFIG_FST
408 	wpabuf_free(sta->mb_ies);
409 #endif /* CONFIG_FST */
410 
411 #ifndef EXT_CODE_CROP
412 	os_free(sta->ht_capabilities);
413 	os_free(sta->vht_capabilities);
414 	os_free(sta->vht_operation);
415 	os_free(sta->he_capab);
416 	os_free(sta->he_6ghz_capab);
417 	hostapd_free_psk_list(sta->psk);
418 #endif /* EXT_CODE_CROP */
419 	os_free(sta->identity);
420 #ifndef EXT_CODE_CROP
421 	os_free(sta->radius_cui);
422 	os_free(sta->remediation_url);
423 	os_free(sta->t_c_url);
424 	wpabuf_free(sta->hs20_deauth_req);
425 	os_free(sta->hs20_session_info_url);
426 #endif /* EXT_CODE_CROP */
427 
428 #ifdef CONFIG_SAE
429 	sae_clear_data(sta->sae);
430 	os_free(sta->sae);
431 #endif /* CONFIG_SAE */
432 
433 	mbo_ap_sta_free(sta);
434 #ifndef EXT_CODE_CROP
435 	os_free(sta->supp_op_classes);
436 
437 #ifdef CONFIG_FILS
438 	os_free(sta->fils_pending_assoc_req);
439 	wpabuf_free(sta->fils_hlp_resp);
440 	wpabuf_free(sta->hlp_dhcp_discover);
441 	eloop_cancel_timeout(fils_hlp_timeout, hapd, sta);
442 #ifdef CONFIG_FILS_SK_PFS
443 	crypto_ecdh_deinit(sta->fils_ecdh);
444 	wpabuf_clear_free(sta->fils_dh_ss);
445 	wpabuf_free(sta->fils_g_sta);
446 #endif /* CONFIG_FILS_SK_PFS */
447 #endif /* CONFIG_FILS */
448 
449 #ifdef CONFIG_OWE
450 	bin_clear_free(sta->owe_pmk, sta->owe_pmk_len);
451 	crypto_ecdh_deinit(sta->owe_ecdh);
452 #endif /* CONFIG_OWE */
453 #endif /* EXT_CODE_CROP */
454 #ifdef CONFIG_DPP2
455 	dpp_pfs_free(sta->dpp_pfs);
456 	sta->dpp_pfs = NULL;
457 #endif /* CONFIG_DPP2 */
458 
459 	os_free(sta->ext_capability);
460 
461 #ifdef CONFIG_WNM_AP
462 	eloop_cancel_timeout(ap_sta_reset_steer_flag_timer, hapd, sta);
463 #endif /* CONFIG_WNM_AP */
464 #ifndef EXT_CODE_CROP
465 #ifdef CONFIG_PASN
466 	ap_free_sta_pasn(hapd, sta);
467 #endif /* CONFIG_PASN */
468 
469 	os_free(sta->ifname_wds);
470 #endif /* EXT_CODE_CROP */
471 #ifdef CONFIG_TESTING_OPTIONS
472 	os_free(sta->sae_postponed_commit);
473 #endif /* CONFIG_TESTING_OPTIONS */
474 
475 	os_free(sta);
476 }
477 
478 
hostapd_free_stas(struct hostapd_data * hapd)479 void hostapd_free_stas(struct hostapd_data *hapd)
480 {
481 	struct sta_info *sta, *prev;
482 #ifdef LOS_CONFIG_P2P
483 	const u8 *p2p_dev_addr = NULL;
484 #endif /* LOS_CONFIG_P2P */
485 
486 	sta = hapd->sta_list;
487 
488 	while (sta) {
489 		prev = sta;
490 		if (sta->flags & WLAN_STA_AUTH) {
491 			mlme_deauthenticate_indication(
492 				hapd, sta, WLAN_REASON_UNSPECIFIED);
493 #ifdef LOS_CONFIG_P2P
494 			if (sta->p2p_ie)
495 				p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
496 			if (p2p_dev_addr != NULL)
497 				(void)hostapd_drv_sta_deauth(hapd, sta->addr, WLAN_REASON_DEAUTH_LEAVING);
498 #endif /* LOS_CONFIG_P2P */
499 		}
500 		sta = sta->next;
501 		wpa_warning_log4(MSG_DEBUG, "Removing station " "%02x:xx:xx:%02x:%02x:%02x",
502 			   (prev->addr)[0], (prev->addr)[3], (prev->addr)[4], (prev->addr)[5]);
503 		ap_free_sta(hapd, prev);
504 	}
505 }
506 
507 
508 /**
509  * ap_handle_timer - Per STA timer handler
510  * @eloop_ctx: struct hostapd_data *
511  * @timeout_ctx: struct sta_info *
512  *
513  * This function is called to check station activity and to remove inactive
514  * stations.
515  */
ap_handle_timer(void * eloop_ctx,void * timeout_ctx)516 void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
517 {
518 	struct hostapd_data *hapd = eloop_ctx;
519 	struct sta_info *sta = timeout_ctx;
520 	unsigned long next_time = 0;
521 	int reason;
522 
523 	wpa_warning_buf(MSG_DEBUG, "%s: ap_handle_timer: ",
524 		   hapd->conf->iface, strlen(hapd->conf->iface));
525 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
526 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
527 	wpa_warning_log2(MSG_DEBUG, " flags=0x%x timeout_next=%d",
528 		   sta->flags, sta->timeout_next);
529 	if (sta->timeout_next == STA_REMOVE) {
530 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
531 			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
532 			       "local deauth request");
533 		ap_free_sta(hapd, sta);
534 		return;
535 	}
536 
537 	if ((sta->flags & WLAN_STA_ASSOC) &&
538 	    (sta->timeout_next == STA_NULLFUNC ||
539 	     sta->timeout_next == STA_DISASSOC)) {
540 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
541 		int inactive_sec;
542 		/*
543 		 * Add random value to timeout so that we don't end up bouncing
544 		 * all stations at the same time if we have lots of associated
545 		 * stations that are idle (but keep re-associating).
546 		 */
547 		int fuzz = os_random() % 20;
548 		inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
549 		if (inactive_sec == -1) {
550 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
551 				"Check inactivity: Could not "
552 				"get station info from kernel driver for "
553 				MACSTR, MAC2STR(sta->addr));
554 			/*
555 			 * The driver may not support this functionality.
556 			 * Anyway, try again after the next inactivity timeout,
557 			 * but do not disconnect the station now.
558 			 */
559 			next_time = hapd->conf->ap_max_inactivity + fuzz;
560 		} else if (inactive_sec == -ENOENT) {
561 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
562 				"Station " MACSTR " has lost its driver entry",
563 				MAC2STR(sta->addr));
564 
565 			/* Avoid sending client probe on removed client */
566 			sta->timeout_next = STA_DISASSOC;
567 			goto skip_poll;
568 		} else if (inactive_sec < hapd->conf->ap_max_inactivity) {
569 			/* station activity detected; reset timeout state */
570 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
571 				"Station " MACSTR " has been active %is ago",
572 				MAC2STR(sta->addr), inactive_sec);
573 			sta->timeout_next = STA_NULLFUNC;
574 			next_time = hapd->conf->ap_max_inactivity + fuzz -
575 				inactive_sec;
576 		} else {
577 			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
578 				"Station " MACSTR " has been "
579 				"inactive too long: %d sec, max allowed: %d",
580 				MAC2STR(sta->addr), inactive_sec,
581 				hapd->conf->ap_max_inactivity);
582 
583 			if (hapd->conf->skip_inactivity_poll)
584 				sta->timeout_next = STA_DISASSOC;
585 		}
586 #else
587 		int fuzz = (int)(os_random() % 20);
588 		sta->timeout_next = STA_NULLFUNC;
589 		next_time = (unsigned long)(hapd->conf->ap_max_inactivity + fuzz);
590 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
591 	}
592 
593 #ifndef EXT_CODE_CROP
594 	if ((sta->flags & WLAN_STA_ASSOC) &&
595 	    sta->timeout_next == STA_DISASSOC &&
596 	    !(sta->flags & WLAN_STA_PENDING_POLL) &&
597 	    !hapd->conf->skip_inactivity_poll) {
598 #else
599 	if ((sta->flags & WLAN_STA_ASSOC) &&
600 		sta->timeout_next == STA_DISASSOC &&
601 		!(sta->flags & WLAN_STA_PENDING_POLL)) {
602 #endif /* EXT_CODE_CROP */
603 		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
604 			" has ACKed data poll", MAC2STR(sta->addr));
605 		/* data nullfunc frame poll did not produce TX errors; assume
606 		 * station ACKed it */
607 		sta->timeout_next = STA_NULLFUNC;
608 		next_time = hapd->conf->ap_max_inactivity;
609 	}
610 
611 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
612 skip_poll:
613 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
614 	if (next_time) {
615 		wpa_warning_log4(MSG_DEBUG, "ap_handle_timer: register ap_handle_timer timeout "
616 			   "for " "%02x:xx:xx:%02x:%02x:%02x",
617 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
618 		wpa_warning_log1(MSG_DEBUG, " (%lu seconds)",
619 				next_time);
620 		eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
621 				       sta);
622 		return;
623 	}
624 
625 	if (sta->timeout_next == STA_NULLFUNC &&
626 	    (sta->flags & WLAN_STA_ASSOC)) {
627 		wpa_warning_log0(MSG_DEBUG, "  Polling STA");
628 		sta->flags |= WLAN_STA_PENDING_POLL;
629 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
630 		hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
631 					sta->flags & WLAN_STA_WMM);
632 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
633 	}
634 #ifndef EXT_CODE_CROP
635 	else if (sta->timeout_next != STA_REMOVE) {
636 		int deauth = sta->timeout_next == STA_DEAUTH;
637 
638 		if (!deauth && !(sta->flags & WLAN_STA_ASSOC)) {
639 			/* Cannot disassociate not-associated STA, so move
640 			 * directly to deauthentication. */
641 			sta->timeout_next = STA_DEAUTH;
642 			deauth = 1;
643 		}
644 
645 		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
646 			"Timeout, sending %s info to STA " MACSTR,
647 			deauth ? "deauthentication" : "disassociation",
648 			MAC2STR(sta->addr));
649 
650 		if (deauth) {
651 			hostapd_drv_sta_deauth(
652 				hapd, sta->addr,
653 				WLAN_REASON_PREV_AUTH_NOT_VALID);
654 		} else {
655 			reason = (sta->timeout_next == STA_DISASSOC) ?
656 				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
657 				WLAN_REASON_PREV_AUTH_NOT_VALID;
658 
659 			hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
660 		}
661 	}
662 #endif /* EXT_CODE_CROP */
663 	switch (sta->timeout_next) {
664 	case STA_NULLFUNC:
665 		sta->timeout_next = STA_DISASSOC;
666 		wpa_warning_log4(MSG_DEBUG, "ap_handle_timer: register ap_handle_timer timeout "
667 			   "for " "%02x:xx:xx:%02x:%02x:%02x",
668 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
669 		wpa_warning_log1(MSG_DEBUG, " (%d seconds - AP_DISASSOC_DELAY)",
670 			   AP_DISASSOC_DELAY);
671 		eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
672 				       hapd, sta);
673 		break;
674 	case STA_DISASSOC:
675 	case STA_DISASSOC_FROM_CLI:
676 		ap_sta_set_authorized(hapd, sta, 0);
677 		sta->flags &= ~WLAN_STA_ASSOC;
678 #ifndef EXT_CODE_CROP
679 		hostapd_set_sta_flags(hapd, sta);
680 #endif /* EXT_CODE_CROP */
681 		ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
682 #ifndef EXT_CODE_CROP
683 		if (!sta->acct_terminate_cause)
684 			sta->acct_terminate_cause =
685 				RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
686 #endif /* EXT_CODE_CROP */
687 		accounting_sta_stop(hapd, sta);
688 		ieee802_1x_free_station(hapd, sta);
689 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
690 			       HOSTAPD_LEVEL_INFO, "disassociated due to "
691 			       "inactivity");
692 		reason = (sta->timeout_next == STA_DISASSOC) ?
693 			WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
694 			WLAN_REASON_PREV_AUTH_NOT_VALID;
695 		sta->timeout_next = STA_DEAUTH;
696 		wpa_warning_log4(MSG_DEBUG, "ap_handle_timer: register ap_handle_timer timeout "
697 			   "for " "%02x:xx:xx:%02x:%02x:%02x",
698 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
699 		wpa_warning_log1(MSG_DEBUG, " (%d seconds - AP_DEAUTH_DELAY)", AP_DEAUTH_DELAY);
700 		(void)eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
701 				       hapd, sta);
702 		mlme_disassociate_indication(hapd, sta, reason);
703 		break;
704 	case STA_DEAUTH:
705 	case STA_REMOVE:
706 #ifndef EXT_CODE_CROP
707 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
708 			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
709 			       "inactivity (timer DEAUTH/REMOVE)");
710 		if (!sta->acct_terminate_cause)
711 			sta->acct_terminate_cause =
712 				RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
713 #endif /* EXT_CODE_CROP */
714 		mlme_deauthenticate_indication(
715 			hapd, sta,
716 			WLAN_REASON_PREV_AUTH_NOT_VALID);
717 		ap_free_sta(hapd, sta);
718 		break;
719 	}
720 }
721 
722 
723 static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
724 {
725 	struct hostapd_data *hapd = eloop_ctx;
726 	struct sta_info *sta = timeout_ctx;
727 
728 	wpa_printf(MSG_DEBUG, "%s: Session timer for STA " MACSTR,
729 		   hapd->conf->iface, MAC2STR(sta->addr));
730 	if (!(sta->flags & (WLAN_STA_AUTH | WLAN_STA_ASSOC |
731 			    WLAN_STA_AUTHORIZED))) {
732 		if (sta->flags & WLAN_STA_GAS) {
733 			wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
734 				   "entry " MACSTR, MAC2STR(sta->addr));
735 			ap_free_sta(hapd, sta);
736 		}
737 		return;
738 	}
739 #ifndef EXT_CODE_CROP
740 	hostapd_drv_sta_deauth(hapd, sta->addr,
741 			       WLAN_REASON_PREV_AUTH_NOT_VALID);
742 #endif /* EXT_CODE_CROP */
743 	mlme_deauthenticate_indication(hapd, sta,
744 				       WLAN_REASON_PREV_AUTH_NOT_VALID);
745 #ifndef EXT_CODE_CROP
746 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
747 		       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
748 		       "session timeout");
749 	sta->acct_terminate_cause =
750 		RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
751 #endif /* EXT_CODE_CROP */
752 	ap_free_sta(hapd, sta);
753 }
754 
755 
756 void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
757 			      u32 session_timeout)
758 {
759 	if (eloop_replenish_timeout(session_timeout, 0,
760 				    ap_handle_session_timer, hapd, sta) == 1) {
761 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
762 			       HOSTAPD_LEVEL_DEBUG, "setting session timeout "
763 			       "to %d seconds", session_timeout);
764 	}
765 }
766 
767 
768 void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
769 			    u32 session_timeout)
770 {
771 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
772 		       HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
773 		       "seconds", session_timeout);
774 	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
775 	eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
776 			       hapd, sta);
777 }
778 
779 
780 void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
781 {
782 	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
783 }
784 
785 
786 static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
787 {
788 #ifdef CONFIG_WNM_AP
789 	struct hostapd_data *hapd = eloop_ctx;
790 	struct sta_info *sta = timeout_ctx;
791 
792 	wpa_printf(MSG_DEBUG, "%s: WNM: Session warning time reached for "
793 		   MACSTR, hapd->conf->iface, MAC2STR(sta->addr));
794 	if (sta->hs20_session_info_url == NULL)
795 		return;
796 
797 	wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
798 				       sta->hs20_disassoc_timer);
799 #endif /* CONFIG_WNM_AP */
800 }
801 
802 
803 void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
804 				    struct sta_info *sta, int warning_time)
805 {
806 	eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
807 	eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
808 			       hapd, sta);
809 }
810 
811 
812 struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
813 {
814 	struct sta_info *sta;
815 #ifndef EXT_CODE_CROP
816 	int i;
817 #endif /* EXT_CODE_CROP */
818 
819 	sta = ap_get_sta(hapd, addr);
820 	if (sta)
821 		return sta;
822 
823 	wpa_warning_log0(MSG_DEBUG, "  New STA");
824 	if (hapd->num_sta >= hapd->conf->max_num_sta) {
825 		/* FIX: might try to remove some old STAs first? */
826 		wpa_warning_log2(MSG_DEBUG, "no more room for new STAs (%d/%d)",
827 			   hapd->num_sta, hapd->conf->max_num_sta);
828 		return NULL;
829 	}
830 
831 	sta = os_zalloc(sizeof(struct sta_info));
832 	if (sta == NULL) {
833 		wpa_printf(MSG_ERROR, "malloc failed");
834 		return NULL;
835 	}
836 #ifndef CONFIG_PRINT_NOUSE
837 	sta->acct_interim_interval = hapd->conf->acct_interim_interval;
838 #endif /* CONFIG_PRINT_NOUSE */
839 	if (accounting_sta_get_id(hapd, sta) < 0) {
840 		os_free(sta);
841 		return NULL;
842 	}
843 
844 #ifndef EXT_CODE_CROP
845 	for (i = 0; i < WLAN_SUPP_RATES_MAX; i++) {
846 		if (!hapd->iface->basic_rates)
847 			break;
848 		if (hapd->iface->basic_rates[i] < 0)
849 			break;
850 		sta->supported_rates[i] = hapd->iface->basic_rates[i] / 5;
851 	}
852 	sta->supported_rates_len = i;
853 #endif /* EXT_CODE_CROP */
854 
855 	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
856 		wpa_warning_log4(MSG_DEBUG, "ap_sta_add: register ap_handle_timer timeout "
857 			   "for " "%02x:xx:xx:%02x:%02x:%02x",
858 			   addr[0], addr[3], addr[4], addr[5]);
859 		wpa_warning_log1(MSG_DEBUG," (%d seconds - ap_max_inactivity)",
860 			   hapd->conf->ap_max_inactivity);
861 		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
862 				       ap_handle_timer, hapd, sta);
863 	}
864 
865 	/* initialize STA info data */
866 	os_memcpy(sta->addr, addr, ETH_ALEN);
867 	sta->next = hapd->sta_list;
868 	hapd->sta_list = sta;
869 	hapd->num_sta++;
870 #ifndef EXT_CODE_CROP
871 	ap_sta_hash_add(hapd, sta);
872 #endif /* EXT_CODE_CROP */
873 	ap_sta_remove_in_other_bss(hapd, sta);
874 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
875 #ifndef EXT_CODE_CROP
876 	dl_list_init(&sta->ip6addr);
877 #endif /* EXT_CODE_CROP */
878 
879 #ifdef CONFIG_TAXONOMY
880 	sta_track_claim_taxonomy_info(hapd->iface, addr,
881 				      &sta->probe_ie_taxonomy);
882 #endif /* CONFIG_TAXONOMY */
883 
884 	return sta;
885 }
886 
887 
888 static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
889 {
890 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
891 
892 #ifndef LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT
893 	if (sta->ipaddr)
894 		hostapd_drv_br_delete_ip_neigh(hapd, 4, (u8 *) &sta->ipaddr);
895 #endif /* LOS_CONFIG_EXT_DRIVER_NOT_SUPPORT */
896 #ifndef EXT_CODE_CROP
897 	ap_sta_ip6addr_del(hapd, sta);
898 #endif /* EXT_CODE_CROP */
899 
900 	wpa_error_buf(MSG_DEBUG, "%s: Removing STA ",
901 		   hapd->conf->iface, strlen(hapd->conf->iface));
902 	wpa_error_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x" " from kernel driver",
903 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
904 	if (hostapd_drv_sta_remove(hapd, sta->addr) &&
905 	    sta->flags & WLAN_STA_ASSOC) {
906 		wpa_warning_buf(MSG_DEBUG, "%s: Could not remove station ",
907 			   hapd->conf->iface, strlen(hapd->conf->iface));
908 		wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x"
909 			   " from kernel driver",
910 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
911 		return -1;
912 	}
913 	sta->added_unassoc = 0;
914 	return 0;
915 }
916 
917 
918 static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
919 				       struct sta_info *sta)
920 {
921 	struct hostapd_iface *iface = hapd->iface;
922 	size_t i;
923 
924 	for (i = 0; i < iface->num_bss; i++) {
925 		struct hostapd_data *bss = iface->bss[i];
926 		struct sta_info *sta2;
927 		/* bss should always be set during operation, but it may be
928 		 * NULL during reconfiguration. Assume the STA is not
929 		 * associated to another BSS in that case to avoid NULL pointer
930 		 * dereferences. */
931 		if (bss == hapd || bss == NULL)
932 			continue;
933 		sta2 = ap_get_sta(bss, sta->addr);
934 		if (!sta2)
935 			continue;
936 
937 		wpa_warning_two_buf(MSG_DEBUG, "%s: disconnect old STA "
938 			   " association from another BSS %s",
939 			   hapd->conf->iface, strlen(hapd->conf->iface),
940 			   bss->conf->iface, strlen(bss->conf->iface));
941 		wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
942 			   (sta2->addr)[0], (sta2->addr)[3], (sta2->addr)[4], (sta2->addr)[5]);
943 		ap_sta_disconnect(bss, sta2, sta2->addr,
944 				  WLAN_REASON_PREV_AUTH_NOT_VALID);
945 	}
946 }
947 
948 
949 static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
950 {
951 	struct hostapd_data *hapd = eloop_ctx;
952 	struct sta_info *sta = timeout_ctx;
953 
954 	wpa_warning_buf(MSG_DEBUG, "%s: Disassociation callback for STA ",
955 		   hapd->conf->iface, strlen(hapd->conf->iface));
956 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
957 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
958 	ap_sta_remove(hapd, sta);
959 	mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
960 }
961 
962 
963 void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
964 			 u16 reason)
965 {
966 	wpa_warning_buf(MSG_DEBUG, "%s: disassociate STA ",
967 		   hapd->conf->iface, strlen(hapd->conf->iface));
968 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
969 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
970 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
971 #ifndef EXT_CODE_CROP
972 	if (hapd->iface->current_mode &&
973 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
974 		/* Skip deauthentication in DMG/IEEE 802.11ad */
975 		sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC |
976 				WLAN_STA_ASSOC_REQ_OK);
977 		sta->timeout_next = STA_REMOVE;
978 	} else {
979 		sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
980 		sta->timeout_next = STA_DEAUTH;
981 	}
982 #else
983 	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
984 	sta->timeout_next = STA_DEAUTH;
985 #endif /* EXT_CODE_CROP */
986 	ap_sta_set_authorized(hapd, sta, 0);
987 #ifndef EXT_CODE_CROP
988 	hostapd_set_sta_flags(hapd, sta);
989 #endif
990 	wpa_warning_log4(MSG_DEBUG, "ap_sta_disassociate: reschedule ap_handle_timer timeout "
991 		   "for " "%02x:xx:xx:%02x:%02x:%02x",
992 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
993 	wpa_warning_log1(MSG_DEBUG, " (%d seconds - "
994 		   "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
995 		   AP_MAX_INACTIVITY_AFTER_DISASSOC);
996 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
997 	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
998 			       ap_handle_timer, hapd, sta);
999 	accounting_sta_stop(hapd, sta);
1000 	ieee802_1x_free_station(hapd, sta);
1001 	wpa_auth_sta_deinit(sta->wpa_sm);
1002 	sta->wpa_sm = NULL;
1003 
1004 	sta->disassoc_reason = reason;
1005 	sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1006 	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1007 	eloop_register_timeout(hapd->iface->drv_flags &
1008 			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1009 			       ap_sta_disassoc_cb_timeout, hapd, sta);
1010 }
1011 
1012 
1013 static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
1014 {
1015 	struct hostapd_data *hapd = eloop_ctx;
1016 	struct sta_info *sta = timeout_ctx;
1017 
1018 	wpa_warning_buf(MSG_DEBUG, "%s: Deauthentication callback for STA ",
1019 		   hapd->conf->iface, strlen(hapd->conf->iface));
1020 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
1021 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1022 	ap_sta_remove(hapd, sta);
1023 	mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
1024 }
1025 
1026 
1027 void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
1028 			   u16 reason)
1029 {
1030 #ifndef EXT_CODE_CROP
1031 	if (hapd->iface->current_mode &&
1032 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1033 		/* Deauthentication is not used in DMG/IEEE 802.11ad;
1034 		 * disassociate the STA instead. */
1035 		ap_sta_disassociate(hapd, sta, reason);
1036 		return;
1037 	}
1038 #endif /* EXT_CODE_CROP */
1039 	wpa_warning_buf(MSG_DEBUG, "%s: deauthenticate STA ",
1040 		   hapd->conf->iface, strlen(hapd->conf->iface));
1041 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
1042 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1043 	sta->last_seq_ctrl = WLAN_INVALID_MGMT_SEQ;
1044 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
1045 	ap_sta_set_authorized(hapd, sta, 0);
1046 #ifndef EXT_CODE_CROP
1047 	hostapd_set_sta_flags(hapd, sta);
1048 #endif
1049 	sta->timeout_next = STA_REMOVE;
1050 	wpa_warning_log4(MSG_DEBUG, "ap_sta_deauthenticate: reschedule ap_handle_timer timeout "
1051 		   "for " "%02x:xx:xx:%02x:%02x:%02x",
1052 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1053 	wpa_warning_log1(MSG_DEBUG, " (%d seconds - "
1054 		   "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1055 		   AP_MAX_INACTIVITY_AFTER_DEAUTH);
1056 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1057 	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1058 			       ap_handle_timer, hapd, sta);
1059 	accounting_sta_stop(hapd, sta);
1060 	ieee802_1x_free_station(hapd, sta);
1061 
1062 	sta->deauth_reason = reason;
1063 	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1064 	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1065 	eloop_register_timeout(hapd->iface->drv_flags &
1066 			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1067 			       ap_sta_deauth_cb_timeout, hapd, sta);
1068 }
1069 
1070 
1071 #ifdef CONFIG_WPS
1072 int ap_sta_wps_cancel(struct hostapd_data *hapd,
1073 		      struct sta_info *sta, void *ctx)
1074 {
1075 	(void)ctx;
1076 	if (sta && (sta->flags & WLAN_STA_WPS)) {
1077 		ap_sta_deauthenticate(hapd, sta,
1078 				      WLAN_REASON_PREV_AUTH_NOT_VALID);
1079 		wpa_warning_log4(MSG_DEBUG, "WPS: ap_sta_wps_cancel: Deauth sta=" "%02x:xx:xx:%02x:%02x:%02x",
1080 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1081 		return 1;
1082 	}
1083 
1084 	return 0;
1085 }
1086 #endif /* CONFIG_WPS */
1087 #ifndef LOS_CONFIG_NO_VLAN
1088 
1089 static int ap_sta_get_free_vlan_id(struct hostapd_data *hapd)
1090 {
1091 	struct hostapd_vlan *vlan;
1092 	int vlan_id = MAX_VLAN_ID + 2;
1093 
1094 retry:
1095 	for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1096 		if (vlan->vlan_id == vlan_id) {
1097 			vlan_id++;
1098 			goto retry;
1099 		}
1100 	}
1101 	return vlan_id;
1102 }
1103 
1104 
1105 int ap_sta_set_vlan(struct hostapd_data *hapd, struct sta_info *sta,
1106 		    struct vlan_description *vlan_desc)
1107 {
1108 	struct hostapd_vlan *vlan = NULL, *wildcard_vlan = NULL;
1109 	int old_vlan_id, vlan_id = 0, ret = 0;
1110 
1111 	/* Check if there is something to do */
1112 	if (hapd->conf->ssid.per_sta_vif && !sta->vlan_id) {
1113 		/* This sta is lacking its own vif */
1114 	} else if (hapd->conf->ssid.dynamic_vlan == DYNAMIC_VLAN_DISABLED &&
1115 		   !hapd->conf->ssid.per_sta_vif && sta->vlan_id) {
1116 		/* sta->vlan_id needs to be reset */
1117 	} else if (!vlan_compare(vlan_desc, sta->vlan_desc)) {
1118 		return 0; /* nothing to change */
1119 	}
1120 
1121 	/* Now the real VLAN changed or the STA just needs its own vif */
1122 	if (hapd->conf->ssid.per_sta_vif) {
1123 		/* Assign a new vif, always */
1124 		/* find a free vlan_id sufficiently big */
1125 		vlan_id = ap_sta_get_free_vlan_id(hapd);
1126 		/* Get wildcard VLAN */
1127 		for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1128 			if (vlan->vlan_id == VLAN_ID_WILDCARD)
1129 				break;
1130 		}
1131 		if (!vlan) {
1132 			hostapd_logger(hapd, sta->addr,
1133 				       HOSTAPD_MODULE_IEEE80211,
1134 				       HOSTAPD_LEVEL_DEBUG,
1135 				       "per_sta_vif missing wildcard");
1136 			vlan_id = 0;
1137 			ret = -1;
1138 			goto done;
1139 		}
1140 	} else if (vlan_desc && vlan_desc->notempty) {
1141 		for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1142 			if (!vlan_compare(&vlan->vlan_desc, vlan_desc))
1143 				break;
1144 			if (vlan->vlan_id == VLAN_ID_WILDCARD)
1145 				wildcard_vlan = vlan;
1146 		}
1147 		if (vlan) {
1148 			vlan_id = vlan->vlan_id;
1149 		} else if (wildcard_vlan) {
1150 			vlan = wildcard_vlan;
1151 			vlan_id = vlan_desc->untagged;
1152 			if (vlan_desc->tagged[0]) {
1153 				/* Tagged VLAN configuration */
1154 				vlan_id = ap_sta_get_free_vlan_id(hapd);
1155 			}
1156 		} else {
1157 			hostapd_logger(hapd, sta->addr,
1158 				       HOSTAPD_MODULE_IEEE80211,
1159 				       HOSTAPD_LEVEL_DEBUG,
1160 				       "missing vlan and wildcard for vlan=%d%s",
1161 				       vlan_desc->untagged,
1162 				       vlan_desc->tagged[0] ? "+" : "");
1163 			vlan_id = 0;
1164 			ret = -1;
1165 			goto done;
1166 		}
1167 	}
1168 
1169 	if (vlan && vlan->vlan_id == VLAN_ID_WILDCARD) {
1170 		vlan = vlan_add_dynamic(hapd, vlan, vlan_id, vlan_desc);
1171 		if (vlan == NULL) {
1172 			hostapd_logger(hapd, sta->addr,
1173 				       HOSTAPD_MODULE_IEEE80211,
1174 				       HOSTAPD_LEVEL_DEBUG,
1175 				       "could not add dynamic VLAN interface for vlan=%d%s",
1176 				       vlan_desc ? vlan_desc->untagged : -1,
1177 				       (vlan_desc && vlan_desc->tagged[0]) ?
1178 				       "+" : "");
1179 			vlan_id = 0;
1180 			ret = -1;
1181 			goto done;
1182 		}
1183 
1184 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1185 			       HOSTAPD_LEVEL_DEBUG,
1186 			       "added new dynamic VLAN interface '%s'",
1187 			       vlan->ifname);
1188 	} else if (vlan && vlan->dynamic_vlan > 0) {
1189 		vlan->dynamic_vlan++;
1190 		hostapd_logger(hapd, sta->addr,
1191 			       HOSTAPD_MODULE_IEEE80211,
1192 			       HOSTAPD_LEVEL_DEBUG,
1193 			       "updated existing dynamic VLAN interface '%s'",
1194 			       vlan->ifname);
1195 	}
1196 done:
1197 	old_vlan_id = sta->vlan_id;
1198 	sta->vlan_id = vlan_id;
1199 	sta->vlan_desc = vlan ? &vlan->vlan_desc : NULL;
1200 
1201 	if (vlan_id != old_vlan_id && old_vlan_id)
1202 		vlan_remove_dynamic(hapd, old_vlan_id);
1203 
1204 	return ret;
1205 }
1206 
1207 
1208 int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta)
1209 {
1210 #ifndef CONFIG_NO_VLAN
1211 	const char *iface;
1212 	struct hostapd_vlan *vlan = NULL;
1213 	int ret;
1214 	int old_vlanid = sta->vlan_id_bound;
1215 
1216 	if ((sta->flags & WLAN_STA_WDS) && sta->vlan_id == 0) {
1217 		wpa_printf(MSG_DEBUG,
1218 			   "Do not override WDS VLAN assignment for STA "
1219 			   MACSTR, MAC2STR(sta->addr));
1220 		return 0;
1221 	}
1222 
1223 	iface = hapd->conf->iface;
1224 	if (hapd->conf->ssid.vlan[0])
1225 		iface = hapd->conf->ssid.vlan;
1226 
1227 	if (sta->vlan_id > 0) {
1228 		for (vlan = hapd->conf->vlan; vlan; vlan = vlan->next) {
1229 			if (vlan->vlan_id == sta->vlan_id)
1230 				break;
1231 		}
1232 		if (vlan)
1233 			iface = vlan->ifname;
1234 	}
1235 
1236 	/*
1237 	 * Do not increment ref counters if the VLAN ID remains same, but do
1238 	 * not skip hostapd_drv_set_sta_vlan() as hostapd_drv_sta_remove() might
1239 	 * have been called before.
1240 	 */
1241 	if (sta->vlan_id == old_vlanid)
1242 		goto skip_counting;
1243 
1244 	if (sta->vlan_id > 0 && !vlan &&
1245 	    !(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_VLAN_OFFLOAD)) {
1246 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1247 			       HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
1248 			       "binding station to (vlan_id=%d)",
1249 			       sta->vlan_id);
1250 		ret = -1;
1251 		goto done;
1252 	} else if (vlan && vlan->dynamic_vlan > 0) {
1253 		vlan->dynamic_vlan++;
1254 		hostapd_logger(hapd, sta->addr,
1255 			       HOSTAPD_MODULE_IEEE80211,
1256 			       HOSTAPD_LEVEL_DEBUG,
1257 			       "updated existing dynamic VLAN interface '%s'",
1258 			       iface);
1259 	}
1260 
1261 	/* ref counters have been increased, so mark the station */
1262 	sta->vlan_id_bound = sta->vlan_id;
1263 
1264 skip_counting:
1265 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1266 		       HOSTAPD_LEVEL_DEBUG, "binding station to interface "
1267 		       "'%s'", iface);
1268 
1269 	if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
1270 		wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
1271 
1272 	ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
1273 	if (ret < 0) {
1274 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1275 			       HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
1276 			       "entry to vlan_id=%d", sta->vlan_id);
1277 	}
1278 
1279 	/* During 1x reauth, if the vlan id changes, then remove the old id. */
1280 	if (old_vlanid > 0 && old_vlanid != sta->vlan_id)
1281 		vlan_remove_dynamic(hapd, old_vlanid);
1282 done:
1283 
1284 	return ret;
1285 #else /* CONFIG_NO_VLAN */
1286 	return 0;
1287 #endif /* CONFIG_NO_VLAN */
1288 }
1289 #endif
1290 
1291 #ifdef CONFIG_IEEE80211W_AP
1292 
1293 int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
1294 {
1295 	u32 tu;
1296 	struct os_reltime now, passed;
1297 	os_get_reltime(&now);
1298 	os_reltime_sub(&now, &sta->sa_query_start, &passed);
1299 	tu = (passed.sec * 1000000 + passed.usec) / 1024;
1300 	if (hapd->conf->assoc_sa_query_max_timeout < tu) {
1301 		hostapd_logger(hapd, sta->addr,
1302 			       HOSTAPD_MODULE_IEEE80211,
1303 			       HOSTAPD_LEVEL_DEBUG,
1304 			       "association SA Query timed out");
1305 		sta->sa_query_timed_out = 1;
1306 		os_free(sta->sa_query_trans_id);
1307 		sta->sa_query_trans_id = NULL;
1308 		sta->sa_query_count = 0;
1309 		eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1310 		return 1;
1311 	}
1312 
1313 	return 0;
1314 }
1315 
1316 
1317 static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
1318 {
1319 	struct hostapd_data *hapd = eloop_ctx;
1320 	struct sta_info *sta = timeout_ctx;
1321 	unsigned int timeout, sec, usec;
1322 	u8 *trans_id, *nbuf;
1323 
1324 	wpa_warning_buf(MSG_DEBUG, "%s: SA Query timer for STA ",
1325 		   hapd->conf->iface, strlen(hapd->conf->iface));
1326 	wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
1327 		   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1328 	wpa_warning_log1(MSG_DEBUG, " (count=%d)", sta->sa_query_count);
1329 	if (sta->sa_query_count > 0 &&
1330 	    ap_check_sa_query_timeout(hapd, sta))
1331 		return;
1332 	if (sta->sa_query_count >= 1000)
1333 		return;
1334 
1335 	nbuf = os_realloc_array(sta->sa_query_trans_id,
1336 				sta->sa_query_count + 1,
1337 				WLAN_SA_QUERY_TR_ID_LEN);
1338 	if (nbuf == NULL)
1339 		return;
1340 	if (sta->sa_query_count == 0) {
1341 		/* Starting a new SA Query procedure */
1342 		os_get_reltime(&sta->sa_query_start);
1343 	}
1344 	trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
1345 	sta->sa_query_trans_id = nbuf;
1346 	sta->sa_query_count++;
1347 
1348 	if (os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN) < 0) {
1349 		/*
1350 		 * We don't really care which ID is used here, so simply
1351 		 * hardcode this if the mostly theoretical os_get_random()
1352 		 * failure happens.
1353 		 */
1354 		trans_id[0] = 0x12;
1355 		trans_id[1] = 0x34;
1356 	}
1357 
1358 	timeout = hapd->conf->assoc_sa_query_retry_timeout;
1359 	sec = ((timeout / 1000) * 1024) / 1000;
1360 	usec = (timeout % 1000) * 1024;
1361 	eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
1362 
1363 	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
1364 		       HOSTAPD_LEVEL_DEBUG,
1365 		       "association SA Query attempt %d", sta->sa_query_count);
1366 
1367 	ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
1368 }
1369 
1370 
1371 void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1372 {
1373 	ap_sa_query_timer(hapd, sta);
1374 }
1375 
1376 
1377 void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
1378 {
1379 	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
1380 	os_free(sta->sa_query_trans_id);
1381 	sta->sa_query_trans_id = NULL;
1382 	sta->sa_query_count = 0;
1383 }
1384 
1385 #endif /* CONFIG_IEEE80211W_AP */
1386 
1387 
1388 const char * ap_sta_wpa_get_keyid(struct hostapd_data *hapd,
1389 				  struct sta_info *sta)
1390 {
1391 	struct hostapd_wpa_psk *psk;
1392 	struct hostapd_ssid *ssid;
1393 	const u8 *pmk;
1394 	int pmk_len;
1395 
1396 	ssid = &hapd->conf->ssid;
1397 
1398 	pmk = wpa_auth_get_pmk(sta->wpa_sm, &pmk_len);
1399 	if (!pmk || pmk_len != PMK_LEN)
1400 		return NULL;
1401 
1402 	for (psk = ssid->wpa_psk; psk; psk = psk->next)
1403 		if (os_memcmp(pmk, psk->psk, PMK_LEN) == 0)
1404 			break;
1405 	if (!psk)
1406 		return NULL;
1407 	if (!psk || !psk->keyid[0])
1408 		return NULL;
1409 
1410 	return psk->keyid;
1411 }
1412 
1413 
1414 void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
1415 			   int authorized)
1416 {
1417 	const u8 *dev_addr = NULL;
1418 	char buf[100];
1419 #ifdef CONFIG_P2P
1420 	u8 addr[ETH_ALEN];
1421 	u8 ip_addr_buf[4];
1422 #endif /* CONFIG_P2P */
1423 
1424 	if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
1425 		return;
1426 
1427 	if (authorized)
1428 		sta->flags |= WLAN_STA_AUTHORIZED;
1429 	else
1430 		sta->flags &= ~WLAN_STA_AUTHORIZED;
1431 
1432 #ifdef CONFIG_P2P
1433 	if (hapd->p2p_group == NULL) {
1434 		if (sta->p2p_ie != NULL &&
1435 		    p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
1436 			dev_addr = addr;
1437 	} else
1438 		dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
1439 
1440 	if (dev_addr)
1441 		os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
1442 			    MAC2STR(sta->addr), MAC2STR(dev_addr));
1443 	else
1444 #endif /* CONFIG_P2P */
1445 		os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
1446 
1447 #ifdef CONFIG_P2P
1448 	if (hapd->sta_authorized_cb)
1449 		hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
1450 					sta->addr, authorized, dev_addr);
1451 #endif /* CONFIG_P2P */
1452 	if (authorized) {
1453 		const char *keyid;
1454 		char keyid_buf[100];
1455 		char ip_addr[100];
1456 
1457 		keyid_buf[0] = '\0';
1458 		ip_addr[0] = '\0';
1459 #ifdef CONFIG_P2P
1460 		if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
1461 			os_snprintf(ip_addr, sizeof(ip_addr),
1462 				    " ip_addr=%u.%u.%u.%u",
1463 				    ip_addr_buf[0], ip_addr_buf[1],
1464 				    ip_addr_buf[2], ip_addr_buf[3]);
1465 		}
1466 #endif /* CONFIG_P2P */
1467 
1468 		keyid = ap_sta_wpa_get_keyid(hapd, sta);
1469 		if (keyid) {
1470 			os_snprintf(keyid_buf, sizeof(keyid_buf),
1471 				    " keyid=%s", keyid);
1472 		}
1473 #ifndef CONFIG_NO_WPA_MSG
1474 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s%s",
1475 			buf, ip_addr, keyid_buf);
1476 #endif /* CONFIG_NO_WPA_MSG */
1477 		osal_printk("+NOTICE:STA CONNECTED\r\n");
1478 #ifdef LOS_WPA_PATCH
1479 		event_ap_sta_connected ap_sta_connected = {0};
1480 
1481 		(void)os_memcpy(ap_sta_connected.addr, sta->addr, ETH_ALEN);
1482 		(void)os_memcpy(ap_sta_connected.ifname, hapd->conf->iface, IFNAMSIZ);
1483 		wpa_comm_event_report(EXT_WIFI_EVT_STA_CONNECTED, hapd->msg_ctx, &ap_sta_connected);
1484 #endif /* LOS_WPA_PATCH */
1485 #ifndef EXT_CODE_CROP
1486 		if (hapd->msg_ctx_parent &&
1487 		    hapd->msg_ctx_parent != hapd->msg_ctx)
1488 			wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1489 					  AP_STA_CONNECTED "%s%s%s",
1490 					  buf, ip_addr, keyid_buf);
1491 #endif /* EXT_CODE_CROP */
1492 	} else {
1493 		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
1494 		osal_printk("+NOTICE:STA DISCONNECTED\r\n");
1495 #ifdef LOS_WPA_PATCH
1496 		event_ap_sta_disconnected ap_sta_disconnected = {0};
1497 
1498 		(void)os_memcpy(ap_sta_disconnected.addr, sta->addr, ETH_ALEN);
1499 		(void)os_memcpy(ap_sta_disconnected.ifname, hapd->conf->iface, IFNAMSIZ);
1500 		wpa_comm_event_report(EXT_WIFI_EVT_STA_DISCONNECTED, hapd->msg_ctx, &ap_sta_disconnected);
1501 #endif /* LOS_WPA_PATCH */
1502 #ifndef EXT_CODE_CROP
1503 		if (hapd->msg_ctx_parent &&
1504 		    hapd->msg_ctx_parent != hapd->msg_ctx)
1505 			wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
1506 					  AP_STA_DISCONNECTED "%s", buf);
1507 #endif /* EXT_CODE_CROP */
1508 	}
1509 
1510 #ifdef CONFIG_FST
1511 	if (hapd->iface->fst) {
1512 		if (authorized)
1513 			fst_notify_peer_connected(hapd->iface->fst, sta->addr);
1514 		else
1515 			fst_notify_peer_disconnected(hapd->iface->fst,
1516 						     sta->addr);
1517 	}
1518 #endif /* CONFIG_FST */
1519 }
1520 
1521 
1522 void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1523 		       const u8 *addr, u16 reason)
1524 {
1525 	if (sta) {
1526 		wpa_warning_buf(MSG_DEBUG, "ap_sta_disconnect: %s STA ",
1527 			   hapd->conf->iface, strlen(hapd->conf->iface));
1528 		wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
1529 			   addr[0], addr[3], addr[4], addr[5]);
1530 		wpa_warning_log1(MSG_DEBUG, " reason=%u", reason);
1531 	}
1532 	else if (addr) {
1533 		wpa_warning_buf(MSG_DEBUG, "ap_sta_disconnect: %s addr ",
1534 			   hapd->conf->iface, strlen(hapd->conf->iface));
1535 		wpa_warning_log4(MSG_DEBUG, "%02x:xx:xx:%02x:%02x:%02x",
1536 			   addr[0], addr[3], addr[4], addr[5]);
1537 		wpa_warning_log1(MSG_DEBUG, " reason=%u", reason);
1538 	}
1539 
1540 	if (sta == NULL && addr)
1541 		sta = ap_get_sta(hapd, addr);
1542 #ifndef EXT_CODE_CROP
1543 	if (addr)
1544 		hostapd_drv_sta_deauth(hapd, addr, reason);
1545 #endif /* EXT_CODE_CROP */
1546 	if (sta == NULL)
1547 		return;
1548 	ap_sta_set_authorized(hapd, sta, 0);
1549 	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1550 #ifndef EXT_CODE_CROP
1551 	hostapd_set_sta_flags(hapd, sta);
1552 #endif
1553 	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1554 	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1555 	wpa_printf(MSG_DEBUG, "%s: %s: reschedule ap_handle_timer timeout "
1556 		   "for " MACSTR " (%d seconds - "
1557 		   "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1558 		   hapd->conf->iface, __func__, MAC2STR(sta->addr),
1559 		   AP_MAX_INACTIVITY_AFTER_DEAUTH);
1560 	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1561 	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1562 			       ap_handle_timer, hapd, sta);
1563 	sta->timeout_next = STA_REMOVE;
1564 
1565 #ifndef EXT_CODE_CROP
1566 	if (hapd->iface->current_mode &&
1567 	    hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211AD) {
1568 		/* Deauthentication is not used in DMG/IEEE 802.11ad;
1569 		 * disassociate the STA instead. */
1570 		sta->disassoc_reason = reason;
1571 		sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
1572 		eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1573 		eloop_register_timeout(hapd->iface->drv_flags &
1574 				       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ?
1575 				       2 : 0, 0, ap_sta_disassoc_cb_timeout,
1576 				       hapd, sta);
1577 		return;
1578 	}
1579 #endif  /* EXT_CODE_CROP */
1580 	sta->deauth_reason = reason;
1581 	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1582 	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1583 	eloop_register_timeout(hapd->iface->drv_flags &
1584 			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1585 			       ap_sta_deauth_cb_timeout, hapd, sta);
1586 }
1587 
1588 
1589 void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1590 {
1591 	if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1592 		wpa_warning_log0(MSG_DEBUG, "Ignore deauth cb for test frame");
1593 		return;
1594 	}
1595 	sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1596 	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1597 	ap_sta_deauth_cb_timeout(hapd, sta);
1598 }
1599 
1600 
1601 void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1602 {
1603 	if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1604 		wpa_warning_log0(MSG_DEBUG, "Ignore disassoc cb for test frame");
1605 		return;
1606 	}
1607 	sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1608 	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1609 	ap_sta_disassoc_cb_timeout(hapd, sta);
1610 }
1611 
1612 
1613 void ap_sta_clear_disconnect_timeouts(struct hostapd_data *hapd,
1614 				      struct sta_info *sta)
1615 {
1616 	if (eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta) > 0) {
1617 		wpa_warning_buf(MSG_DEBUG,
1618 			   "%s: Removed ap_sta_deauth_cb_timeout timeout for ",
1619 			   hapd->conf->iface, strlen(hapd->conf->iface));
1620 		wpa_warning_log4(MSG_DEBUG,
1621 			   "%02x:xx:xx:%02x:%02x:%02x",
1622 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1623 	}
1624 	if (eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta) > 0) {
1625 		wpa_warning_buf(MSG_DEBUG,
1626 			   "%s: Removed ap_sta_disassoc_cb_timeout timeout for ",
1627 			   hapd->conf->iface, strlen(hapd->conf->iface));
1628 		wpa_warning_log4(MSG_DEBUG,
1629 			   "%02x:xx:xx:%02x:%02x:%02x",
1630 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1631 	}
1632 	if (eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta) > 0)
1633 	{
1634 		wpa_warning_buf(MSG_DEBUG,
1635 			   "%s: Removed ap_sta_delayed_1x_auth_fail_cb timeout for ",
1636 			   hapd->conf->iface, strlen(hapd->conf->iface));
1637 		wpa_warning_log4(MSG_DEBUG,
1638 			   "%02x:xx:xx:%02x:%02x:%02x",
1639 			   (sta->addr)[0], (sta->addr)[3], (sta->addr)[4], (sta->addr)[5]);
1640 #ifdef CONFIG_WPS_AP
1641 		if (sta->flags & WLAN_STA_WPS)
1642 			hostapd_wps_eap_completed(hapd);
1643 #endif /* CONFIG_WPS_AP */
1644 	}
1645 }
1646 
1647 
1648 int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1649 {
1650 	int res;
1651 
1652 	buf[0] = '\0';
1653 	res = os_snprintf(buf, buflen,
1654 			  "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1655 			  (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1656 			  (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1657 			  (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1658 			  (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1659 			   ""),
1660 			  (flags & WLAN_STA_SHORT_PREAMBLE ?
1661 			   "[SHORT_PREAMBLE]" : ""),
1662 			  (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1663 			  (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1664 			  (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1665 			  (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1666 			  (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1667 			  (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1668 			  (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1669 			  (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1670 			  (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1671 			  (flags & WLAN_STA_HT ? "[HT]" : ""),
1672 			  (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1673 			  (flags & WLAN_STA_HE ? "[HE]" : ""),
1674 			  (flags & WLAN_STA_6GHZ ? "[6GHZ]" : ""),
1675 			  (flags & WLAN_STA_VENDOR_VHT ? "[VENDOR_VHT]" : ""),
1676 			  (flags & WLAN_STA_WNM_SLEEP_MODE ?
1677 			   "[WNM_SLEEP_MODE]" : ""));
1678 	if (os_snprintf_error(buflen, res))
1679 		res = -1;
1680 
1681 	return res;
1682 }
1683 
1684 
1685 static void ap_sta_delayed_1x_auth_fail_cb(void *eloop_ctx, void *timeout_ctx)
1686 {
1687 	struct hostapd_data *hapd = eloop_ctx;
1688 	struct sta_info *sta = timeout_ctx;
1689 	u16 reason;
1690 
1691 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1692 		"IEEE 802.1X: Scheduled disconnection of " MACSTR
1693 		" after EAP-Failure", MAC2STR(sta->addr));
1694 
1695 	reason = sta->disconnect_reason_code;
1696 	if (!reason)
1697 		reason = WLAN_REASON_IEEE_802_1X_AUTH_FAILED;
1698 	ap_sta_disconnect(hapd, sta, sta->addr, reason);
1699 #ifdef CONFIG_WPS_AP
1700 	if (sta->flags & WLAN_STA_WPS)
1701 		hostapd_wps_eap_completed(hapd);
1702 #endif /* CONFIG_WPS_AP */
1703 }
1704 
1705 
1706 void ap_sta_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1707 					    struct sta_info *sta)
1708 {
1709 	wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
1710 		"IEEE 802.1X: Force disconnection of " MACSTR
1711 		" after EAP-Failure in 10 ms", MAC2STR(sta->addr));
1712 
1713 	/*
1714 	 * Add a small sleep to increase likelihood of previously requested
1715 	 * EAP-Failure TX getting out before this should the driver reorder
1716 	 * operations.
1717 	 */
1718 	eloop_cancel_timeout(ap_sta_delayed_1x_auth_fail_cb, hapd, sta);
1719 	eloop_register_timeout(0, 10000, ap_sta_delayed_1x_auth_fail_cb,
1720 			       hapd, sta);
1721 }
1722 
1723 
1724 int ap_sta_pending_delayed_1x_auth_fail_disconnect(struct hostapd_data *hapd,
1725 						   struct sta_info *sta)
1726 {
1727 	return eloop_is_timeout_registered(ap_sta_delayed_1x_auth_fail_cb,
1728 					   hapd, sta);
1729 }
1730 
1731 #ifndef EXT_CODE_CROP
1732 int ap_sta_re_add(struct hostapd_data *hapd, struct sta_info *sta)
1733 {
1734 	/*
1735 	 * If a station that is already associated to the AP, is trying to
1736 	 * authenticate again, remove the STA entry, in order to make sure the
1737 	 * STA PS state gets cleared and configuration gets updated. To handle
1738 	 * this, station's added_unassoc flag is cleared once the station has
1739 	 * completed association.
1740 	 */
1741 	ap_sta_set_authorized(hapd, sta, 0);
1742 	hostapd_drv_sta_remove(hapd, sta->addr);
1743 	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_AUTH | WLAN_STA_AUTHORIZED);
1744 #ifndef EXT_CODE_CROP
1745 	if (hostapd_sta_add(hapd, sta->addr, 0, 0,
1746 			    sta->supported_rates,
1747 			    sta->supported_rates_len,
1748 			    0, NULL, NULL, NULL, 0, NULL,
1749 			    sta->flags, 0, 0, 0, 0)) {
1750 #else
1751 	if (hostapd_sta_add(hapd, sta->addr, 0, 0,
1752 		    sta->supported_rates,
1753 		    sta->supported_rates_len,
1754 		    0, NULL, sta->flags, 0, 0, 0)) {
1755 #endif /* EXT_CODE_CROP */
1756 		hostapd_logger(hapd, sta->addr,
1757 			       HOSTAPD_MODULE_IEEE80211,
1758 			       HOSTAPD_LEVEL_NOTICE,
1759 			       "Could not add STA to kernel driver");
1760 		return -1;
1761 	}
1762 
1763 	sta->added_unassoc = 1;
1764 	return 0;
1765 }
1766 #endif /* EXT_CODE_CROP */