sta_info.c revision c5ec7f57ead87efa365800228aa0b09a12d9e6c4
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 "sta_info.h"
32
33static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
34				       struct sta_info *sta);
35static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
36static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
37static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
38#ifdef CONFIG_IEEE80211W
39static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
40#endif /* CONFIG_IEEE80211W */
41static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
42
43int ap_for_each_sta(struct hostapd_data *hapd,
44		    int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
45			      void *ctx),
46		    void *ctx)
47{
48	struct sta_info *sta;
49
50	for (sta = hapd->sta_list; sta; sta = sta->next) {
51		if (cb(hapd, sta, ctx))
52			return 1;
53	}
54
55	return 0;
56}
57
58
59struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
60{
61	struct sta_info *s;
62
63	s = hapd->sta_hash[STA_HASH(sta)];
64	while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
65		s = s->hnext;
66	return s;
67}
68
69
70static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
71{
72	struct sta_info *tmp;
73
74	if (hapd->sta_list == sta) {
75		hapd->sta_list = sta->next;
76		return;
77	}
78
79	tmp = hapd->sta_list;
80	while (tmp != NULL && tmp->next != sta)
81		tmp = tmp->next;
82	if (tmp == NULL) {
83		wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
84			   "list.", MAC2STR(sta->addr));
85	} else
86		tmp->next = sta->next;
87}
88
89
90void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
91{
92	sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
93	hapd->sta_hash[STA_HASH(sta->addr)] = sta;
94}
95
96
97static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
98{
99	struct sta_info *s;
100
101	s = hapd->sta_hash[STA_HASH(sta->addr)];
102	if (s == NULL) return;
103	if (os_memcmp(s->addr, sta->addr, 6) == 0) {
104		hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
105		return;
106	}
107
108	while (s->hnext != NULL &&
109	       os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
110		s = s->hnext;
111	if (s->hnext != NULL)
112		s->hnext = s->hnext->hnext;
113	else
114		wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
115			   " from hash table", MAC2STR(sta->addr));
116}
117
118
119void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
120{
121	int set_beacon = 0;
122
123	accounting_sta_stop(hapd, sta);
124
125	/* just in case */
126	ap_sta_set_authorized(hapd, sta, 0);
127
128	if (sta->flags & WLAN_STA_WDS)
129		hostapd_set_wds_sta(hapd, sta->addr, sta->aid, 0);
130
131	if (!(sta->flags & WLAN_STA_PREAUTH))
132		hostapd_drv_sta_remove(hapd, sta->addr);
133
134	ap_sta_hash_del(hapd, sta);
135	ap_sta_list_del(hapd, sta);
136
137	if (sta->aid > 0)
138		hapd->sta_aid[(sta->aid - 1) / 32] &=
139			~BIT((sta->aid - 1) % 32);
140
141	hapd->num_sta--;
142	if (sta->nonerp_set) {
143		sta->nonerp_set = 0;
144		hapd->iface->num_sta_non_erp--;
145		if (hapd->iface->num_sta_non_erp == 0)
146			set_beacon++;
147	}
148
149	if (sta->no_short_slot_time_set) {
150		sta->no_short_slot_time_set = 0;
151		hapd->iface->num_sta_no_short_slot_time--;
152		if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
153		    && hapd->iface->num_sta_no_short_slot_time == 0)
154			set_beacon++;
155	}
156
157	if (sta->no_short_preamble_set) {
158		sta->no_short_preamble_set = 0;
159		hapd->iface->num_sta_no_short_preamble--;
160		if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
161		    && hapd->iface->num_sta_no_short_preamble == 0)
162			set_beacon++;
163	}
164
165	if (sta->no_ht_gf_set) {
166		sta->no_ht_gf_set = 0;
167		hapd->iface->num_sta_ht_no_gf--;
168	}
169
170	if (sta->no_ht_set) {
171		sta->no_ht_set = 0;
172		hapd->iface->num_sta_no_ht--;
173	}
174
175	if (sta->ht_20mhz_set) {
176		sta->ht_20mhz_set = 0;
177		hapd->iface->num_sta_ht_20mhz--;
178	}
179
180#ifdef CONFIG_P2P
181	if (sta->no_p2p_set) {
182		sta->no_p2p_set = 0;
183		hapd->num_sta_no_p2p--;
184		if (hapd->num_sta_no_p2p == 0)
185			hostapd_p2p_non_p2p_sta_disconnected(hapd);
186	}
187#endif /* CONFIG_P2P */
188
189#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
190	if (hostapd_ht_operation_update(hapd->iface) > 0)
191		set_beacon++;
192#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
193
194	if (set_beacon)
195		ieee802_11_set_beacons(hapd->iface);
196
197	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
198	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
199	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
200	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
201
202	ieee802_1x_free_station(sta);
203	wpa_auth_sta_deinit(sta->wpa_sm);
204	rsn_preauth_free_station(hapd, sta);
205#ifndef CONFIG_NO_RADIUS
206	radius_client_flush_auth(hapd->radius, sta->addr);
207#endif /* CONFIG_NO_RADIUS */
208
209	os_free(sta->last_assoc_req);
210	os_free(sta->challenge);
211
212#ifdef CONFIG_IEEE80211W
213	os_free(sta->sa_query_trans_id);
214	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
215#endif /* CONFIG_IEEE80211W */
216
217#ifdef CONFIG_P2P
218	p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
219#endif /* CONFIG_P2P */
220
221	wpabuf_free(sta->wps_ie);
222	wpabuf_free(sta->p2p_ie);
223
224	os_free(sta->ht_capabilities);
225	os_free(sta->psk);
226
227	os_free(sta);
228}
229
230
231void hostapd_free_stas(struct hostapd_data *hapd)
232{
233	struct sta_info *sta, *prev;
234
235	sta = hapd->sta_list;
236
237	while (sta) {
238		prev = sta;
239		if (sta->flags & WLAN_STA_AUTH) {
240			mlme_deauthenticate_indication(
241				hapd, sta, WLAN_REASON_UNSPECIFIED);
242		}
243		sta = sta->next;
244		wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
245			   MAC2STR(prev->addr));
246		ap_free_sta(hapd, prev);
247	}
248}
249
250
251/**
252 * ap_handle_timer - Per STA timer handler
253 * @eloop_ctx: struct hostapd_data *
254 * @timeout_ctx: struct sta_info *
255 *
256 * This function is called to check station activity and to remove inactive
257 * stations.
258 */
259void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
260{
261	struct hostapd_data *hapd = eloop_ctx;
262	struct sta_info *sta = timeout_ctx;
263	unsigned long next_time = 0;
264
265	if (sta->timeout_next == STA_REMOVE) {
266		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
267			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
268			       "local deauth request");
269		ap_free_sta(hapd, sta);
270		return;
271	}
272
273	if ((sta->flags & WLAN_STA_ASSOC) &&
274	    (sta->timeout_next == STA_NULLFUNC ||
275	     sta->timeout_next == STA_DISASSOC)) {
276		int inactive_sec;
277		inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
278		if (inactive_sec == -1) {
279			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
280				"Check inactivity: Could not "
281				"get station info from kernel driver for "
282				MACSTR, MAC2STR(sta->addr));
283			/*
284			 * The driver may not support this functionality.
285			 * Anyway, try again after the next inactivity timeout,
286			 * but do not disconnect the station now.
287			 */
288			next_time = hapd->conf->ap_max_inactivity;
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	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
849	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
850	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
851	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
852	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
853			       ap_handle_timer, hapd, sta);
854	sta->timeout_next = STA_REMOVE;
855
856	sta->deauth_reason = reason;
857	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
858	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
859	eloop_register_timeout(hapd->iface->drv_flags &
860			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
861			       ap_sta_deauth_cb_timeout, hapd, sta);
862}
863
864
865void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
866{
867	if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
868		wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
869		return;
870	}
871	sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
872	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
873	ap_sta_deauth_cb_timeout(hapd, sta);
874}
875
876
877void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
878{
879	if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
880		wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
881		return;
882	}
883	sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
884	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
885	ap_sta_disassoc_cb_timeout(hapd, sta);
886}
887