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