blob: 6ecd094ac3b4e681b9109c26c914642331ea70ca [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Callback functions for driver wrappers
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07003 * Copyright (c) 2002-2013, Jouni Malinen <j@w1.fi>
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004 *
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08005 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07007 */
8
9#include "utils/includes.h"
10
11#include "utils/common.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070012#include "utils/eloop.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070013#include "radius/radius.h"
14#include "drivers/driver.h"
15#include "common/ieee802_11_defs.h"
16#include "common/ieee802_11_common.h"
Dmitry Shmidtf8623282013-02-20 14:34:59 -080017#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070018#include "crypto/random.h"
19#include "p2p/p2p.h"
20#include "wps/wps.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070021#include "wnm_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070022#include "hostapd.h"
23#include "ieee802_11.h"
24#include "sta_info.h"
25#include "accounting.h"
26#include "tkip_countermeasures.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070027#include "ieee802_1x.h"
28#include "wpa_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070029#include "wps_hostapd.h"
30#include "ap_drv_ops.h"
31#include "ap_config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070032#include "hw_features.h"
Dmitry Shmidt051af732013-10-22 13:52:46 -070033#include "dfs.h"
Dmitry Shmidt7832adb2014-04-29 10:53:02 -070034#include "beacon.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070035
36
37int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080038 const u8 *req_ies, size_t req_ies_len, int reassoc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070039{
40 struct sta_info *sta;
41 int new_assoc, res;
42 struct ieee802_11_elems elems;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080043 const u8 *ie;
44 size_t ielen;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070045#ifdef CONFIG_IEEE80211R
46 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
47 u8 *p = buf;
48#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080049 u16 reason = WLAN_REASON_UNSPECIFIED;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070050 u16 status = WLAN_STATUS_SUCCESS;
Dmitry Shmidt391c59f2013-09-03 12:16:28 -070051 const u8 *p2p_dev_addr = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070052
53 if (addr == NULL) {
54 /*
55 * This could potentially happen with unexpected event from the
56 * driver wrapper. This was seen at least in one case where the
57 * driver ended up being set to station mode while hostapd was
58 * running, so better make sure we stop processing such an
59 * event here.
60 */
61 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
62 "no address");
63 return -1;
64 }
65 random_add_randomness(addr, ETH_ALEN);
66
67 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
68 HOSTAPD_LEVEL_INFO, "associated");
69
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080070 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070071 if (elems.wps_ie) {
72 ie = elems.wps_ie - 2;
73 ielen = elems.wps_ie_len + 2;
74 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
75 } else if (elems.rsn_ie) {
76 ie = elems.rsn_ie - 2;
77 ielen = elems.rsn_ie_len + 2;
78 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
79 } else if (elems.wpa_ie) {
80 ie = elems.wpa_ie - 2;
81 ielen = elems.wpa_ie_len + 2;
82 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
Dmitry Shmidtf21452a2014-02-26 10:55:25 -080083#ifdef CONFIG_HS20
84 } else if (elems.osen) {
85 ie = elems.osen - 2;
86 ielen = elems.osen_len + 2;
87 wpa_printf(MSG_DEBUG, "STA included OSEN IE in (Re)AssocReq");
88#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 } else {
90 ie = NULL;
91 ielen = 0;
92 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
93 "(Re)AssocReq");
94 }
95
96 sta = ap_get_sta(hapd, addr);
97 if (sta) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070098 ap_sta_no_session_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070099 accounting_sta_stop(hapd, sta);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700100
101 /*
102 * Make sure that the previously registered inactivity timer
103 * will not remove the STA immediately.
104 */
105 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700106 } else {
107 sta = ap_sta_add(hapd, addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700108 if (sta == NULL) {
109 hostapd_drv_sta_disassoc(hapd, addr,
110 WLAN_REASON_DISASSOC_AP_BUSY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700111 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700112 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700113 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800114 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700115
116#ifdef CONFIG_P2P
117 if (elems.p2p) {
118 wpabuf_free(sta->p2p_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800119 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700120 P2P_IE_VENDOR_TYPE);
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700121 if (sta->p2p_ie)
122 p2p_dev_addr = p2p_get_go_dev_addr(sta->p2p_ie);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 }
124#endif /* CONFIG_P2P */
125
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700126#ifdef CONFIG_IEEE80211N
127#ifdef NEED_AP_MLME
128 if (elems.ht_capabilities &&
Dmitry Shmidt7832adb2014-04-29 10:53:02 -0700129 (hapd->iface->conf->ht_capab &
130 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
131 struct ieee80211_ht_capabilities *ht_cap =
132 (struct ieee80211_ht_capabilities *)
133 elems.ht_capabilities;
134
135 if (le_to_host16(ht_cap->ht_capabilities_info) &
136 HT_CAP_INFO_40MHZ_INTOLERANT)
137 ht40_intolerant_add(hapd->iface, sta);
138 }
139#endif /* NEED_AP_MLME */
140#endif /* CONFIG_IEEE80211N */
141
Dmitry Shmidt051af732013-10-22 13:52:46 -0700142#ifdef CONFIG_INTERWORKING
143 if (elems.ext_capab && elems.ext_capab_len > 4) {
144 if (elems.ext_capab[4] & 0x01)
145 sta->qos_map_enabled = 1;
146 }
147#endif /* CONFIG_INTERWORKING */
148
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800149#ifdef CONFIG_HS20
150 wpabuf_free(sta->hs20_ie);
151 if (elems.hs20 && elems.hs20_len > 4) {
152 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
153 elems.hs20_len - 4);
154 } else
155 sta->hs20_ie = NULL;
156#endif /* CONFIG_HS20 */
157
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700158 if (hapd->conf->wpa) {
159 if (ie == NULL || ielen == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800160#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700161 if (hapd->conf->wps_state) {
162 wpa_printf(MSG_DEBUG, "STA did not include "
163 "WPA/RSN IE in (Re)Association "
164 "Request - possible WPS use");
165 sta->flags |= WLAN_STA_MAYBE_WPS;
166 goto skip_wpa_check;
167 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800168#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169
170 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
171 return -1;
172 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800173#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700174 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
175 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176 struct wpabuf *wps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 sta->flags |= WLAN_STA_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 wps = ieee802_11_vendor_ie_concat(ie, ielen,
179 WPS_IE_VENDOR_TYPE);
180 if (wps) {
181 if (wps_is_20(wps)) {
182 wpa_printf(MSG_DEBUG, "WPS: STA "
183 "supports WPS 2.0");
184 sta->flags |= WLAN_STA_WPS2;
185 }
186 wpabuf_free(wps);
187 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700188 goto skip_wpa_check;
189 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800190#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700191
192 if (sta->wpa_sm == NULL)
193 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700194 sta->addr,
195 p2p_dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 if (sta->wpa_sm == NULL) {
197 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
198 "machine");
199 return -1;
200 }
201 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700202 ie, ielen,
203 elems.mdie, elems.mdie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700204 if (res != WPA_IE_OK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700205 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
206 "rejected? (res %u)", res);
207 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700208 if (res == WPA_INVALID_GROUP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800209 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700210 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
211 } else if (res == WPA_INVALID_PAIRWISE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800212 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700213 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
214 } else if (res == WPA_INVALID_AKMP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800215 reason = WLAN_REASON_AKMP_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700216 status = WLAN_STATUS_AKMP_NOT_VALID;
217 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700218#ifdef CONFIG_IEEE80211W
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700219 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800220 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700221 status = WLAN_STATUS_INVALID_IE;
222 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800223 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700224 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700226#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700227 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800228 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700229 status = WLAN_STATUS_INVALID_IE;
230 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800231 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700232 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700233#ifdef CONFIG_IEEE80211W
234 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
235 sta->sa_query_count > 0)
236 ap_check_sa_query_timeout(hapd, sta);
237 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
238 (sta->auth_alg != WLAN_AUTH_FT)) {
239 /*
240 * STA has already been associated with MFP and SA
241 * Query timeout has not been reached. Reject the
242 * association attempt temporarily and start SA Query,
243 * if one is not pending.
244 */
245
246 if (sta->sa_query_count == 0)
247 ap_sta_start_sa_query(hapd, sta);
248
249#ifdef CONFIG_IEEE80211R
250 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
251
252 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
253
254 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
255 p - buf);
256#endif /* CONFIG_IEEE80211R */
257 return 0;
258 }
259
260 if (wpa_auth_uses_mfp(sta->wpa_sm))
261 sta->flags |= WLAN_STA_MFP;
262 else
263 sta->flags &= ~WLAN_STA_MFP;
264#endif /* CONFIG_IEEE80211W */
265
266#ifdef CONFIG_IEEE80211R
267 if (sta->auth_alg == WLAN_AUTH_FT) {
268 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
269 req_ies_len);
270 if (status != WLAN_STATUS_SUCCESS) {
271 if (status == WLAN_STATUS_INVALID_PMKID)
272 reason = WLAN_REASON_INVALID_IE;
273 if (status == WLAN_STATUS_INVALID_MDIE)
274 reason = WLAN_REASON_INVALID_IE;
275 if (status == WLAN_STATUS_INVALID_FTIE)
276 reason = WLAN_REASON_INVALID_IE;
277 goto fail;
278 }
279 }
280#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700281 } else if (hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800282#ifdef CONFIG_WPS
283 struct wpabuf *wps;
284 if (req_ies)
285 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700286 WPS_IE_VENDOR_TYPE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800287 else
288 wps = NULL;
289#ifdef CONFIG_WPS_STRICT
290 if (wps && wps_validate_assoc_req(wps) < 0) {
291 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700292 status = WLAN_STATUS_INVALID_IE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 wpabuf_free(wps);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800294 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 }
296#endif /* CONFIG_WPS_STRICT */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800297 if (wps) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700298 sta->flags |= WLAN_STA_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800299 if (wps_is_20(wps)) {
300 wpa_printf(MSG_DEBUG, "WPS: STA supports "
301 "WPS 2.0");
302 sta->flags |= WLAN_STA_WPS2;
303 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700304 } else
305 sta->flags |= WLAN_STA_MAYBE_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800306 wpabuf_free(wps);
307#endif /* CONFIG_WPS */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800308#ifdef CONFIG_HS20
309 } else if (hapd->conf->osen) {
310 if (elems.osen == NULL) {
311 hostapd_logger(
312 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
313 HOSTAPD_LEVEL_INFO,
314 "No HS 2.0 OSEN element in association request");
315 return WLAN_STATUS_INVALID_IE;
316 }
317
318 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
319 if (sta->wpa_sm == NULL)
320 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
321 sta->addr, NULL);
322 if (sta->wpa_sm == NULL) {
323 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
324 "state machine");
325 return WLAN_STATUS_UNSPECIFIED_FAILURE;
326 }
327 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
328 elems.osen - 2, elems.osen_len + 2) < 0)
329 return WLAN_STATUS_INVALID_IE;
330#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700331 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800332#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333skip_wpa_check:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700336#ifdef CONFIG_IEEE80211R
337 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
338 sta->auth_alg, req_ies, req_ies_len);
339
340 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700341
342 if (sta->auth_alg == WLAN_AUTH_FT)
343 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700344#else /* CONFIG_IEEE80211R */
345 /* Keep compiler silent about unused variables */
346 if (status) {
347 }
348#endif /* CONFIG_IEEE80211R */
349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700350 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
351 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800352 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700353
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700354 hostapd_set_sta_flags(hapd, sta);
355
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700356 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
357 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
358 else
359 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700360
361 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
362
363 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
364
365#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800366 if (req_ies) {
367 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
368 req_ies, req_ies_len);
369 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700370#endif /* CONFIG_P2P */
371
372 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800373
374fail:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700375#ifdef CONFIG_IEEE80211R
376 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
377#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800378 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
379 ap_free_sta(hapd, sta);
380 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700381}
382
383
384void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
385{
386 struct sta_info *sta;
387
388 if (addr == NULL) {
389 /*
390 * This could potentially happen with unexpected event from the
391 * driver wrapper. This was seen at least in one case where the
392 * driver ended up reporting a station mode event while hostapd
393 * was running, so better make sure we stop processing such an
394 * event here.
395 */
396 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
397 "with no address");
398 return;
399 }
400
401 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
402 HOSTAPD_LEVEL_INFO, "disassociated");
403
404 sta = ap_get_sta(hapd, addr);
405 if (sta == NULL) {
406 wpa_printf(MSG_DEBUG, "Disassociation notification for "
407 "unknown STA " MACSTR, MAC2STR(addr));
408 return;
409 }
410
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800411 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700412 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700413 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
414 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
415 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
416 ap_free_sta(hapd, sta);
417}
418
419
420void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
421{
422 struct sta_info *sta = ap_get_sta(hapd, addr);
423
424 if (!sta || !hapd->conf->disassoc_low_ack)
425 return;
426
427 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
428 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
429 "missing ACKs");
430 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
431 if (sta)
432 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
433}
434
435
Dmitry Shmidt04949592012-07-19 12:16:46 -0700436void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800437 int offset, int width, int cf1, int cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700438{
439#ifdef NEED_AP_MLME
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800440 int channel, chwidth, seg0_idx = 0, seg1_idx = 0, is_dfs;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700441
442 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800443 HOSTAPD_LEVEL_INFO,
444 "driver had channel switch: freq=%d, ht=%d, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
445 freq, ht, offset, width, channel_width_to_string(width),
446 cf1, cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700447
448 hapd->iface->freq = freq;
449
450 channel = hostapd_hw_get_channel(hapd, freq);
451 if (!channel) {
452 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
453 HOSTAPD_LEVEL_WARNING, "driver switched to "
454 "bad channel!");
455 return;
456 }
457
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800458 switch (width) {
459 case CHAN_WIDTH_80:
460 chwidth = VHT_CHANWIDTH_80MHZ;
461 break;
462 case CHAN_WIDTH_80P80:
463 chwidth = VHT_CHANWIDTH_80P80MHZ;
464 break;
465 case CHAN_WIDTH_160:
466 chwidth = VHT_CHANWIDTH_160MHZ;
467 break;
468 case CHAN_WIDTH_20_NOHT:
469 case CHAN_WIDTH_20:
470 case CHAN_WIDTH_40:
471 default:
472 chwidth = VHT_CHANWIDTH_USE_HT;
473 break;
474 }
475
476 switch (hapd->iface->current_mode->mode) {
477 case HOSTAPD_MODE_IEEE80211A:
478 if (cf1 > 5000)
479 seg0_idx = (cf1 - 5000) / 5;
480 if (cf2 > 5000)
481 seg1_idx = (cf2 - 5000) / 5;
482 break;
483 default:
484 seg0_idx = hostapd_hw_get_channel(hapd, cf1);
485 seg1_idx = hostapd_hw_get_channel(hapd, cf2);
486 break;
487 }
488
Dmitry Shmidt04949592012-07-19 12:16:46 -0700489 hapd->iconf->channel = channel;
490 hapd->iconf->ieee80211n = ht;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800491 if (!ht)
492 hapd->iconf->ieee80211ac = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700493 hapd->iconf->secondary_channel = offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800494 hapd->iconf->vht_oper_chwidth = chwidth;
495 hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
496 hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800497
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800498 is_dfs = ieee80211_is_dfs(freq);
499
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700500 if (hapd->csa_in_progress &&
501 freq == hapd->cs_freq_params.freq) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800502 hostapd_cleanup_cs_params(hapd);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700503 ieee802_11_set_beacon(hapd);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800504
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800505 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
506 "freq=%d dfs=%d", freq, is_dfs);
507 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
508 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
509 "freq=%d dfs=%d", freq, is_dfs);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800510 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700511#endif /* NEED_AP_MLME */
512}
513
514
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800515void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
516 const u8 *addr, int reason_code)
517{
518 switch (reason_code) {
519 case MAX_CLIENT_REACHED:
520 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
521 MAC2STR(addr));
522 break;
523 case BLOCKED_CLIENT:
524 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
525 MAC2STR(addr));
526 break;
527 }
528}
529
530
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800531#ifdef CONFIG_ACS
532static void hostapd_acs_channel_selected(struct hostapd_data *hapd,
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700533 struct acs_selected_channels *acs_res)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800534{
Dmitry Shmidtb1e52102015-05-29 12:36:29 -0700535 int ret, i;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800536
537 if (hapd->iconf->channel) {
538 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
539 hapd->iconf->channel);
540 return;
541 }
542
Dmitry Shmidtb1e52102015-05-29 12:36:29 -0700543 if (!hapd->iface->current_mode) {
544 for (i = 0; i < hapd->iface->num_hw_features; i++) {
545 struct hostapd_hw_modes *mode =
546 &hapd->iface->hw_features[i];
547
548 if (mode->mode == acs_res->hw_mode) {
549 hapd->iface->current_mode = mode;
550 break;
551 }
552 }
553 if (!hapd->iface->current_mode) {
554 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
555 HOSTAPD_LEVEL_WARNING,
556 "driver selected to bad hw_mode");
557 return;
558 }
559 }
560
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700561 hapd->iface->freq = hostapd_hw_get_freq(hapd, acs_res->pri_channel);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800562
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700563 if (!acs_res->pri_channel) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800564 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
565 HOSTAPD_LEVEL_WARNING,
566 "driver switched to bad channel");
567 return;
568 }
569
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700570 hapd->iconf->channel = acs_res->pri_channel;
571 hapd->iconf->acs = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800572
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700573 if (acs_res->sec_channel == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800574 hapd->iconf->secondary_channel = 0;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700575 else if (acs_res->sec_channel < acs_res->pri_channel)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800576 hapd->iconf->secondary_channel = -1;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700577 else if (acs_res->sec_channel > acs_res->pri_channel)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800578 hapd->iconf->secondary_channel = 1;
579 else {
580 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
581 return;
582 }
583
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700584 if (hapd->iface->conf->ieee80211ac) {
585 /* set defaults for backwards compatibility */
586 hapd->iconf->vht_oper_centr_freq_seg1_idx = 0;
587 hapd->iconf->vht_oper_centr_freq_seg0_idx = 0;
588 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
589 if (acs_res->ch_width == 80) {
590 hapd->iconf->vht_oper_centr_freq_seg0_idx =
591 acs_res->vht_seg0_center_ch;
592 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
593 } else if (acs_res->ch_width == 160) {
594 if (acs_res->vht_seg1_center_ch == 0) {
595 hapd->iconf->vht_oper_centr_freq_seg0_idx =
596 acs_res->vht_seg0_center_ch;
597 hapd->iconf->vht_oper_chwidth =
598 VHT_CHANWIDTH_160MHZ;
599 } else {
600 hapd->iconf->vht_oper_centr_freq_seg0_idx =
601 acs_res->vht_seg0_center_ch;
602 hapd->iconf->vht_oper_centr_freq_seg1_idx =
603 acs_res->vht_seg1_center_ch;
604 hapd->iconf->vht_oper_chwidth =
605 VHT_CHANWIDTH_80P80MHZ;
606 }
607 }
608 }
609
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800610 ret = hostapd_acs_completed(hapd->iface, 0);
611 if (ret) {
612 wpa_printf(MSG_ERROR,
613 "ACS: Possibly channel configuration is invalid");
614 }
615}
616#endif /* CONFIG_ACS */
617
618
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800619int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700620 const u8 *bssid, const u8 *ie, size_t ie_len,
621 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622{
623 size_t i;
624 int ret = 0;
625
626 if (sa == NULL || ie == NULL)
627 return -1;
628
629 random_add_randomness(sa, ETH_ALEN);
630 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
631 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700632 sa, da, bssid, ie, ie_len,
633 ssi_signal) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 ret = 1;
635 break;
636 }
637 }
638 return ret;
639}
640
641
642#ifdef HOSTAPD
643
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700644#ifdef CONFIG_IEEE80211R
645static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
646 const u8 *bssid,
647 u16 auth_transaction, u16 status,
648 const u8 *ies, size_t ies_len)
649{
650 struct hostapd_data *hapd = ctx;
651 struct sta_info *sta;
652
653 sta = ap_get_sta(hapd, dst);
654 if (sta == NULL)
655 return;
656
657 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
658 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
659 sta->flags |= WLAN_STA_AUTH;
660
661 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
662}
663#endif /* CONFIG_IEEE80211R */
664
665
666static void hostapd_notif_auth(struct hostapd_data *hapd,
667 struct auth_info *rx_auth)
668{
669 struct sta_info *sta;
670 u16 status = WLAN_STATUS_SUCCESS;
671 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
672 size_t resp_ies_len = 0;
673
674 sta = ap_get_sta(hapd, rx_auth->peer);
675 if (!sta) {
676 sta = ap_sta_add(hapd, rx_auth->peer);
677 if (sta == NULL) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700678 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700679 goto fail;
680 }
681 }
682 sta->flags &= ~WLAN_STA_PREAUTH;
683 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
684#ifdef CONFIG_IEEE80211R
685 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
686 sta->auth_alg = WLAN_AUTH_FT;
687 if (sta->wpa_sm == NULL)
688 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700689 sta->addr, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700690 if (sta->wpa_sm == NULL) {
691 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
692 "state machine");
693 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
694 goto fail;
695 }
696 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
697 rx_auth->auth_transaction, rx_auth->ies,
698 rx_auth->ies_len,
699 hostapd_notify_auth_ft_finish, hapd);
700 return;
701 }
702#endif /* CONFIG_IEEE80211R */
703fail:
704 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
705 status, resp_ies, resp_ies_len);
706}
707
708
709static void hostapd_action_rx(struct hostapd_data *hapd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800710 struct rx_mgmt *drv_mgmt)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700711{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800712 struct ieee80211_mgmt *mgmt;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700713 struct sta_info *sta;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800714 size_t plen __maybe_unused;
715 u16 fc;
716
717 if (drv_mgmt->frame_len < 24 + 1)
718 return;
719
720 plen = drv_mgmt->frame_len - 24 - 1;
721
722 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
723 fc = le_to_host16(mgmt->frame_control);
724 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
725 return; /* handled by the driver */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700726
727 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800728 mgmt->u.action.category, (int) plen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800730 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700731 if (sta == NULL) {
732 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
733 return;
734 }
735#ifdef CONFIG_IEEE80211R
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800736 if (mgmt->u.action.category == WLAN_ACTION_FT) {
737 const u8 *payload = drv_mgmt->frame + 24 + 1;
738 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700739 }
740#endif /* CONFIG_IEEE80211R */
741#ifdef CONFIG_IEEE80211W
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800742 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
743 ieee802_11_sa_query_action(
744 hapd, mgmt->sa,
745 mgmt->u.action.u.sa_query_resp.action,
746 mgmt->u.action.u.sa_query_resp.trans_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700747 }
748#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800749#ifdef CONFIG_WNM
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800750 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
751 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700752 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800753#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700754}
755
756
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700757#ifdef NEED_AP_MLME
758
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700759#define HAPD_BROADCAST ((struct hostapd_data *) -1)
760
761static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
762 const u8 *bssid)
763{
764 size_t i;
765
766 if (bssid == NULL)
767 return NULL;
768 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
769 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
770 return HAPD_BROADCAST;
771
772 for (i = 0; i < iface->num_bss; i++) {
773 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
774 return iface->bss[i];
775 }
776
777 return NULL;
778}
779
780
781static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800782 const u8 *bssid, const u8 *addr,
783 int wds)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800785 hapd = get_hapd_bssid(hapd->iface, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700786 if (hapd == NULL || hapd == HAPD_BROADCAST)
787 return;
788
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800789 ieee802_11_rx_from_unknown(hapd, addr, wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790}
791
792
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800793static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700794{
795 struct hostapd_iface *iface = hapd->iface;
796 const struct ieee80211_hdr *hdr;
797 const u8 *bssid;
798 struct hostapd_frame_info fi;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800799 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700800
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800801#ifdef CONFIG_TESTING_OPTIONS
802 if (hapd->ext_mgmt_frame_handling) {
803 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
804 char *hex = os_malloc(hex_len);
805 if (hex) {
806 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
807 rx_mgmt->frame_len);
808 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
809 os_free(hex);
810 }
811 return 1;
812 }
813#endif /* CONFIG_TESTING_OPTIONS */
814
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700815 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
816 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
817 if (bssid == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800818 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700819
820 hapd = get_hapd_bssid(iface, bssid);
821 if (hapd == NULL) {
822 u16 fc;
823 fc = le_to_host16(hdr->frame_control);
824
825 /*
826 * Drop frames to unknown BSSIDs except for Beacon frames which
827 * could be used to update neighbor information.
828 */
829 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
830 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
831 hapd = iface->bss[0];
832 else
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800833 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700834 }
835
836 os_memset(&fi, 0, sizeof(fi));
837 fi.datarate = rx_mgmt->datarate;
838 fi.ssi_signal = rx_mgmt->ssi_signal;
839
840 if (hapd == HAPD_BROADCAST) {
841 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800842 ret = 0;
843 for (i = 0; i < iface->num_bss; i++) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700844 /* if bss is set, driver will call this function for
845 * each bss individually. */
846 if (rx_mgmt->drv_priv &&
847 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
848 continue;
849
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800850 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
851 rx_mgmt->frame_len, &fi) > 0)
852 ret = 1;
853 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700854 } else
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800855 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
856 &fi);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700857
858 random_add_randomness(&fi, sizeof(fi));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700859
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800860 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700861}
862
863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
865 size_t len, u16 stype, int ok)
866{
867 struct ieee80211_hdr *hdr;
868 hdr = (struct ieee80211_hdr *) buf;
869 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
870 if (hapd == NULL || hapd == HAPD_BROADCAST)
871 return;
872 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
873}
874
875#endif /* NEED_AP_MLME */
876
877
878static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
879{
880 struct sta_info *sta = ap_get_sta(hapd, addr);
881 if (sta)
882 return 0;
883
884 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
885 " - adding a new STA", MAC2STR(addr));
886 sta = ap_sta_add(hapd, addr);
887 if (sta) {
888 hostapd_new_assoc_sta(hapd, sta, 0);
889 } else {
890 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
891 MAC2STR(addr));
892 return -1;
893 }
894
895 return 0;
896}
897
898
899static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
900 const u8 *data, size_t data_len)
901{
902 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800903 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700904 size_t j;
905
906 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800907 if ((sta = ap_get_sta(iface->bss[j], src))) {
908 if (sta->flags & WLAN_STA_ASSOC) {
909 hapd = iface->bss[j];
910 break;
911 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700912 }
913 }
914
915 ieee802_1x_receive(hapd, src, data, data_len);
916}
917
918
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700919static struct hostapd_channel_data * hostapd_get_mode_channel(
920 struct hostapd_iface *iface, unsigned int freq)
921{
922 int i;
923 struct hostapd_channel_data *chan;
924
925 for (i = 0; i < iface->current_mode->num_channels; i++) {
926 chan = &iface->current_mode->channels[i];
927 if (!chan)
928 return NULL;
929 if ((unsigned int) chan->freq == freq)
930 return chan;
931 }
932
933 return NULL;
934}
935
936
937static void hostapd_update_nf(struct hostapd_iface *iface,
938 struct hostapd_channel_data *chan,
939 struct freq_survey *survey)
940{
941 if (!iface->chans_surveyed) {
942 chan->min_nf = survey->nf;
943 iface->lowest_nf = survey->nf;
944 } else {
945 if (dl_list_empty(&chan->survey_list))
946 chan->min_nf = survey->nf;
947 else if (survey->nf < chan->min_nf)
948 chan->min_nf = survey->nf;
949 if (survey->nf < iface->lowest_nf)
950 iface->lowest_nf = survey->nf;
951 }
952}
953
954
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800955static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
956 struct survey_results *survey_res)
957{
958 struct hostapd_channel_data *chan;
959 struct freq_survey *survey;
960 u64 divisor, dividend;
961
962 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
963 list);
964 if (!survey || !survey->freq)
965 return;
966
967 chan = hostapd_get_mode_channel(iface, survey->freq);
968 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
969 return;
970
971 wpa_printf(MSG_DEBUG, "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
972 survey->freq,
973 (unsigned long int) survey->channel_time,
974 (unsigned long int) survey->channel_time_busy);
975
976 if (survey->channel_time > iface->last_channel_time &&
977 survey->channel_time > survey->channel_time_busy) {
978 dividend = survey->channel_time_busy -
979 iface->last_channel_time_busy;
980 divisor = survey->channel_time - iface->last_channel_time;
981
982 iface->channel_utilization = dividend * 255 / divisor;
983 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
984 iface->channel_utilization);
985 }
986 iface->last_channel_time = survey->channel_time;
987 iface->last_channel_time_busy = survey->channel_time_busy;
988}
989
990
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700991static void hostapd_event_get_survey(struct hostapd_data *hapd,
992 struct survey_results *survey_results)
993{
994 struct hostapd_iface *iface = hapd->iface;
995 struct freq_survey *survey, *tmp;
996 struct hostapd_channel_data *chan;
997
998 if (dl_list_empty(&survey_results->survey_list)) {
999 wpa_printf(MSG_DEBUG, "No survey data received");
1000 return;
1001 }
1002
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001003 if (survey_results->freq_filter) {
1004 hostapd_single_channel_get_survey(iface, survey_results);
1005 return;
1006 }
1007
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001008 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
1009 struct freq_survey, list) {
1010 chan = hostapd_get_mode_channel(iface, survey->freq);
1011 if (!chan)
1012 continue;
1013 if (chan->flag & HOSTAPD_CHAN_DISABLED)
1014 continue;
1015
1016 dl_list_del(&survey->list);
1017 dl_list_add_tail(&chan->survey_list, &survey->list);
1018
1019 hostapd_update_nf(iface, chan, survey);
1020
1021 iface->chans_surveyed++;
1022 }
1023}
1024
1025
Dmitry Shmidt051af732013-10-22 13:52:46 -07001026#ifdef NEED_AP_MLME
1027
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001028static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1029{
1030 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1031 hapd->conf->iface);
1032
1033 if (hapd->csa_in_progress) {
1034 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1035 hapd->conf->iface);
1036 hostapd_switch_channel_fallback(hapd->iface,
1037 &hapd->cs_freq_params);
1038 }
1039}
1040
1041
Dmitry Shmidt051af732013-10-22 13:52:46 -07001042static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1043 struct dfs_event *radar)
1044{
1045 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001046 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001047 radar->chan_offset, radar->chan_width,
1048 radar->cf1, radar->cf2);
1049}
1050
1051
1052static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1053 struct dfs_event *radar)
1054{
1055 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001056 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001057 radar->chan_offset, radar->chan_width,
1058 radar->cf1, radar->cf2);
1059}
1060
1061
1062static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1063 struct dfs_event *radar)
1064{
1065 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001066 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001067 radar->chan_offset, radar->chan_width,
1068 radar->cf1, radar->cf2);
1069}
1070
1071
1072static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1073 struct dfs_event *radar)
1074{
1075 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001076 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001077 radar->chan_offset, radar->chan_width,
1078 radar->cf1, radar->cf2);
1079}
1080
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001081
1082static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1083 struct dfs_event *radar)
1084{
1085 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1086 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1087 radar->chan_offset, radar->chan_width,
1088 radar->cf1, radar->cf2);
1089}
1090
Dmitry Shmidt051af732013-10-22 13:52:46 -07001091#endif /* NEED_AP_MLME */
1092
1093
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001094void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1095 union wpa_event_data *data)
1096{
1097 struct hostapd_data *hapd = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001098#ifndef CONFIG_NO_STDOUT_DEBUG
1099 int level = MSG_DEBUG;
1100
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001101 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001102 data->rx_mgmt.frame_len >= 24) {
1103 const struct ieee80211_hdr *hdr;
1104 u16 fc;
1105 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1106 fc = le_to_host16(hdr->frame_control);
1107 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1108 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1109 level = MSG_EXCESSIVE;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001110 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1111 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1112 level = MSG_EXCESSIVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001113 }
1114
1115 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1116 event_to_string(event), event);
1117#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118
1119 switch (event) {
1120 case EVENT_MICHAEL_MIC_FAILURE:
1121 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1122 break;
1123 case EVENT_SCAN_RESULTS:
1124 if (hapd->iface->scan_cb)
1125 hapd->iface->scan_cb(hapd->iface);
1126 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001127 case EVENT_WPS_BUTTON_PUSHED:
1128 hostapd_wps_button_pushed(hapd, NULL);
1129 break;
1130#ifdef NEED_AP_MLME
1131 case EVENT_TX_STATUS:
1132 switch (data->tx_status.type) {
1133 case WLAN_FC_TYPE_MGMT:
1134 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1135 data->tx_status.data_len,
1136 data->tx_status.stype,
1137 data->tx_status.ack);
1138 break;
1139 case WLAN_FC_TYPE_DATA:
1140 hostapd_tx_status(hapd, data->tx_status.dst,
1141 data->tx_status.data,
1142 data->tx_status.data_len,
1143 data->tx_status.ack);
1144 break;
1145 }
1146 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001147 case EVENT_EAPOL_TX_STATUS:
1148 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1149 data->eapol_tx_status.data,
1150 data->eapol_tx_status.data_len,
1151 data->eapol_tx_status.ack);
1152 break;
1153 case EVENT_DRIVER_CLIENT_POLL_OK:
1154 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1155 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001156 case EVENT_RX_FROM_UNKNOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001157 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1158 data->rx_from_unknown.addr,
1159 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001161#endif /* NEED_AP_MLME */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001162 case EVENT_RX_MGMT:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001163 if (!data->rx_mgmt.frame)
1164 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001165#ifdef NEED_AP_MLME
1166 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1167 break;
1168#endif /* NEED_AP_MLME */
1169 hostapd_action_rx(hapd, &data->rx_mgmt);
1170 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001171 case EVENT_RX_PROBE_REQ:
1172 if (data->rx_probe_req.sa == NULL ||
1173 data->rx_probe_req.ie == NULL)
1174 break;
1175 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001176 data->rx_probe_req.da,
1177 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001178 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001179 data->rx_probe_req.ie_len,
1180 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001181 break;
1182 case EVENT_NEW_STA:
1183 hostapd_event_new_sta(hapd, data->new_sta.addr);
1184 break;
1185 case EVENT_EAPOL_RX:
1186 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1187 data->eapol_rx.data,
1188 data->eapol_rx.data_len);
1189 break;
1190 case EVENT_ASSOC:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001191 if (!data)
1192 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001193 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1194 data->assoc_info.req_ies,
1195 data->assoc_info.req_ies_len,
1196 data->assoc_info.reassoc);
1197 break;
1198 case EVENT_DISASSOC:
1199 if (data)
1200 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1201 break;
1202 case EVENT_DEAUTH:
1203 if (data)
1204 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1205 break;
1206 case EVENT_STATION_LOW_ACK:
1207 if (!data)
1208 break;
1209 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1210 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001211 case EVENT_AUTH:
1212 hostapd_notif_auth(hapd, &data->auth);
1213 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001214 case EVENT_CH_SWITCH:
1215 if (!data)
1216 break;
1217 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1218 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001219 data->ch_switch.ch_offset,
1220 data->ch_switch.ch_width,
1221 data->ch_switch.cf1,
1222 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001223 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001224 case EVENT_CONNECT_FAILED_REASON:
1225 if (!data)
1226 break;
1227 hostapd_event_connect_failed_reason(
1228 hapd, data->connect_failed_reason.addr,
1229 data->connect_failed_reason.code);
1230 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001231 case EVENT_SURVEY:
1232 hostapd_event_get_survey(hapd, &data->survey_results);
1233 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001234#ifdef NEED_AP_MLME
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001235 case EVENT_INTERFACE_UNAVAILABLE:
1236 hostapd_event_iface_unavailable(hapd);
1237 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001238 case EVENT_DFS_RADAR_DETECTED:
1239 if (!data)
1240 break;
1241 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1242 break;
1243 case EVENT_DFS_CAC_FINISHED:
1244 if (!data)
1245 break;
1246 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1247 break;
1248 case EVENT_DFS_CAC_ABORTED:
1249 if (!data)
1250 break;
1251 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1252 break;
1253 case EVENT_DFS_NOP_FINISHED:
1254 if (!data)
1255 break;
1256 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1257 break;
1258 case EVENT_CHANNEL_LIST_CHANGED:
1259 /* channel list changed (regulatory?), update channel list */
1260 /* TODO: check this. hostapd_get_hw_features() initializes
1261 * too much stuff. */
1262 /* hostapd_get_hw_features(hapd->iface); */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001263 hostapd_channel_list_updated(
1264 hapd->iface, data->channel_list_changed.initiator);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001265 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001266 case EVENT_DFS_CAC_STARTED:
1267 if (!data)
1268 break;
1269 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1270 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001271#endif /* NEED_AP_MLME */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001272 case EVENT_INTERFACE_ENABLED:
1273 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001274 if (hapd->disabled && hapd->started) {
1275 hapd->disabled = 0;
1276 /*
1277 * Try to re-enable interface if the driver stopped it
1278 * when the interface got disabled.
1279 */
1280 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1281 hapd->reenable_beacon = 1;
1282 ieee802_11_set_beacon(hapd);
1283 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001284 break;
1285 case EVENT_INTERFACE_DISABLED:
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001286 hostapd_free_stas(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001287 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001288 hapd->disabled = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001289 break;
1290#ifdef CONFIG_ACS
1291 case EVENT_ACS_CHANNEL_SELECTED:
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001292 hostapd_acs_channel_selected(hapd,
1293 &data->acs_selected_channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001294 break;
1295#endif /* CONFIG_ACS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001296 default:
1297 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1298 break;
1299 }
1300}
1301
1302#endif /* HOSTAPD */