blob: 80e4c2e7c2065b7b0a1cdcb0ec8100c10b1b327b [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 Shmidt6c0da2b2015-01-05 13:08:17 -0800535 int ret;
536
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 Shmidtdda10c22015-03-24 16:05:01 -0700543 hapd->iface->freq = hostapd_hw_get_freq(hapd, acs_res->pri_channel);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800544
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700545 if (!acs_res->pri_channel) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800546 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
547 HOSTAPD_LEVEL_WARNING,
548 "driver switched to bad channel");
549 return;
550 }
551
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700552 hapd->iconf->channel = acs_res->pri_channel;
553 hapd->iconf->acs = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800554
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700555 if (acs_res->sec_channel == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800556 hapd->iconf->secondary_channel = 0;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700557 else if (acs_res->sec_channel < acs_res->pri_channel)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800558 hapd->iconf->secondary_channel = -1;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700559 else if (acs_res->sec_channel > acs_res->pri_channel)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800560 hapd->iconf->secondary_channel = 1;
561 else {
562 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
563 return;
564 }
565
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700566 if (hapd->iface->conf->ieee80211ac) {
567 /* set defaults for backwards compatibility */
568 hapd->iconf->vht_oper_centr_freq_seg1_idx = 0;
569 hapd->iconf->vht_oper_centr_freq_seg0_idx = 0;
570 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
571 if (acs_res->ch_width == 80) {
572 hapd->iconf->vht_oper_centr_freq_seg0_idx =
573 acs_res->vht_seg0_center_ch;
574 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
575 } else if (acs_res->ch_width == 160) {
576 if (acs_res->vht_seg1_center_ch == 0) {
577 hapd->iconf->vht_oper_centr_freq_seg0_idx =
578 acs_res->vht_seg0_center_ch;
579 hapd->iconf->vht_oper_chwidth =
580 VHT_CHANWIDTH_160MHZ;
581 } else {
582 hapd->iconf->vht_oper_centr_freq_seg0_idx =
583 acs_res->vht_seg0_center_ch;
584 hapd->iconf->vht_oper_centr_freq_seg1_idx =
585 acs_res->vht_seg1_center_ch;
586 hapd->iconf->vht_oper_chwidth =
587 VHT_CHANWIDTH_80P80MHZ;
588 }
589 }
590 }
591
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800592 ret = hostapd_acs_completed(hapd->iface, 0);
593 if (ret) {
594 wpa_printf(MSG_ERROR,
595 "ACS: Possibly channel configuration is invalid");
596 }
597}
598#endif /* CONFIG_ACS */
599
600
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800601int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700602 const u8 *bssid, const u8 *ie, size_t ie_len,
603 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604{
605 size_t i;
606 int ret = 0;
607
608 if (sa == NULL || ie == NULL)
609 return -1;
610
611 random_add_randomness(sa, ETH_ALEN);
612 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
613 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700614 sa, da, bssid, ie, ie_len,
615 ssi_signal) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700616 ret = 1;
617 break;
618 }
619 }
620 return ret;
621}
622
623
624#ifdef HOSTAPD
625
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700626#ifdef CONFIG_IEEE80211R
627static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
628 const u8 *bssid,
629 u16 auth_transaction, u16 status,
630 const u8 *ies, size_t ies_len)
631{
632 struct hostapd_data *hapd = ctx;
633 struct sta_info *sta;
634
635 sta = ap_get_sta(hapd, dst);
636 if (sta == NULL)
637 return;
638
639 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
640 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
641 sta->flags |= WLAN_STA_AUTH;
642
643 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
644}
645#endif /* CONFIG_IEEE80211R */
646
647
648static void hostapd_notif_auth(struct hostapd_data *hapd,
649 struct auth_info *rx_auth)
650{
651 struct sta_info *sta;
652 u16 status = WLAN_STATUS_SUCCESS;
653 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
654 size_t resp_ies_len = 0;
655
656 sta = ap_get_sta(hapd, rx_auth->peer);
657 if (!sta) {
658 sta = ap_sta_add(hapd, rx_auth->peer);
659 if (sta == NULL) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700660 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700661 goto fail;
662 }
663 }
664 sta->flags &= ~WLAN_STA_PREAUTH;
665 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
666#ifdef CONFIG_IEEE80211R
667 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
668 sta->auth_alg = WLAN_AUTH_FT;
669 if (sta->wpa_sm == NULL)
670 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700671 sta->addr, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700672 if (sta->wpa_sm == NULL) {
673 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
674 "state machine");
675 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
676 goto fail;
677 }
678 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
679 rx_auth->auth_transaction, rx_auth->ies,
680 rx_auth->ies_len,
681 hostapd_notify_auth_ft_finish, hapd);
682 return;
683 }
684#endif /* CONFIG_IEEE80211R */
685fail:
686 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
687 status, resp_ies, resp_ies_len);
688}
689
690
691static void hostapd_action_rx(struct hostapd_data *hapd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800692 struct rx_mgmt *drv_mgmt)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700693{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800694 struct ieee80211_mgmt *mgmt;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700695 struct sta_info *sta;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800696 size_t plen __maybe_unused;
697 u16 fc;
698
699 if (drv_mgmt->frame_len < 24 + 1)
700 return;
701
702 plen = drv_mgmt->frame_len - 24 - 1;
703
704 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
705 fc = le_to_host16(mgmt->frame_control);
706 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
707 return; /* handled by the driver */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700708
709 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800710 mgmt->u.action.category, (int) plen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700711
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800712 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700713 if (sta == NULL) {
714 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
715 return;
716 }
717#ifdef CONFIG_IEEE80211R
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800718 if (mgmt->u.action.category == WLAN_ACTION_FT) {
719 const u8 *payload = drv_mgmt->frame + 24 + 1;
720 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700721 }
722#endif /* CONFIG_IEEE80211R */
723#ifdef CONFIG_IEEE80211W
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800724 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
725 ieee802_11_sa_query_action(
726 hapd, mgmt->sa,
727 mgmt->u.action.u.sa_query_resp.action,
728 mgmt->u.action.u.sa_query_resp.trans_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700729 }
730#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800731#ifdef CONFIG_WNM
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800732 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
733 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700734 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800735#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700736}
737
738
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700739#ifdef NEED_AP_MLME
740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741#define HAPD_BROADCAST ((struct hostapd_data *) -1)
742
743static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
744 const u8 *bssid)
745{
746 size_t i;
747
748 if (bssid == NULL)
749 return NULL;
750 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
751 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
752 return HAPD_BROADCAST;
753
754 for (i = 0; i < iface->num_bss; i++) {
755 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
756 return iface->bss[i];
757 }
758
759 return NULL;
760}
761
762
763static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800764 const u8 *bssid, const u8 *addr,
765 int wds)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800767 hapd = get_hapd_bssid(hapd->iface, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768 if (hapd == NULL || hapd == HAPD_BROADCAST)
769 return;
770
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800771 ieee802_11_rx_from_unknown(hapd, addr, wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700772}
773
774
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800775static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700776{
777 struct hostapd_iface *iface = hapd->iface;
778 const struct ieee80211_hdr *hdr;
779 const u8 *bssid;
780 struct hostapd_frame_info fi;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800781 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800783#ifdef CONFIG_TESTING_OPTIONS
784 if (hapd->ext_mgmt_frame_handling) {
785 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
786 char *hex = os_malloc(hex_len);
787 if (hex) {
788 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
789 rx_mgmt->frame_len);
790 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
791 os_free(hex);
792 }
793 return 1;
794 }
795#endif /* CONFIG_TESTING_OPTIONS */
796
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700797 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
798 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
799 if (bssid == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800800 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
802 hapd = get_hapd_bssid(iface, bssid);
803 if (hapd == NULL) {
804 u16 fc;
805 fc = le_to_host16(hdr->frame_control);
806
807 /*
808 * Drop frames to unknown BSSIDs except for Beacon frames which
809 * could be used to update neighbor information.
810 */
811 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
812 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
813 hapd = iface->bss[0];
814 else
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800815 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700816 }
817
818 os_memset(&fi, 0, sizeof(fi));
819 fi.datarate = rx_mgmt->datarate;
820 fi.ssi_signal = rx_mgmt->ssi_signal;
821
822 if (hapd == HAPD_BROADCAST) {
823 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800824 ret = 0;
825 for (i = 0; i < iface->num_bss; i++) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700826 /* if bss is set, driver will call this function for
827 * each bss individually. */
828 if (rx_mgmt->drv_priv &&
829 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
830 continue;
831
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800832 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
833 rx_mgmt->frame_len, &fi) > 0)
834 ret = 1;
835 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700836 } else
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800837 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
838 &fi);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700839
840 random_add_randomness(&fi, sizeof(fi));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800842 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700843}
844
845
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700846static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
847 size_t len, u16 stype, int ok)
848{
849 struct ieee80211_hdr *hdr;
850 hdr = (struct ieee80211_hdr *) buf;
851 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
852 if (hapd == NULL || hapd == HAPD_BROADCAST)
853 return;
854 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
855}
856
857#endif /* NEED_AP_MLME */
858
859
860static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
861{
862 struct sta_info *sta = ap_get_sta(hapd, addr);
863 if (sta)
864 return 0;
865
866 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
867 " - adding a new STA", MAC2STR(addr));
868 sta = ap_sta_add(hapd, addr);
869 if (sta) {
870 hostapd_new_assoc_sta(hapd, sta, 0);
871 } else {
872 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
873 MAC2STR(addr));
874 return -1;
875 }
876
877 return 0;
878}
879
880
881static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
882 const u8 *data, size_t data_len)
883{
884 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800885 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700886 size_t j;
887
888 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800889 if ((sta = ap_get_sta(iface->bss[j], src))) {
890 if (sta->flags & WLAN_STA_ASSOC) {
891 hapd = iface->bss[j];
892 break;
893 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700894 }
895 }
896
897 ieee802_1x_receive(hapd, src, data, data_len);
898}
899
900
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700901static struct hostapd_channel_data * hostapd_get_mode_channel(
902 struct hostapd_iface *iface, unsigned int freq)
903{
904 int i;
905 struct hostapd_channel_data *chan;
906
907 for (i = 0; i < iface->current_mode->num_channels; i++) {
908 chan = &iface->current_mode->channels[i];
909 if (!chan)
910 return NULL;
911 if ((unsigned int) chan->freq == freq)
912 return chan;
913 }
914
915 return NULL;
916}
917
918
919static void hostapd_update_nf(struct hostapd_iface *iface,
920 struct hostapd_channel_data *chan,
921 struct freq_survey *survey)
922{
923 if (!iface->chans_surveyed) {
924 chan->min_nf = survey->nf;
925 iface->lowest_nf = survey->nf;
926 } else {
927 if (dl_list_empty(&chan->survey_list))
928 chan->min_nf = survey->nf;
929 else if (survey->nf < chan->min_nf)
930 chan->min_nf = survey->nf;
931 if (survey->nf < iface->lowest_nf)
932 iface->lowest_nf = survey->nf;
933 }
934}
935
936
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800937static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
938 struct survey_results *survey_res)
939{
940 struct hostapd_channel_data *chan;
941 struct freq_survey *survey;
942 u64 divisor, dividend;
943
944 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
945 list);
946 if (!survey || !survey->freq)
947 return;
948
949 chan = hostapd_get_mode_channel(iface, survey->freq);
950 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
951 return;
952
953 wpa_printf(MSG_DEBUG, "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
954 survey->freq,
955 (unsigned long int) survey->channel_time,
956 (unsigned long int) survey->channel_time_busy);
957
958 if (survey->channel_time > iface->last_channel_time &&
959 survey->channel_time > survey->channel_time_busy) {
960 dividend = survey->channel_time_busy -
961 iface->last_channel_time_busy;
962 divisor = survey->channel_time - iface->last_channel_time;
963
964 iface->channel_utilization = dividend * 255 / divisor;
965 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
966 iface->channel_utilization);
967 }
968 iface->last_channel_time = survey->channel_time;
969 iface->last_channel_time_busy = survey->channel_time_busy;
970}
971
972
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700973static void hostapd_event_get_survey(struct hostapd_data *hapd,
974 struct survey_results *survey_results)
975{
976 struct hostapd_iface *iface = hapd->iface;
977 struct freq_survey *survey, *tmp;
978 struct hostapd_channel_data *chan;
979
980 if (dl_list_empty(&survey_results->survey_list)) {
981 wpa_printf(MSG_DEBUG, "No survey data received");
982 return;
983 }
984
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800985 if (survey_results->freq_filter) {
986 hostapd_single_channel_get_survey(iface, survey_results);
987 return;
988 }
989
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700990 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
991 struct freq_survey, list) {
992 chan = hostapd_get_mode_channel(iface, survey->freq);
993 if (!chan)
994 continue;
995 if (chan->flag & HOSTAPD_CHAN_DISABLED)
996 continue;
997
998 dl_list_del(&survey->list);
999 dl_list_add_tail(&chan->survey_list, &survey->list);
1000
1001 hostapd_update_nf(iface, chan, survey);
1002
1003 iface->chans_surveyed++;
1004 }
1005}
1006
1007
Dmitry Shmidt051af732013-10-22 13:52:46 -07001008#ifdef NEED_AP_MLME
1009
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001010static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1011{
1012 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1013 hapd->conf->iface);
1014
1015 if (hapd->csa_in_progress) {
1016 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1017 hapd->conf->iface);
1018 hostapd_switch_channel_fallback(hapd->iface,
1019 &hapd->cs_freq_params);
1020 }
1021}
1022
1023
Dmitry Shmidt051af732013-10-22 13:52:46 -07001024static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1025 struct dfs_event *radar)
1026{
1027 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001028 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001029 radar->chan_offset, radar->chan_width,
1030 radar->cf1, radar->cf2);
1031}
1032
1033
1034static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1035 struct dfs_event *radar)
1036{
1037 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001038 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001039 radar->chan_offset, radar->chan_width,
1040 radar->cf1, radar->cf2);
1041}
1042
1043
1044static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1045 struct dfs_event *radar)
1046{
1047 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001048 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001049 radar->chan_offset, radar->chan_width,
1050 radar->cf1, radar->cf2);
1051}
1052
1053
1054static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1055 struct dfs_event *radar)
1056{
1057 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001058 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001059 radar->chan_offset, radar->chan_width,
1060 radar->cf1, radar->cf2);
1061}
1062
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001063
1064static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1065 struct dfs_event *radar)
1066{
1067 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1068 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1069 radar->chan_offset, radar->chan_width,
1070 radar->cf1, radar->cf2);
1071}
1072
Dmitry Shmidt051af732013-10-22 13:52:46 -07001073#endif /* NEED_AP_MLME */
1074
1075
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1077 union wpa_event_data *data)
1078{
1079 struct hostapd_data *hapd = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001080#ifndef CONFIG_NO_STDOUT_DEBUG
1081 int level = MSG_DEBUG;
1082
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001083 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001084 data->rx_mgmt.frame_len >= 24) {
1085 const struct ieee80211_hdr *hdr;
1086 u16 fc;
1087 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1088 fc = le_to_host16(hdr->frame_control);
1089 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1090 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1091 level = MSG_EXCESSIVE;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001092 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1093 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1094 level = MSG_EXCESSIVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001095 }
1096
1097 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1098 event_to_string(event), event);
1099#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100
1101 switch (event) {
1102 case EVENT_MICHAEL_MIC_FAILURE:
1103 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1104 break;
1105 case EVENT_SCAN_RESULTS:
1106 if (hapd->iface->scan_cb)
1107 hapd->iface->scan_cb(hapd->iface);
1108 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001109 case EVENT_WPS_BUTTON_PUSHED:
1110 hostapd_wps_button_pushed(hapd, NULL);
1111 break;
1112#ifdef NEED_AP_MLME
1113 case EVENT_TX_STATUS:
1114 switch (data->tx_status.type) {
1115 case WLAN_FC_TYPE_MGMT:
1116 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1117 data->tx_status.data_len,
1118 data->tx_status.stype,
1119 data->tx_status.ack);
1120 break;
1121 case WLAN_FC_TYPE_DATA:
1122 hostapd_tx_status(hapd, data->tx_status.dst,
1123 data->tx_status.data,
1124 data->tx_status.data_len,
1125 data->tx_status.ack);
1126 break;
1127 }
1128 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001129 case EVENT_EAPOL_TX_STATUS:
1130 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1131 data->eapol_tx_status.data,
1132 data->eapol_tx_status.data_len,
1133 data->eapol_tx_status.ack);
1134 break;
1135 case EVENT_DRIVER_CLIENT_POLL_OK:
1136 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1137 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001138 case EVENT_RX_FROM_UNKNOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001139 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1140 data->rx_from_unknown.addr,
1141 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001143#endif /* NEED_AP_MLME */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001144 case EVENT_RX_MGMT:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001145 if (!data->rx_mgmt.frame)
1146 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001147#ifdef NEED_AP_MLME
1148 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1149 break;
1150#endif /* NEED_AP_MLME */
1151 hostapd_action_rx(hapd, &data->rx_mgmt);
1152 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001153 case EVENT_RX_PROBE_REQ:
1154 if (data->rx_probe_req.sa == NULL ||
1155 data->rx_probe_req.ie == NULL)
1156 break;
1157 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001158 data->rx_probe_req.da,
1159 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001160 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001161 data->rx_probe_req.ie_len,
1162 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001163 break;
1164 case EVENT_NEW_STA:
1165 hostapd_event_new_sta(hapd, data->new_sta.addr);
1166 break;
1167 case EVENT_EAPOL_RX:
1168 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1169 data->eapol_rx.data,
1170 data->eapol_rx.data_len);
1171 break;
1172 case EVENT_ASSOC:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001173 if (!data)
1174 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001175 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1176 data->assoc_info.req_ies,
1177 data->assoc_info.req_ies_len,
1178 data->assoc_info.reassoc);
1179 break;
1180 case EVENT_DISASSOC:
1181 if (data)
1182 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1183 break;
1184 case EVENT_DEAUTH:
1185 if (data)
1186 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1187 break;
1188 case EVENT_STATION_LOW_ACK:
1189 if (!data)
1190 break;
1191 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1192 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001193 case EVENT_AUTH:
1194 hostapd_notif_auth(hapd, &data->auth);
1195 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001196 case EVENT_CH_SWITCH:
1197 if (!data)
1198 break;
1199 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1200 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001201 data->ch_switch.ch_offset,
1202 data->ch_switch.ch_width,
1203 data->ch_switch.cf1,
1204 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001205 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001206 case EVENT_CONNECT_FAILED_REASON:
1207 if (!data)
1208 break;
1209 hostapd_event_connect_failed_reason(
1210 hapd, data->connect_failed_reason.addr,
1211 data->connect_failed_reason.code);
1212 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001213 case EVENT_SURVEY:
1214 hostapd_event_get_survey(hapd, &data->survey_results);
1215 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001216#ifdef NEED_AP_MLME
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001217 case EVENT_INTERFACE_UNAVAILABLE:
1218 hostapd_event_iface_unavailable(hapd);
1219 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001220 case EVENT_DFS_RADAR_DETECTED:
1221 if (!data)
1222 break;
1223 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1224 break;
1225 case EVENT_DFS_CAC_FINISHED:
1226 if (!data)
1227 break;
1228 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1229 break;
1230 case EVENT_DFS_CAC_ABORTED:
1231 if (!data)
1232 break;
1233 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1234 break;
1235 case EVENT_DFS_NOP_FINISHED:
1236 if (!data)
1237 break;
1238 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1239 break;
1240 case EVENT_CHANNEL_LIST_CHANGED:
1241 /* channel list changed (regulatory?), update channel list */
1242 /* TODO: check this. hostapd_get_hw_features() initializes
1243 * too much stuff. */
1244 /* hostapd_get_hw_features(hapd->iface); */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001245 hostapd_channel_list_updated(
1246 hapd->iface, data->channel_list_changed.initiator);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001247 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001248 case EVENT_DFS_CAC_STARTED:
1249 if (!data)
1250 break;
1251 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1252 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001253#endif /* NEED_AP_MLME */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001254 case EVENT_INTERFACE_ENABLED:
1255 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001256 if (hapd->disabled && hapd->started) {
1257 hapd->disabled = 0;
1258 /*
1259 * Try to re-enable interface if the driver stopped it
1260 * when the interface got disabled.
1261 */
1262 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1263 hapd->reenable_beacon = 1;
1264 ieee802_11_set_beacon(hapd);
1265 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001266 break;
1267 case EVENT_INTERFACE_DISABLED:
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001268 hostapd_free_stas(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001269 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001270 hapd->disabled = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001271 break;
1272#ifdef CONFIG_ACS
1273 case EVENT_ACS_CHANNEL_SELECTED:
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001274 hostapd_acs_channel_selected(hapd,
1275 &data->acs_selected_channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001276 break;
1277#endif /* CONFIG_ACS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001278 default:
1279 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1280 break;
1281 }
1282}
1283
1284#endif /* HOSTAPD */