sta_info.c revision 818ea489ef32dcdc7c098d8a336d6e1dd8996112
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * hostapd / Station table
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This software may be distributed under the terms of the BSD license.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * See README for more details.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "utils/includes.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "utils/common.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "utils/eloop.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "common/ieee802_11_defs.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "common/wpa_ctrl.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "common/sae.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "radius/radius.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "radius/radius_client.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "p2p/p2p.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "hostapd.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "accounting.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ieee802_1x.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ieee802_11.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ieee802_11_auth.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "wpa_auth.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "preauth_auth.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ap_config.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "beacon.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ap_mlme.h"
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "vlan_init.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "p2p_hostapd.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ap_drv_ops.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "gas_serv.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "wnm_ap.h"
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sta_info.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)				       struct sta_info *sta);
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx);
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CONFIG_IEEE80211W
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx);
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CONFIG_IEEE80211W */
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int ap_for_each_sta(struct hostapd_data *hapd,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    int (*cb)(struct hostapd_data *hapd, struct sta_info *sta,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			      void *ctx),
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		    void *ctx)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct sta_info *sta;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (sta = hapd->sta_list; sta; sta = sta->next) {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (cb(hapd, sta, ctx))
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			return 1;
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return 0;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct sta_info * ap_get_sta(struct hostapd_data *hapd, const u8 *sta)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct sta_info *s;
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	s = hapd->sta_hash[STA_HASH(sta)];
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	while (s != NULL && os_memcmp(s->addr, sta, 6) != 0)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		s = s->hnext;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return s;
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifdef CONFIG_P2P
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct sta_info * ap_get_sta_p2p(struct hostapd_data *hapd, const u8 *addr)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct sta_info *sta;
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	for (sta = hapd->sta_list; sta; sta = sta->next) {
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		const u8 *p2p_dev_addr;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (sta->p2p_ie == NULL)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			continue;
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (p2p_dev_addr == NULL)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			continue;
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (os_memcmp(p2p_dev_addr, addr, ETH_ALEN) == 0)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			return sta;
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	return NULL;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif /* CONFIG_P2P */
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_sta_list_del(struct hostapd_data *hapd, struct sta_info *sta)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct sta_info *tmp;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (hapd->sta_list == sta) {
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		hapd->sta_list = sta->next;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	tmp = hapd->sta_list;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	while (tmp != NULL && tmp->next != sta)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		tmp = tmp->next;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (tmp == NULL) {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_printf(MSG_DEBUG, "Could not remove STA " MACSTR " from "
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			   "list.", MAC2STR(sta->addr));
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	} else
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		tmp->next = sta->next;
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ap_sta_hash_add(struct hostapd_data *hapd, struct sta_info *sta)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	sta->hnext = hapd->sta_hash[STA_HASH(sta->addr)];
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	hapd->sta_hash[STA_HASH(sta->addr)] = sta;
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)static void ap_sta_hash_del(struct hostapd_data *hapd, struct sta_info *sta)
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	struct sta_info *s;
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	s = hapd->sta_hash[STA_HASH(sta->addr)];
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (s == NULL) return;
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (os_memcmp(s->addr, sta->addr, 6) == 0) {
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		hapd->sta_hash[STA_HASH(sta->addr)] = s->hnext;
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		return;
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	while (s->hnext != NULL &&
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	       os_memcmp(s->hnext->addr, sta->addr, ETH_ALEN) != 0)
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		s = s->hnext;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (s->hnext != NULL)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		s->hnext = s->hnext->hnext;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	else
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		wpa_printf(MSG_DEBUG, "AP: could not remove STA " MACSTR
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			   " from hash table", MAC2STR(sta->addr));
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void ap_free_sta(struct hostapd_data *hapd, struct sta_info *sta)
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles){
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	int set_beacon = 0;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	accounting_sta_stop(hapd, sta);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	/* just in case */
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ap_sta_set_authorized(hapd, sta, 0);
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (sta->flags & WLAN_STA_WDS)
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		hostapd_set_wds_sta(hapd, NULL, sta->addr, sta->aid, 0);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (!hapd->iface->driver_ap_teardown &&
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	    !(sta->flags & WLAN_STA_PREAUTH))
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		hostapd_drv_sta_remove(hapd, sta->addr);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ap_sta_hash_del(hapd, sta);
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	ap_sta_list_del(hapd, sta);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (sta->aid > 0)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		hapd->sta_aid[(sta->aid - 1) / 32] &=
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			~BIT((sta->aid - 1) % 32);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	hapd->num_sta--;
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	if (sta->nonerp_set) {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		sta->nonerp_set = 0;
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		hapd->iface->num_sta_non_erp--;
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)		if (hapd->iface->num_sta_non_erp == 0)
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)			set_beacon++;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)	}
177
178	if (sta->no_short_slot_time_set) {
179		sta->no_short_slot_time_set = 0;
180		hapd->iface->num_sta_no_short_slot_time--;
181		if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
182		    && hapd->iface->num_sta_no_short_slot_time == 0)
183			set_beacon++;
184	}
185
186	if (sta->no_short_preamble_set) {
187		sta->no_short_preamble_set = 0;
188		hapd->iface->num_sta_no_short_preamble--;
189		if (hapd->iface->current_mode->mode == HOSTAPD_MODE_IEEE80211G
190		    && hapd->iface->num_sta_no_short_preamble == 0)
191			set_beacon++;
192	}
193
194	if (sta->no_ht_gf_set) {
195		sta->no_ht_gf_set = 0;
196		hapd->iface->num_sta_ht_no_gf--;
197	}
198
199	if (sta->no_ht_set) {
200		sta->no_ht_set = 0;
201		hapd->iface->num_sta_no_ht--;
202	}
203
204	if (sta->ht_20mhz_set) {
205		sta->ht_20mhz_set = 0;
206		hapd->iface->num_sta_ht_20mhz--;
207	}
208
209#ifdef CONFIG_P2P
210	if (sta->no_p2p_set) {
211		sta->no_p2p_set = 0;
212		hapd->num_sta_no_p2p--;
213		if (hapd->num_sta_no_p2p == 0)
214			hostapd_p2p_non_p2p_sta_disconnected(hapd);
215	}
216#endif /* CONFIG_P2P */
217
218#if defined(NEED_AP_MLME) && defined(CONFIG_IEEE80211N)
219	if (hostapd_ht_operation_update(hapd->iface) > 0)
220		set_beacon++;
221#endif /* NEED_AP_MLME && CONFIG_IEEE80211N */
222
223	if (set_beacon)
224		ieee802_11_set_beacons(hapd->iface);
225
226	wpa_printf(MSG_DEBUG, "%s: cancel ap_handle_timer for " MACSTR,
227		   __func__, MAC2STR(sta->addr));
228	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
229	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
230	eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
231	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
232	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
233
234	ieee802_1x_free_station(sta);
235	wpa_auth_sta_deinit(sta->wpa_sm);
236	rsn_preauth_free_station(hapd, sta);
237#ifndef CONFIG_NO_RADIUS
238	if (hapd->radius)
239		radius_client_flush_auth(hapd->radius, sta->addr);
240#endif /* CONFIG_NO_RADIUS */
241
242	os_free(sta->last_assoc_req);
243	os_free(sta->challenge);
244
245#ifdef CONFIG_IEEE80211W
246	os_free(sta->sa_query_trans_id);
247	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
248#endif /* CONFIG_IEEE80211W */
249
250#ifdef CONFIG_P2P
251	p2p_group_notif_disassoc(hapd->p2p_group, sta->addr);
252#endif /* CONFIG_P2P */
253
254#ifdef CONFIG_INTERWORKING
255	if (sta->gas_dialog) {
256		int i;
257		for (i = 0; i < GAS_DIALOG_MAX; i++)
258			gas_serv_dialog_clear(&sta->gas_dialog[i]);
259		os_free(sta->gas_dialog);
260	}
261#endif /* CONFIG_INTERWORKING */
262
263	wpabuf_free(sta->wps_ie);
264	wpabuf_free(sta->p2p_ie);
265	wpabuf_free(sta->hs20_ie);
266
267	os_free(sta->ht_capabilities);
268	os_free(sta->vht_capabilities);
269	hostapd_free_psk_list(sta->psk);
270	os_free(sta->identity);
271	os_free(sta->radius_cui);
272	os_free(sta->remediation_url);
273	wpabuf_free(sta->hs20_deauth_req);
274	os_free(sta->hs20_session_info_url);
275
276#ifdef CONFIG_SAE
277	sae_clear_data(sta->sae);
278	os_free(sta->sae);
279#endif /* CONFIG_SAE */
280
281	os_free(sta);
282}
283
284
285void hostapd_free_stas(struct hostapd_data *hapd)
286{
287	struct sta_info *sta, *prev;
288
289	sta = hapd->sta_list;
290
291	while (sta) {
292		prev = sta;
293		if (sta->flags & WLAN_STA_AUTH) {
294			mlme_deauthenticate_indication(
295				hapd, sta, WLAN_REASON_UNSPECIFIED);
296		}
297		sta = sta->next;
298		wpa_printf(MSG_DEBUG, "Removing station " MACSTR,
299			   MAC2STR(prev->addr));
300		ap_free_sta(hapd, prev);
301	}
302}
303
304
305/**
306 * ap_handle_timer - Per STA timer handler
307 * @eloop_ctx: struct hostapd_data *
308 * @timeout_ctx: struct sta_info *
309 *
310 * This function is called to check station activity and to remove inactive
311 * stations.
312 */
313void ap_handle_timer(void *eloop_ctx, void *timeout_ctx)
314{
315	struct hostapd_data *hapd = eloop_ctx;
316	struct sta_info *sta = timeout_ctx;
317	unsigned long next_time = 0;
318	int reason;
319
320	wpa_printf(MSG_DEBUG, "%s: " MACSTR " flags=0x%x timeout_next=%d",
321		   __func__, MAC2STR(sta->addr), sta->flags,
322		   sta->timeout_next);
323	if (sta->timeout_next == STA_REMOVE) {
324		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
325			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
326			       "local deauth request");
327		ap_free_sta(hapd, sta);
328		return;
329	}
330
331	if ((sta->flags & WLAN_STA_ASSOC) &&
332	    (sta->timeout_next == STA_NULLFUNC ||
333	     sta->timeout_next == STA_DISASSOC)) {
334		int inactive_sec;
335		/*
336		 * Add random value to timeout so that we don't end up bouncing
337		 * all stations at the same time if we have lots of associated
338		 * stations that are idle (but keep re-associating).
339		 */
340		int fuzz = os_random() % 20;
341		inactive_sec = hostapd_drv_get_inact_sec(hapd, sta->addr);
342		if (inactive_sec == -1) {
343			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
344				"Check inactivity: Could not "
345				"get station info from kernel driver for "
346				MACSTR, MAC2STR(sta->addr));
347			/*
348			 * The driver may not support this functionality.
349			 * Anyway, try again after the next inactivity timeout,
350			 * but do not disconnect the station now.
351			 */
352			next_time = hapd->conf->ap_max_inactivity + fuzz;
353		} else if (inactive_sec < hapd->conf->ap_max_inactivity &&
354			   sta->flags & WLAN_STA_ASSOC) {
355			/* station activity detected; reset timeout state */
356			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
357				"Station " MACSTR " has been active %is ago",
358				MAC2STR(sta->addr), inactive_sec);
359			sta->timeout_next = STA_NULLFUNC;
360			next_time = hapd->conf->ap_max_inactivity + fuzz -
361				inactive_sec;
362		} else {
363			wpa_msg(hapd->msg_ctx, MSG_DEBUG,
364				"Station " MACSTR " has been "
365				"inactive too long: %d sec, max allowed: %d",
366				MAC2STR(sta->addr), inactive_sec,
367				hapd->conf->ap_max_inactivity);
368
369			if (hapd->conf->skip_inactivity_poll)
370				sta->timeout_next = STA_DISASSOC;
371		}
372	}
373
374	if ((sta->flags & WLAN_STA_ASSOC) &&
375	    sta->timeout_next == STA_DISASSOC &&
376	    !(sta->flags & WLAN_STA_PENDING_POLL) &&
377	    !hapd->conf->skip_inactivity_poll) {
378		wpa_msg(hapd->msg_ctx, MSG_DEBUG, "Station " MACSTR
379			" has ACKed data poll", MAC2STR(sta->addr));
380		/* data nullfunc frame poll did not produce TX errors; assume
381		 * station ACKed it */
382		sta->timeout_next = STA_NULLFUNC;
383		next_time = hapd->conf->ap_max_inactivity;
384	}
385
386	if (next_time) {
387		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
388			   "for " MACSTR " (%lu seconds)",
389			   __func__, MAC2STR(sta->addr), next_time);
390		eloop_register_timeout(next_time, 0, ap_handle_timer, hapd,
391				       sta);
392		return;
393	}
394
395	if (sta->timeout_next == STA_NULLFUNC &&
396	    (sta->flags & WLAN_STA_ASSOC)) {
397		wpa_printf(MSG_DEBUG, "  Polling STA");
398		sta->flags |= WLAN_STA_PENDING_POLL;
399		hostapd_drv_poll_client(hapd, hapd->own_addr, sta->addr,
400					sta->flags & WLAN_STA_WMM);
401	} else if (sta->timeout_next != STA_REMOVE) {
402		int deauth = sta->timeout_next == STA_DEAUTH;
403
404		wpa_dbg(hapd->msg_ctx, MSG_DEBUG,
405			"Timeout, sending %s info to STA " MACSTR,
406			deauth ? "deauthentication" : "disassociation",
407			MAC2STR(sta->addr));
408
409		if (deauth) {
410			hostapd_drv_sta_deauth(
411				hapd, sta->addr,
412				WLAN_REASON_PREV_AUTH_NOT_VALID);
413		} else {
414			reason = (sta->timeout_next == STA_DISASSOC) ?
415				WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
416				WLAN_REASON_PREV_AUTH_NOT_VALID;
417
418			hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
419		}
420	}
421
422	switch (sta->timeout_next) {
423	case STA_NULLFUNC:
424		sta->timeout_next = STA_DISASSOC;
425		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
426			   "for " MACSTR " (%d seconds - AP_DISASSOC_DELAY)",
427			   __func__, MAC2STR(sta->addr), AP_DISASSOC_DELAY);
428		eloop_register_timeout(AP_DISASSOC_DELAY, 0, ap_handle_timer,
429				       hapd, sta);
430		break;
431	case STA_DISASSOC:
432	case STA_DISASSOC_FROM_CLI:
433		ap_sta_set_authorized(hapd, sta, 0);
434		sta->flags &= ~WLAN_STA_ASSOC;
435		ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
436		if (!sta->acct_terminate_cause)
437			sta->acct_terminate_cause =
438				RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
439		accounting_sta_stop(hapd, sta);
440		ieee802_1x_free_station(sta);
441		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
442			       HOSTAPD_LEVEL_INFO, "disassociated due to "
443			       "inactivity");
444		reason = (sta->timeout_next == STA_DISASSOC) ?
445			WLAN_REASON_DISASSOC_DUE_TO_INACTIVITY :
446			WLAN_REASON_PREV_AUTH_NOT_VALID;
447		sta->timeout_next = STA_DEAUTH;
448		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
449			   "for " MACSTR " (%d seconds - AP_DEAUTH_DELAY)",
450			   __func__, MAC2STR(sta->addr), AP_DEAUTH_DELAY);
451		eloop_register_timeout(AP_DEAUTH_DELAY, 0, ap_handle_timer,
452				       hapd, sta);
453		mlme_disassociate_indication(hapd, sta, reason);
454		break;
455	case STA_DEAUTH:
456	case STA_REMOVE:
457		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
458			       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
459			       "inactivity (timer DEAUTH/REMOVE)");
460		if (!sta->acct_terminate_cause)
461			sta->acct_terminate_cause =
462				RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT;
463		mlme_deauthenticate_indication(
464			hapd, sta,
465			WLAN_REASON_PREV_AUTH_NOT_VALID);
466		ap_free_sta(hapd, sta);
467		break;
468	}
469}
470
471
472static void ap_handle_session_timer(void *eloop_ctx, void *timeout_ctx)
473{
474	struct hostapd_data *hapd = eloop_ctx;
475	struct sta_info *sta = timeout_ctx;
476
477	if (!(sta->flags & WLAN_STA_AUTH)) {
478		if (sta->flags & WLAN_STA_GAS) {
479			wpa_printf(MSG_DEBUG, "GAS: Remove temporary STA "
480				   "entry " MACSTR, MAC2STR(sta->addr));
481			ap_free_sta(hapd, sta);
482		}
483		return;
484	}
485
486	hostapd_drv_sta_deauth(hapd, sta->addr,
487			       WLAN_REASON_PREV_AUTH_NOT_VALID);
488	mlme_deauthenticate_indication(hapd, sta,
489				       WLAN_REASON_PREV_AUTH_NOT_VALID);
490	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
491		       HOSTAPD_LEVEL_INFO, "deauthenticated due to "
492		       "session timeout");
493	sta->acct_terminate_cause =
494		RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT;
495	ap_free_sta(hapd, sta);
496}
497
498
499void ap_sta_replenish_timeout(struct hostapd_data *hapd, struct sta_info *sta,
500			      u32 session_timeout)
501{
502	if (eloop_replenish_timeout(session_timeout, 0,
503				    ap_handle_session_timer, hapd, sta) == 1) {
504		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
505			       HOSTAPD_LEVEL_DEBUG, "setting session timeout "
506			       "to %d seconds", session_timeout);
507	}
508}
509
510
511void ap_sta_session_timeout(struct hostapd_data *hapd, struct sta_info *sta,
512			    u32 session_timeout)
513{
514	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
515		       HOSTAPD_LEVEL_DEBUG, "setting session timeout to %d "
516		       "seconds", session_timeout);
517	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
518	eloop_register_timeout(session_timeout, 0, ap_handle_session_timer,
519			       hapd, sta);
520}
521
522
523void ap_sta_no_session_timeout(struct hostapd_data *hapd, struct sta_info *sta)
524{
525	eloop_cancel_timeout(ap_handle_session_timer, hapd, sta);
526}
527
528
529static void ap_handle_session_warning_timer(void *eloop_ctx, void *timeout_ctx)
530{
531#ifdef CONFIG_WNM
532	struct hostapd_data *hapd = eloop_ctx;
533	struct sta_info *sta = timeout_ctx;
534
535	wpa_printf(MSG_DEBUG, "WNM: Session warning time reached for " MACSTR,
536		   MAC2STR(sta->addr));
537	if (sta->hs20_session_info_url == NULL)
538		return;
539
540	wnm_send_ess_disassoc_imminent(hapd, sta, sta->hs20_session_info_url,
541				       sta->hs20_disassoc_timer);
542#endif /* CONFIG_WNM */
543}
544
545
546void ap_sta_session_warning_timeout(struct hostapd_data *hapd,
547				    struct sta_info *sta, int warning_time)
548{
549	eloop_cancel_timeout(ap_handle_session_warning_timer, hapd, sta);
550	eloop_register_timeout(warning_time, 0, ap_handle_session_warning_timer,
551			       hapd, sta);
552}
553
554
555struct sta_info * ap_sta_add(struct hostapd_data *hapd, const u8 *addr)
556{
557	struct sta_info *sta;
558
559	sta = ap_get_sta(hapd, addr);
560	if (sta)
561		return sta;
562
563	wpa_printf(MSG_DEBUG, "  New STA");
564	if (hapd->num_sta >= hapd->conf->max_num_sta) {
565		/* FIX: might try to remove some old STAs first? */
566		wpa_printf(MSG_DEBUG, "no more room for new STAs (%d/%d)",
567			   hapd->num_sta, hapd->conf->max_num_sta);
568		return NULL;
569	}
570
571	sta = os_zalloc(sizeof(struct sta_info));
572	if (sta == NULL) {
573		wpa_printf(MSG_ERROR, "malloc failed");
574		return NULL;
575	}
576	sta->acct_interim_interval = hapd->conf->acct_interim_interval;
577	accounting_sta_get_id(hapd, sta);
578
579	if (!(hapd->iface->drv_flags & WPA_DRIVER_FLAGS_INACTIVITY_TIMER)) {
580		wpa_printf(MSG_DEBUG, "%s: register ap_handle_timer timeout "
581			   "for " MACSTR " (%d seconds - ap_max_inactivity)",
582			   __func__, MAC2STR(addr),
583			   hapd->conf->ap_max_inactivity);
584		eloop_register_timeout(hapd->conf->ap_max_inactivity, 0,
585				       ap_handle_timer, hapd, sta);
586	}
587
588	/* initialize STA info data */
589	os_memcpy(sta->addr, addr, ETH_ALEN);
590	sta->next = hapd->sta_list;
591	hapd->sta_list = sta;
592	hapd->num_sta++;
593	ap_sta_hash_add(hapd, sta);
594	sta->ssid = &hapd->conf->ssid;
595	ap_sta_remove_in_other_bss(hapd, sta);
596
597	return sta;
598}
599
600
601static int ap_sta_remove(struct hostapd_data *hapd, struct sta_info *sta)
602{
603	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
604
605	wpa_printf(MSG_DEBUG, "Removing STA " MACSTR " from kernel driver",
606		   MAC2STR(sta->addr));
607	if (hostapd_drv_sta_remove(hapd, sta->addr) &&
608	    sta->flags & WLAN_STA_ASSOC) {
609		wpa_printf(MSG_DEBUG, "Could not remove station " MACSTR
610			   " from kernel driver.", MAC2STR(sta->addr));
611		return -1;
612	}
613	return 0;
614}
615
616
617static void ap_sta_remove_in_other_bss(struct hostapd_data *hapd,
618				       struct sta_info *sta)
619{
620	struct hostapd_iface *iface = hapd->iface;
621	size_t i;
622
623	for (i = 0; i < iface->num_bss; i++) {
624		struct hostapd_data *bss = iface->bss[i];
625		struct sta_info *sta2;
626		/* bss should always be set during operation, but it may be
627		 * NULL during reconfiguration. Assume the STA is not
628		 * associated to another BSS in that case to avoid NULL pointer
629		 * dereferences. */
630		if (bss == hapd || bss == NULL)
631			continue;
632		sta2 = ap_get_sta(bss, sta->addr);
633		if (!sta2)
634			continue;
635
636		ap_sta_disconnect(bss, sta2, sta2->addr,
637				  WLAN_REASON_PREV_AUTH_NOT_VALID);
638	}
639}
640
641
642static void ap_sta_disassoc_cb_timeout(void *eloop_ctx, void *timeout_ctx)
643{
644	struct hostapd_data *hapd = eloop_ctx;
645	struct sta_info *sta = timeout_ctx;
646
647	ap_sta_remove(hapd, sta);
648	mlme_disassociate_indication(hapd, sta, sta->disassoc_reason);
649}
650
651
652void ap_sta_disassociate(struct hostapd_data *hapd, struct sta_info *sta,
653			 u16 reason)
654{
655	wpa_printf(MSG_DEBUG, "%s: disassociate STA " MACSTR,
656		   hapd->conf->iface, MAC2STR(sta->addr));
657	sta->flags &= ~(WLAN_STA_ASSOC | WLAN_STA_ASSOC_REQ_OK);
658	ap_sta_set_authorized(hapd, sta, 0);
659	sta->timeout_next = STA_DEAUTH;
660	wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
661		   "for " MACSTR " (%d seconds - "
662		   "AP_MAX_INACTIVITY_AFTER_DISASSOC)",
663		   __func__, MAC2STR(sta->addr),
664		   AP_MAX_INACTIVITY_AFTER_DISASSOC);
665	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
666	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DISASSOC, 0,
667			       ap_handle_timer, hapd, sta);
668	accounting_sta_stop(hapd, sta);
669	ieee802_1x_free_station(sta);
670
671	sta->disassoc_reason = reason;
672	sta->flags |= WLAN_STA_PENDING_DISASSOC_CB;
673	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
674	eloop_register_timeout(hapd->iface->drv_flags &
675			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
676			       ap_sta_disassoc_cb_timeout, hapd, sta);
677}
678
679
680static void ap_sta_deauth_cb_timeout(void *eloop_ctx, void *timeout_ctx)
681{
682	struct hostapd_data *hapd = eloop_ctx;
683	struct sta_info *sta = timeout_ctx;
684
685	ap_sta_remove(hapd, sta);
686	mlme_deauthenticate_indication(hapd, sta, sta->deauth_reason);
687}
688
689
690void ap_sta_deauthenticate(struct hostapd_data *hapd, struct sta_info *sta,
691			   u16 reason)
692{
693	wpa_printf(MSG_DEBUG, "%s: deauthenticate STA " MACSTR,
694		   hapd->conf->iface, MAC2STR(sta->addr));
695	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
696	ap_sta_set_authorized(hapd, sta, 0);
697	sta->timeout_next = STA_REMOVE;
698	wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
699		   "for " MACSTR " (%d seconds - "
700		   "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
701		   __func__, MAC2STR(sta->addr),
702		   AP_MAX_INACTIVITY_AFTER_DEAUTH);
703	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
704	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
705			       ap_handle_timer, hapd, sta);
706	accounting_sta_stop(hapd, sta);
707	ieee802_1x_free_station(sta);
708
709	sta->deauth_reason = reason;
710	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
711	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
712	eloop_register_timeout(hapd->iface->drv_flags &
713			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
714			       ap_sta_deauth_cb_timeout, hapd, sta);
715}
716
717
718#ifdef CONFIG_WPS
719int ap_sta_wps_cancel(struct hostapd_data *hapd,
720		      struct sta_info *sta, void *ctx)
721{
722	if (sta && (sta->flags & WLAN_STA_WPS)) {
723		ap_sta_deauthenticate(hapd, sta,
724				      WLAN_REASON_PREV_AUTH_NOT_VALID);
725		wpa_printf(MSG_DEBUG, "WPS: %s: Deauth sta=" MACSTR,
726			   __func__, MAC2STR(sta->addr));
727		return 1;
728	}
729
730	return 0;
731}
732#endif /* CONFIG_WPS */
733
734
735int ap_sta_bind_vlan(struct hostapd_data *hapd, struct sta_info *sta,
736		     int old_vlanid)
737{
738#ifndef CONFIG_NO_VLAN
739	const char *iface;
740	struct hostapd_vlan *vlan = NULL;
741	int ret;
742
743	/*
744	 * Do not proceed furthur if the vlan id remains same. We do not want
745	 * duplicate dynamic vlan entries.
746	 */
747	if (sta->vlan_id == old_vlanid)
748		return 0;
749
750	/*
751	 * During 1x reauth, if the vlan id changes, then remove the old id and
752	 * proceed furthur to add the new one.
753	 */
754	if (old_vlanid > 0)
755		vlan_remove_dynamic(hapd, old_vlanid);
756
757	iface = hapd->conf->iface;
758	if (sta->ssid->vlan[0])
759		iface = sta->ssid->vlan;
760
761	if (sta->ssid->dynamic_vlan == DYNAMIC_VLAN_DISABLED)
762		sta->vlan_id = 0;
763	else if (sta->vlan_id > 0) {
764		struct hostapd_vlan *wildcard_vlan = NULL;
765		vlan = hapd->conf->vlan;
766		while (vlan) {
767			if (vlan->vlan_id == sta->vlan_id)
768				break;
769			if (vlan->vlan_id == VLAN_ID_WILDCARD)
770				wildcard_vlan = vlan;
771			vlan = vlan->next;
772		}
773		if (!vlan)
774			vlan = wildcard_vlan;
775		if (vlan)
776			iface = vlan->ifname;
777	}
778
779	if (sta->vlan_id > 0 && vlan == NULL) {
780		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
781			       HOSTAPD_LEVEL_DEBUG, "could not find VLAN for "
782			       "binding station to (vlan_id=%d)",
783			       sta->vlan_id);
784		return -1;
785	} else if (sta->vlan_id > 0 && vlan->vlan_id == VLAN_ID_WILDCARD) {
786		vlan = vlan_add_dynamic(hapd, vlan, sta->vlan_id);
787		if (vlan == NULL) {
788			hostapd_logger(hapd, sta->addr,
789				       HOSTAPD_MODULE_IEEE80211,
790				       HOSTAPD_LEVEL_DEBUG, "could not add "
791				       "dynamic VLAN interface for vlan_id=%d",
792				       sta->vlan_id);
793			return -1;
794		}
795
796		iface = vlan->ifname;
797		if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
798			hostapd_logger(hapd, sta->addr,
799				       HOSTAPD_MODULE_IEEE80211,
800				       HOSTAPD_LEVEL_DEBUG, "could not "
801				       "configure encryption for dynamic VLAN "
802				       "interface for vlan_id=%d",
803				       sta->vlan_id);
804		}
805
806		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
807			       HOSTAPD_LEVEL_DEBUG, "added new dynamic VLAN "
808			       "interface '%s'", iface);
809	} else if (vlan && vlan->vlan_id == sta->vlan_id) {
810		if (sta->vlan_id > 0) {
811			vlan->dynamic_vlan++;
812			hostapd_logger(hapd, sta->addr,
813				       HOSTAPD_MODULE_IEEE80211,
814				       HOSTAPD_LEVEL_DEBUG, "updated existing "
815				       "dynamic VLAN interface '%s'", iface);
816		}
817
818		/*
819		 * Update encryption configuration for statically generated
820		 * VLAN interface. This is only used for static WEP
821		 * configuration for the case where hostapd did not yet know
822		 * which keys are to be used when the interface was added.
823		 */
824		if (vlan_setup_encryption_dyn(hapd, sta->ssid, iface) != 0) {
825			hostapd_logger(hapd, sta->addr,
826				       HOSTAPD_MODULE_IEEE80211,
827				       HOSTAPD_LEVEL_DEBUG, "could not "
828				       "configure encryption for VLAN "
829				       "interface for vlan_id=%d",
830				       sta->vlan_id);
831		}
832	}
833
834	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
835		       HOSTAPD_LEVEL_DEBUG, "binding station to interface "
836		       "'%s'", iface);
837
838	if (wpa_auth_sta_set_vlan(sta->wpa_sm, sta->vlan_id) < 0)
839		wpa_printf(MSG_INFO, "Failed to update VLAN-ID for WPA");
840
841	ret = hostapd_drv_set_sta_vlan(iface, hapd, sta->addr, sta->vlan_id);
842	if (ret < 0) {
843		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
844			       HOSTAPD_LEVEL_DEBUG, "could not bind the STA "
845			       "entry to vlan_id=%d", sta->vlan_id);
846	}
847	return ret;
848#else /* CONFIG_NO_VLAN */
849	return 0;
850#endif /* CONFIG_NO_VLAN */
851}
852
853
854#ifdef CONFIG_IEEE80211W
855
856int ap_check_sa_query_timeout(struct hostapd_data *hapd, struct sta_info *sta)
857{
858	u32 tu;
859	struct os_reltime now, passed;
860	os_get_reltime(&now);
861	os_reltime_sub(&now, &sta->sa_query_start, &passed);
862	tu = (passed.sec * 1000000 + passed.usec) / 1024;
863	if (hapd->conf->assoc_sa_query_max_timeout < tu) {
864		hostapd_logger(hapd, sta->addr,
865			       HOSTAPD_MODULE_IEEE80211,
866			       HOSTAPD_LEVEL_DEBUG,
867			       "association SA Query timed out");
868		sta->sa_query_timed_out = 1;
869		os_free(sta->sa_query_trans_id);
870		sta->sa_query_trans_id = NULL;
871		sta->sa_query_count = 0;
872		eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
873		return 1;
874	}
875
876	return 0;
877}
878
879
880static void ap_sa_query_timer(void *eloop_ctx, void *timeout_ctx)
881{
882	struct hostapd_data *hapd = eloop_ctx;
883	struct sta_info *sta = timeout_ctx;
884	unsigned int timeout, sec, usec;
885	u8 *trans_id, *nbuf;
886
887	if (sta->sa_query_count > 0 &&
888	    ap_check_sa_query_timeout(hapd, sta))
889		return;
890
891	nbuf = os_realloc_array(sta->sa_query_trans_id,
892				sta->sa_query_count + 1,
893				WLAN_SA_QUERY_TR_ID_LEN);
894	if (nbuf == NULL)
895		return;
896	if (sta->sa_query_count == 0) {
897		/* Starting a new SA Query procedure */
898		os_get_reltime(&sta->sa_query_start);
899	}
900	trans_id = nbuf + sta->sa_query_count * WLAN_SA_QUERY_TR_ID_LEN;
901	sta->sa_query_trans_id = nbuf;
902	sta->sa_query_count++;
903
904	os_get_random(trans_id, WLAN_SA_QUERY_TR_ID_LEN);
905
906	timeout = hapd->conf->assoc_sa_query_retry_timeout;
907	sec = ((timeout / 1000) * 1024) / 1000;
908	usec = (timeout % 1000) * 1024;
909	eloop_register_timeout(sec, usec, ap_sa_query_timer, hapd, sta);
910
911	hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
912		       HOSTAPD_LEVEL_DEBUG,
913		       "association SA Query attempt %d", sta->sa_query_count);
914
915	ieee802_11_send_sa_query_req(hapd, sta->addr, trans_id);
916}
917
918
919void ap_sta_start_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
920{
921	ap_sa_query_timer(hapd, sta);
922}
923
924
925void ap_sta_stop_sa_query(struct hostapd_data *hapd, struct sta_info *sta)
926{
927	eloop_cancel_timeout(ap_sa_query_timer, hapd, sta);
928	os_free(sta->sa_query_trans_id);
929	sta->sa_query_trans_id = NULL;
930	sta->sa_query_count = 0;
931}
932
933#endif /* CONFIG_IEEE80211W */
934
935
936void ap_sta_set_authorized(struct hostapd_data *hapd, struct sta_info *sta,
937			   int authorized)
938{
939	const u8 *dev_addr = NULL;
940	char buf[100];
941#ifdef CONFIG_P2P
942	u8 addr[ETH_ALEN];
943	u8 ip_addr_buf[4];
944#endif /* CONFIG_P2P */
945
946	if (!!authorized == !!(sta->flags & WLAN_STA_AUTHORIZED))
947		return;
948
949#ifdef CONFIG_P2P
950	if (hapd->p2p_group == NULL) {
951		if (sta->p2p_ie != NULL &&
952		    p2p_parse_dev_addr_in_p2p_ie(sta->p2p_ie, addr) == 0)
953			dev_addr = addr;
954	} else
955		dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr);
956#endif /* CONFIG_P2P */
957
958	if (dev_addr)
959		os_snprintf(buf, sizeof(buf), MACSTR " p2p_dev_addr=" MACSTR,
960			    MAC2STR(sta->addr), MAC2STR(dev_addr));
961	else
962		os_snprintf(buf, sizeof(buf), MACSTR, MAC2STR(sta->addr));
963
964	if (authorized) {
965		char ip_addr[100];
966		ip_addr[0] = '\0';
967#ifdef CONFIG_P2P
968		if (wpa_auth_get_ip_addr(sta->wpa_sm, ip_addr_buf) == 0) {
969			os_snprintf(ip_addr, sizeof(ip_addr),
970				    " ip_addr=%u.%u.%u.%u",
971				    ip_addr_buf[0], ip_addr_buf[1],
972				    ip_addr_buf[2], ip_addr_buf[3]);
973		}
974#endif /* CONFIG_P2P */
975
976		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_CONNECTED "%s%s",
977			buf, ip_addr);
978
979		if (hapd->msg_ctx_parent &&
980		    hapd->msg_ctx_parent != hapd->msg_ctx)
981			wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
982					  AP_STA_CONNECTED "%s%s",
983					  buf, ip_addr);
984
985		sta->flags |= WLAN_STA_AUTHORIZED;
986	} else {
987		wpa_msg(hapd->msg_ctx, MSG_INFO, AP_STA_DISCONNECTED "%s", buf);
988
989		if (hapd->msg_ctx_parent &&
990		    hapd->msg_ctx_parent != hapd->msg_ctx)
991			wpa_msg_no_global(hapd->msg_ctx_parent, MSG_INFO,
992					  AP_STA_DISCONNECTED "%s", buf);
993
994		sta->flags &= ~WLAN_STA_AUTHORIZED;
995	}
996
997	if (hapd->sta_authorized_cb)
998		hapd->sta_authorized_cb(hapd->sta_authorized_cb_ctx,
999					sta->addr, authorized, dev_addr);
1000}
1001
1002
1003void ap_sta_disconnect(struct hostapd_data *hapd, struct sta_info *sta,
1004		       const u8 *addr, u16 reason)
1005{
1006
1007	if (sta == NULL && addr)
1008		sta = ap_get_sta(hapd, addr);
1009
1010	if (addr)
1011		hostapd_drv_sta_deauth(hapd, addr, reason);
1012
1013	if (sta == NULL)
1014		return;
1015	ap_sta_set_authorized(hapd, sta, 0);
1016	wpa_auth_sm_event(sta->wpa_sm, WPA_DEAUTH);
1017	ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
1018	sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
1019	wpa_printf(MSG_DEBUG, "%s: reschedule ap_handle_timer timeout "
1020		   "for " MACSTR " (%d seconds - "
1021		   "AP_MAX_INACTIVITY_AFTER_DEAUTH)",
1022		   __func__, MAC2STR(sta->addr),
1023		   AP_MAX_INACTIVITY_AFTER_DEAUTH);
1024	eloop_cancel_timeout(ap_handle_timer, hapd, sta);
1025	eloop_register_timeout(AP_MAX_INACTIVITY_AFTER_DEAUTH, 0,
1026			       ap_handle_timer, hapd, sta);
1027	sta->timeout_next = STA_REMOVE;
1028
1029	sta->deauth_reason = reason;
1030	sta->flags |= WLAN_STA_PENDING_DEAUTH_CB;
1031	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1032	eloop_register_timeout(hapd->iface->drv_flags &
1033			       WPA_DRIVER_FLAGS_DEAUTH_TX_STATUS ? 2 : 0, 0,
1034			       ap_sta_deauth_cb_timeout, hapd, sta);
1035}
1036
1037
1038void ap_sta_deauth_cb(struct hostapd_data *hapd, struct sta_info *sta)
1039{
1040	if (!(sta->flags & WLAN_STA_PENDING_DEAUTH_CB)) {
1041		wpa_printf(MSG_DEBUG, "Ignore deauth cb for test frame");
1042		return;
1043	}
1044	sta->flags &= ~WLAN_STA_PENDING_DEAUTH_CB;
1045	eloop_cancel_timeout(ap_sta_deauth_cb_timeout, hapd, sta);
1046	ap_sta_deauth_cb_timeout(hapd, sta);
1047}
1048
1049
1050void ap_sta_disassoc_cb(struct hostapd_data *hapd, struct sta_info *sta)
1051{
1052	if (!(sta->flags & WLAN_STA_PENDING_DISASSOC_CB)) {
1053		wpa_printf(MSG_DEBUG, "Ignore disassoc cb for test frame");
1054		return;
1055	}
1056	sta->flags &= ~WLAN_STA_PENDING_DISASSOC_CB;
1057	eloop_cancel_timeout(ap_sta_disassoc_cb_timeout, hapd, sta);
1058	ap_sta_disassoc_cb_timeout(hapd, sta);
1059}
1060
1061
1062int ap_sta_flags_txt(u32 flags, char *buf, size_t buflen)
1063{
1064	int res;
1065
1066	buf[0] = '\0';
1067	res = os_snprintf(buf, buflen, "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1068			  (flags & WLAN_STA_AUTH ? "[AUTH]" : ""),
1069			  (flags & WLAN_STA_ASSOC ? "[ASSOC]" : ""),
1070			  (flags & WLAN_STA_AUTHORIZED ? "[AUTHORIZED]" : ""),
1071			  (flags & WLAN_STA_PENDING_POLL ? "[PENDING_POLL" :
1072			   ""),
1073			  (flags & WLAN_STA_SHORT_PREAMBLE ?
1074			   "[SHORT_PREAMBLE]" : ""),
1075			  (flags & WLAN_STA_PREAUTH ? "[PREAUTH]" : ""),
1076			  (flags & WLAN_STA_WMM ? "[WMM]" : ""),
1077			  (flags & WLAN_STA_MFP ? "[MFP]" : ""),
1078			  (flags & WLAN_STA_WPS ? "[WPS]" : ""),
1079			  (flags & WLAN_STA_MAYBE_WPS ? "[MAYBE_WPS]" : ""),
1080			  (flags & WLAN_STA_WDS ? "[WDS]" : ""),
1081			  (flags & WLAN_STA_NONERP ? "[NonERP]" : ""),
1082			  (flags & WLAN_STA_WPS2 ? "[WPS2]" : ""),
1083			  (flags & WLAN_STA_GAS ? "[GAS]" : ""),
1084			  (flags & WLAN_STA_VHT ? "[VHT]" : ""),
1085			  (flags & WLAN_STA_WNM_SLEEP_MODE ?
1086			   "[WNM_SLEEP_MODE]" : ""));
1087
1088	return res;
1089}
1090