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