blob: 507053eaa230ed3f6fbc27daf4589ff6741d7939 [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 &&
129 elems.ht_capabilities_len >=
130 sizeof(struct ieee80211_ht_capabilities) &&
131 (hapd->iface->conf->ht_capab &
132 HT_CAP_INFO_SUPP_CHANNEL_WIDTH_SET)) {
133 struct ieee80211_ht_capabilities *ht_cap =
134 (struct ieee80211_ht_capabilities *)
135 elems.ht_capabilities;
136
137 if (le_to_host16(ht_cap->ht_capabilities_info) &
138 HT_CAP_INFO_40MHZ_INTOLERANT)
139 ht40_intolerant_add(hapd->iface, sta);
140 }
141#endif /* NEED_AP_MLME */
142#endif /* CONFIG_IEEE80211N */
143
Dmitry Shmidt051af732013-10-22 13:52:46 -0700144#ifdef CONFIG_INTERWORKING
145 if (elems.ext_capab && elems.ext_capab_len > 4) {
146 if (elems.ext_capab[4] & 0x01)
147 sta->qos_map_enabled = 1;
148 }
149#endif /* CONFIG_INTERWORKING */
150
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800151#ifdef CONFIG_HS20
152 wpabuf_free(sta->hs20_ie);
153 if (elems.hs20 && elems.hs20_len > 4) {
154 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
155 elems.hs20_len - 4);
156 } else
157 sta->hs20_ie = NULL;
158#endif /* CONFIG_HS20 */
159
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700160 if (hapd->conf->wpa) {
161 if (ie == NULL || ielen == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800162#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700163 if (hapd->conf->wps_state) {
164 wpa_printf(MSG_DEBUG, "STA did not include "
165 "WPA/RSN IE in (Re)Association "
166 "Request - possible WPS use");
167 sta->flags |= WLAN_STA_MAYBE_WPS;
168 goto skip_wpa_check;
169 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700171
172 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
173 return -1;
174 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800175#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700176 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
177 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800178 struct wpabuf *wps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700179 sta->flags |= WLAN_STA_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800180 wps = ieee802_11_vendor_ie_concat(ie, ielen,
181 WPS_IE_VENDOR_TYPE);
182 if (wps) {
183 if (wps_is_20(wps)) {
184 wpa_printf(MSG_DEBUG, "WPS: STA "
185 "supports WPS 2.0");
186 sta->flags |= WLAN_STA_WPS2;
187 }
188 wpabuf_free(wps);
189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190 goto skip_wpa_check;
191 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800192#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700193
194 if (sta->wpa_sm == NULL)
195 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700196 sta->addr,
197 p2p_dev_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700198 if (sta->wpa_sm == NULL) {
199 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
200 "machine");
201 return -1;
202 }
203 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700204 ie, ielen,
205 elems.mdie, elems.mdie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700206 if (res != WPA_IE_OK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700207 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
208 "rejected? (res %u)", res);
209 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700210 if (res == WPA_INVALID_GROUP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800211 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700212 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
213 } else if (res == WPA_INVALID_PAIRWISE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800214 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700215 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
216 } else if (res == WPA_INVALID_AKMP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800217 reason = WLAN_REASON_AKMP_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700218 status = WLAN_STATUS_AKMP_NOT_VALID;
219 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700220#ifdef CONFIG_IEEE80211W
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700221 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800222 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700223 status = WLAN_STATUS_INVALID_IE;
224 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800225 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700226 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
227 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700228#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700229 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800230 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700231 status = WLAN_STATUS_INVALID_IE;
232 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800233 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700234 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700235#ifdef CONFIG_IEEE80211W
236 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
237 sta->sa_query_count > 0)
238 ap_check_sa_query_timeout(hapd, sta);
239 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
240 (sta->auth_alg != WLAN_AUTH_FT)) {
241 /*
242 * STA has already been associated with MFP and SA
243 * Query timeout has not been reached. Reject the
244 * association attempt temporarily and start SA Query,
245 * if one is not pending.
246 */
247
248 if (sta->sa_query_count == 0)
249 ap_sta_start_sa_query(hapd, sta);
250
251#ifdef CONFIG_IEEE80211R
252 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
253
254 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
255
256 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
257 p - buf);
258#endif /* CONFIG_IEEE80211R */
259 return 0;
260 }
261
262 if (wpa_auth_uses_mfp(sta->wpa_sm))
263 sta->flags |= WLAN_STA_MFP;
264 else
265 sta->flags &= ~WLAN_STA_MFP;
266#endif /* CONFIG_IEEE80211W */
267
268#ifdef CONFIG_IEEE80211R
269 if (sta->auth_alg == WLAN_AUTH_FT) {
270 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
271 req_ies_len);
272 if (status != WLAN_STATUS_SUCCESS) {
273 if (status == WLAN_STATUS_INVALID_PMKID)
274 reason = WLAN_REASON_INVALID_IE;
275 if (status == WLAN_STATUS_INVALID_MDIE)
276 reason = WLAN_REASON_INVALID_IE;
277 if (status == WLAN_STATUS_INVALID_FTIE)
278 reason = WLAN_REASON_INVALID_IE;
279 goto fail;
280 }
281 }
282#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700283 } else if (hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800284#ifdef CONFIG_WPS
285 struct wpabuf *wps;
286 if (req_ies)
287 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 WPS_IE_VENDOR_TYPE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800289 else
290 wps = NULL;
291#ifdef CONFIG_WPS_STRICT
292 if (wps && wps_validate_assoc_req(wps) < 0) {
293 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700294 status = WLAN_STATUS_INVALID_IE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295 wpabuf_free(wps);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800296 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700297 }
298#endif /* CONFIG_WPS_STRICT */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800299 if (wps) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700300 sta->flags |= WLAN_STA_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800301 if (wps_is_20(wps)) {
302 wpa_printf(MSG_DEBUG, "WPS: STA supports "
303 "WPS 2.0");
304 sta->flags |= WLAN_STA_WPS2;
305 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700306 } else
307 sta->flags |= WLAN_STA_MAYBE_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308 wpabuf_free(wps);
309#endif /* CONFIG_WPS */
Dmitry Shmidtf21452a2014-02-26 10:55:25 -0800310#ifdef CONFIG_HS20
311 } else if (hapd->conf->osen) {
312 if (elems.osen == NULL) {
313 hostapd_logger(
314 hapd, sta->addr, HOSTAPD_MODULE_IEEE80211,
315 HOSTAPD_LEVEL_INFO,
316 "No HS 2.0 OSEN element in association request");
317 return WLAN_STATUS_INVALID_IE;
318 }
319
320 wpa_printf(MSG_DEBUG, "HS 2.0: OSEN association");
321 if (sta->wpa_sm == NULL)
322 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
323 sta->addr, NULL);
324 if (sta->wpa_sm == NULL) {
325 wpa_printf(MSG_WARNING, "Failed to initialize WPA "
326 "state machine");
327 return WLAN_STATUS_UNSPECIFIED_FAILURE;
328 }
329 if (wpa_validate_osen(hapd->wpa_auth, sta->wpa_sm,
330 elems.osen - 2, elems.osen_len + 2) < 0)
331 return WLAN_STATUS_INVALID_IE;
332#endif /* CONFIG_HS20 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700333 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700335skip_wpa_check:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700337
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700338#ifdef CONFIG_IEEE80211R
339 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
340 sta->auth_alg, req_ies, req_ies_len);
341
342 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700343
344 if (sta->auth_alg == WLAN_AUTH_FT)
345 ap_sta_set_authorized(hapd, sta, 1);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700346#else /* CONFIG_IEEE80211R */
347 /* Keep compiler silent about unused variables */
348 if (status) {
349 }
350#endif /* CONFIG_IEEE80211R */
351
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700352 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
353 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800354 sta->flags &= ~WLAN_STA_WNM_SLEEP_MODE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700355
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700356 hostapd_set_sta_flags(hapd, sta);
357
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700358 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
359 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
360 else
361 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700362
363 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
364
365 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
366
367#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800368 if (req_ies) {
369 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
370 req_ies, req_ies_len);
371 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700372#endif /* CONFIG_P2P */
373
374 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800375
376fail:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700377#ifdef CONFIG_IEEE80211R
378 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
379#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800380 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
381 ap_free_sta(hapd, sta);
382 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700383}
384
385
386void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
387{
388 struct sta_info *sta;
389
390 if (addr == NULL) {
391 /*
392 * This could potentially happen with unexpected event from the
393 * driver wrapper. This was seen at least in one case where the
394 * driver ended up reporting a station mode event while hostapd
395 * was running, so better make sure we stop processing such an
396 * event here.
397 */
398 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
399 "with no address");
400 return;
401 }
402
403 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
404 HOSTAPD_LEVEL_INFO, "disassociated");
405
406 sta = ap_get_sta(hapd, addr);
407 if (sta == NULL) {
408 wpa_printf(MSG_DEBUG, "Disassociation notification for "
409 "unknown STA " MACSTR, MAC2STR(addr));
410 return;
411 }
412
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800413 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700414 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700415 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
416 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
417 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
418 ap_free_sta(hapd, sta);
419}
420
421
422void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
423{
424 struct sta_info *sta = ap_get_sta(hapd, addr);
425
426 if (!sta || !hapd->conf->disassoc_low_ack)
427 return;
428
429 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
430 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
431 "missing ACKs");
432 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
433 if (sta)
434 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
435}
436
437
Dmitry Shmidt04949592012-07-19 12:16:46 -0700438void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800439 int offset, int width, int cf1, int cf2)
Dmitry Shmidt04949592012-07-19 12:16:46 -0700440{
441#ifdef NEED_AP_MLME
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800442 int channel, chwidth, seg0_idx = 0, seg1_idx = 0, is_dfs;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700443
444 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800445 HOSTAPD_LEVEL_INFO,
446 "driver had channel switch: freq=%d, ht=%d, offset=%d, width=%d (%s), cf1=%d, cf2=%d",
447 freq, ht, offset, width, channel_width_to_string(width),
448 cf1, cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700449
450 hapd->iface->freq = freq;
451
452 channel = hostapd_hw_get_channel(hapd, freq);
453 if (!channel) {
454 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
455 HOSTAPD_LEVEL_WARNING, "driver switched to "
456 "bad channel!");
457 return;
458 }
459
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800460 switch (width) {
461 case CHAN_WIDTH_80:
462 chwidth = VHT_CHANWIDTH_80MHZ;
463 break;
464 case CHAN_WIDTH_80P80:
465 chwidth = VHT_CHANWIDTH_80P80MHZ;
466 break;
467 case CHAN_WIDTH_160:
468 chwidth = VHT_CHANWIDTH_160MHZ;
469 break;
470 case CHAN_WIDTH_20_NOHT:
471 case CHAN_WIDTH_20:
472 case CHAN_WIDTH_40:
473 default:
474 chwidth = VHT_CHANWIDTH_USE_HT;
475 break;
476 }
477
478 switch (hapd->iface->current_mode->mode) {
479 case HOSTAPD_MODE_IEEE80211A:
480 if (cf1 > 5000)
481 seg0_idx = (cf1 - 5000) / 5;
482 if (cf2 > 5000)
483 seg1_idx = (cf2 - 5000) / 5;
484 break;
485 default:
486 seg0_idx = hostapd_hw_get_channel(hapd, cf1);
487 seg1_idx = hostapd_hw_get_channel(hapd, cf2);
488 break;
489 }
490
Dmitry Shmidt04949592012-07-19 12:16:46 -0700491 hapd->iconf->channel = channel;
492 hapd->iconf->ieee80211n = ht;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800493 if (!ht)
494 hapd->iconf->ieee80211ac = 0;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700495 hapd->iconf->secondary_channel = offset;
Dmitry Shmidt04f534e2013-12-09 15:50:16 -0800496 hapd->iconf->vht_oper_chwidth = chwidth;
497 hapd->iconf->vht_oper_centr_freq_seg0_idx = seg0_idx;
498 hapd->iconf->vht_oper_centr_freq_seg1_idx = seg1_idx;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800499
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800500 is_dfs = ieee80211_is_dfs(freq);
501
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700502 if (hapd->csa_in_progress &&
503 freq == hapd->cs_freq_params.freq) {
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800504 hostapd_cleanup_cs_params(hapd);
Dmitry Shmidtd30ac602014-06-30 09:54:22 -0700505 ieee802_11_set_beacon(hapd);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800506
Dmitry Shmidt203eadb2015-03-05 14:16:04 -0800507 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
508 "freq=%d dfs=%d", freq, is_dfs);
509 } else if (hapd->iface->drv_flags & WPA_DRIVER_FLAGS_DFS_OFFLOAD) {
510 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_CSA_FINISHED
511 "freq=%d dfs=%d", freq, is_dfs);
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -0800512 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700513#endif /* NEED_AP_MLME */
514}
515
516
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800517void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
518 const u8 *addr, int reason_code)
519{
520 switch (reason_code) {
521 case MAX_CLIENT_REACHED:
522 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
523 MAC2STR(addr));
524 break;
525 case BLOCKED_CLIENT:
526 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
527 MAC2STR(addr));
528 break;
529 }
530}
531
532
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800533#ifdef CONFIG_ACS
534static void hostapd_acs_channel_selected(struct hostapd_data *hapd,
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700535 struct acs_selected_channels *acs_res)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800536{
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800537 int ret;
538
539 if (hapd->iconf->channel) {
540 wpa_printf(MSG_INFO, "ACS: Channel was already set to %d",
541 hapd->iconf->channel);
542 return;
543 }
544
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700545 hapd->iface->freq = hostapd_hw_get_freq(hapd, acs_res->pri_channel);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800546
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700547 if (!acs_res->pri_channel) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800548 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
549 HOSTAPD_LEVEL_WARNING,
550 "driver switched to bad channel");
551 return;
552 }
553
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700554 hapd->iconf->channel = acs_res->pri_channel;
555 hapd->iconf->acs = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800556
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700557 if (acs_res->sec_channel == 0)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800558 hapd->iconf->secondary_channel = 0;
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;
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700561 else if (acs_res->sec_channel > acs_res->pri_channel)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800562 hapd->iconf->secondary_channel = 1;
563 else {
564 wpa_printf(MSG_ERROR, "Invalid secondary channel!");
565 return;
566 }
567
Dmitry Shmidtdda10c22015-03-24 16:05:01 -0700568 if (hapd->iface->conf->ieee80211ac) {
569 /* set defaults for backwards compatibility */
570 hapd->iconf->vht_oper_centr_freq_seg1_idx = 0;
571 hapd->iconf->vht_oper_centr_freq_seg0_idx = 0;
572 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_USE_HT;
573 if (acs_res->ch_width == 80) {
574 hapd->iconf->vht_oper_centr_freq_seg0_idx =
575 acs_res->vht_seg0_center_ch;
576 hapd->iconf->vht_oper_chwidth = VHT_CHANWIDTH_80MHZ;
577 } else if (acs_res->ch_width == 160) {
578 if (acs_res->vht_seg1_center_ch == 0) {
579 hapd->iconf->vht_oper_centr_freq_seg0_idx =
580 acs_res->vht_seg0_center_ch;
581 hapd->iconf->vht_oper_chwidth =
582 VHT_CHANWIDTH_160MHZ;
583 } else {
584 hapd->iconf->vht_oper_centr_freq_seg0_idx =
585 acs_res->vht_seg0_center_ch;
586 hapd->iconf->vht_oper_centr_freq_seg1_idx =
587 acs_res->vht_seg1_center_ch;
588 hapd->iconf->vht_oper_chwidth =
589 VHT_CHANWIDTH_80P80MHZ;
590 }
591 }
592 }
593
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800594 ret = hostapd_acs_completed(hapd->iface, 0);
595 if (ret) {
596 wpa_printf(MSG_ERROR,
597 "ACS: Possibly channel configuration is invalid");
598 }
599}
600#endif /* CONFIG_ACS */
601
602
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800603int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700604 const u8 *bssid, const u8 *ie, size_t ie_len,
605 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700606{
607 size_t i;
608 int ret = 0;
609
610 if (sa == NULL || ie == NULL)
611 return -1;
612
613 random_add_randomness(sa, ETH_ALEN);
614 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
615 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700616 sa, da, bssid, ie, ie_len,
617 ssi_signal) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700618 ret = 1;
619 break;
620 }
621 }
622 return ret;
623}
624
625
626#ifdef HOSTAPD
627
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700628#ifdef CONFIG_IEEE80211R
629static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
630 const u8 *bssid,
631 u16 auth_transaction, u16 status,
632 const u8 *ies, size_t ies_len)
633{
634 struct hostapd_data *hapd = ctx;
635 struct sta_info *sta;
636
637 sta = ap_get_sta(hapd, dst);
638 if (sta == NULL)
639 return;
640
641 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
642 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
643 sta->flags |= WLAN_STA_AUTH;
644
645 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
646}
647#endif /* CONFIG_IEEE80211R */
648
649
650static void hostapd_notif_auth(struct hostapd_data *hapd,
651 struct auth_info *rx_auth)
652{
653 struct sta_info *sta;
654 u16 status = WLAN_STATUS_SUCCESS;
655 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
656 size_t resp_ies_len = 0;
657
658 sta = ap_get_sta(hapd, rx_auth->peer);
659 if (!sta) {
660 sta = ap_sta_add(hapd, rx_auth->peer);
661 if (sta == NULL) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700662 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700663 goto fail;
664 }
665 }
666 sta->flags &= ~WLAN_STA_PREAUTH;
667 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
668#ifdef CONFIG_IEEE80211R
669 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
670 sta->auth_alg = WLAN_AUTH_FT;
671 if (sta->wpa_sm == NULL)
672 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
Dmitry Shmidt391c59f2013-09-03 12:16:28 -0700673 sta->addr, NULL);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700674 if (sta->wpa_sm == NULL) {
675 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
676 "state machine");
677 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
678 goto fail;
679 }
680 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
681 rx_auth->auth_transaction, rx_auth->ies,
682 rx_auth->ies_len,
683 hostapd_notify_auth_ft_finish, hapd);
684 return;
685 }
686#endif /* CONFIG_IEEE80211R */
687fail:
688 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
689 status, resp_ies, resp_ies_len);
690}
691
692
693static void hostapd_action_rx(struct hostapd_data *hapd,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800694 struct rx_mgmt *drv_mgmt)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700695{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800696 struct ieee80211_mgmt *mgmt;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700697 struct sta_info *sta;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800698 size_t plen __maybe_unused;
699 u16 fc;
700
701 if (drv_mgmt->frame_len < 24 + 1)
702 return;
703
704 plen = drv_mgmt->frame_len - 24 - 1;
705
706 mgmt = (struct ieee80211_mgmt *) drv_mgmt->frame;
707 fc = le_to_host16(mgmt->frame_control);
708 if (WLAN_FC_GET_STYPE(fc) != WLAN_FC_STYPE_ACTION)
709 return; /* handled by the driver */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700710
711 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800712 mgmt->u.action.category, (int) plen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700713
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800714 sta = ap_get_sta(hapd, mgmt->sa);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700715 if (sta == NULL) {
716 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
717 return;
718 }
719#ifdef CONFIG_IEEE80211R
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800720 if (mgmt->u.action.category == WLAN_ACTION_FT) {
721 const u8 *payload = drv_mgmt->frame + 24 + 1;
722 wpa_ft_action_rx(sta->wpa_sm, payload, plen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700723 }
724#endif /* CONFIG_IEEE80211R */
725#ifdef CONFIG_IEEE80211W
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800726 if (mgmt->u.action.category == WLAN_ACTION_SA_QUERY && plen >= 4) {
727 ieee802_11_sa_query_action(
728 hapd, mgmt->sa,
729 mgmt->u.action.u.sa_query_resp.action,
730 mgmt->u.action.u.sa_query_resp.trans_id);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700731 }
732#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800733#ifdef CONFIG_WNM
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800734 if (mgmt->u.action.category == WLAN_ACTION_WNM) {
735 ieee802_11_rx_wnm_action_ap(hapd, mgmt, drv_mgmt->frame_len);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700736 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800737#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700738}
739
740
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700741#ifdef NEED_AP_MLME
742
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700743#define HAPD_BROADCAST ((struct hostapd_data *) -1)
744
745static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
746 const u8 *bssid)
747{
748 size_t i;
749
750 if (bssid == NULL)
751 return NULL;
752 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
753 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
754 return HAPD_BROADCAST;
755
756 for (i = 0; i < iface->num_bss; i++) {
757 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
758 return iface->bss[i];
759 }
760
761 return NULL;
762}
763
764
765static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800766 const u8 *bssid, const u8 *addr,
767 int wds)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700768{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800769 hapd = get_hapd_bssid(hapd->iface, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700770 if (hapd == NULL || hapd == HAPD_BROADCAST)
771 return;
772
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800773 ieee802_11_rx_from_unknown(hapd, addr, wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700774}
775
776
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800777static int hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700778{
779 struct hostapd_iface *iface = hapd->iface;
780 const struct ieee80211_hdr *hdr;
781 const u8 *bssid;
782 struct hostapd_frame_info fi;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800783 int ret;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784
Dmitry Shmidt7d5c8f22014-03-03 13:53:28 -0800785#ifdef CONFIG_TESTING_OPTIONS
786 if (hapd->ext_mgmt_frame_handling) {
787 size_t hex_len = 2 * rx_mgmt->frame_len + 1;
788 char *hex = os_malloc(hex_len);
789 if (hex) {
790 wpa_snprintf_hex(hex, hex_len, rx_mgmt->frame,
791 rx_mgmt->frame_len);
792 wpa_msg(hapd->msg_ctx, MSG_INFO, "MGMT-RX %s", hex);
793 os_free(hex);
794 }
795 return 1;
796 }
797#endif /* CONFIG_TESTING_OPTIONS */
798
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
800 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
801 if (bssid == NULL)
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800802 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803
804 hapd = get_hapd_bssid(iface, bssid);
805 if (hapd == NULL) {
806 u16 fc;
807 fc = le_to_host16(hdr->frame_control);
808
809 /*
810 * Drop frames to unknown BSSIDs except for Beacon frames which
811 * could be used to update neighbor information.
812 */
813 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
814 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
815 hapd = iface->bss[0];
816 else
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800817 return 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700818 }
819
820 os_memset(&fi, 0, sizeof(fi));
821 fi.datarate = rx_mgmt->datarate;
822 fi.ssi_signal = rx_mgmt->ssi_signal;
823
824 if (hapd == HAPD_BROADCAST) {
825 size_t i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800826 ret = 0;
827 for (i = 0; i < iface->num_bss; i++) {
Dmitry Shmidt98660862014-03-11 17:26:21 -0700828 /* if bss is set, driver will call this function for
829 * each bss individually. */
830 if (rx_mgmt->drv_priv &&
831 (iface->bss[i]->drv_priv != rx_mgmt->drv_priv))
832 continue;
833
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800834 if (ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
835 rx_mgmt->frame_len, &fi) > 0)
836 ret = 1;
837 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838 } else
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800839 ret = ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len,
840 &fi);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700841
842 random_add_randomness(&fi, sizeof(fi));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700843
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800844 return ret;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700845}
846
847
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
849 size_t len, u16 stype, int ok)
850{
851 struct ieee80211_hdr *hdr;
852 hdr = (struct ieee80211_hdr *) buf;
853 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
854 if (hapd == NULL || hapd == HAPD_BROADCAST)
855 return;
856 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
857}
858
859#endif /* NEED_AP_MLME */
860
861
862static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
863{
864 struct sta_info *sta = ap_get_sta(hapd, addr);
865 if (sta)
866 return 0;
867
868 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
869 " - adding a new STA", MAC2STR(addr));
870 sta = ap_sta_add(hapd, addr);
871 if (sta) {
872 hostapd_new_assoc_sta(hapd, sta, 0);
873 } else {
874 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
875 MAC2STR(addr));
876 return -1;
877 }
878
879 return 0;
880}
881
882
883static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
884 const u8 *data, size_t data_len)
885{
886 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800887 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700888 size_t j;
889
890 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800891 if ((sta = ap_get_sta(iface->bss[j], src))) {
892 if (sta->flags & WLAN_STA_ASSOC) {
893 hapd = iface->bss[j];
894 break;
895 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700896 }
897 }
898
899 ieee802_1x_receive(hapd, src, data, data_len);
900}
901
902
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700903static struct hostapd_channel_data * hostapd_get_mode_channel(
904 struct hostapd_iface *iface, unsigned int freq)
905{
906 int i;
907 struct hostapd_channel_data *chan;
908
909 for (i = 0; i < iface->current_mode->num_channels; i++) {
910 chan = &iface->current_mode->channels[i];
911 if (!chan)
912 return NULL;
913 if ((unsigned int) chan->freq == freq)
914 return chan;
915 }
916
917 return NULL;
918}
919
920
921static void hostapd_update_nf(struct hostapd_iface *iface,
922 struct hostapd_channel_data *chan,
923 struct freq_survey *survey)
924{
925 if (!iface->chans_surveyed) {
926 chan->min_nf = survey->nf;
927 iface->lowest_nf = survey->nf;
928 } else {
929 if (dl_list_empty(&chan->survey_list))
930 chan->min_nf = survey->nf;
931 else if (survey->nf < chan->min_nf)
932 chan->min_nf = survey->nf;
933 if (survey->nf < iface->lowest_nf)
934 iface->lowest_nf = survey->nf;
935 }
936}
937
938
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800939static void hostapd_single_channel_get_survey(struct hostapd_iface *iface,
940 struct survey_results *survey_res)
941{
942 struct hostapd_channel_data *chan;
943 struct freq_survey *survey;
944 u64 divisor, dividend;
945
946 survey = dl_list_first(&survey_res->survey_list, struct freq_survey,
947 list);
948 if (!survey || !survey->freq)
949 return;
950
951 chan = hostapd_get_mode_channel(iface, survey->freq);
952 if (!chan || chan->flag & HOSTAPD_CHAN_DISABLED)
953 return;
954
955 wpa_printf(MSG_DEBUG, "Single Channel Survey: (freq=%d channel_time=%ld channel_time_busy=%ld)",
956 survey->freq,
957 (unsigned long int) survey->channel_time,
958 (unsigned long int) survey->channel_time_busy);
959
960 if (survey->channel_time > iface->last_channel_time &&
961 survey->channel_time > survey->channel_time_busy) {
962 dividend = survey->channel_time_busy -
963 iface->last_channel_time_busy;
964 divisor = survey->channel_time - iface->last_channel_time;
965
966 iface->channel_utilization = dividend * 255 / divisor;
967 wpa_printf(MSG_DEBUG, "Channel Utilization: %d",
968 iface->channel_utilization);
969 }
970 iface->last_channel_time = survey->channel_time;
971 iface->last_channel_time_busy = survey->channel_time_busy;
972}
973
974
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700975static void hostapd_event_get_survey(struct hostapd_data *hapd,
976 struct survey_results *survey_results)
977{
978 struct hostapd_iface *iface = hapd->iface;
979 struct freq_survey *survey, *tmp;
980 struct hostapd_channel_data *chan;
981
982 if (dl_list_empty(&survey_results->survey_list)) {
983 wpa_printf(MSG_DEBUG, "No survey data received");
984 return;
985 }
986
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800987 if (survey_results->freq_filter) {
988 hostapd_single_channel_get_survey(iface, survey_results);
989 return;
990 }
991
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700992 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
993 struct freq_survey, list) {
994 chan = hostapd_get_mode_channel(iface, survey->freq);
995 if (!chan)
996 continue;
997 if (chan->flag & HOSTAPD_CHAN_DISABLED)
998 continue;
999
1000 dl_list_del(&survey->list);
1001 dl_list_add_tail(&chan->survey_list, &survey->list);
1002
1003 hostapd_update_nf(iface, chan, survey);
1004
1005 iface->chans_surveyed++;
1006 }
1007}
1008
1009
Dmitry Shmidt051af732013-10-22 13:52:46 -07001010#ifdef NEED_AP_MLME
1011
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001012static void hostapd_event_iface_unavailable(struct hostapd_data *hapd)
1013{
1014 wpa_printf(MSG_DEBUG, "Interface %s is unavailable -- stopped",
1015 hapd->conf->iface);
1016
1017 if (hapd->csa_in_progress) {
1018 wpa_printf(MSG_INFO, "CSA failed (%s was stopped)",
1019 hapd->conf->iface);
1020 hostapd_switch_channel_fallback(hapd->iface,
1021 &hapd->cs_freq_params);
1022 }
1023}
1024
1025
Dmitry Shmidt051af732013-10-22 13:52:46 -07001026static void hostapd_event_dfs_radar_detected(struct hostapd_data *hapd,
1027 struct dfs_event *radar)
1028{
1029 wpa_printf(MSG_DEBUG, "DFS radar detected on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001030 hostapd_dfs_radar_detected(hapd->iface, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001031 radar->chan_offset, radar->chan_width,
1032 radar->cf1, radar->cf2);
1033}
1034
1035
1036static void hostapd_event_dfs_cac_finished(struct hostapd_data *hapd,
1037 struct dfs_event *radar)
1038{
1039 wpa_printf(MSG_DEBUG, "DFS CAC finished on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001040 hostapd_dfs_complete_cac(hapd->iface, 1, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001041 radar->chan_offset, radar->chan_width,
1042 radar->cf1, radar->cf2);
1043}
1044
1045
1046static void hostapd_event_dfs_cac_aborted(struct hostapd_data *hapd,
1047 struct dfs_event *radar)
1048{
1049 wpa_printf(MSG_DEBUG, "DFS CAC aborted on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001050 hostapd_dfs_complete_cac(hapd->iface, 0, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001051 radar->chan_offset, radar->chan_width,
1052 radar->cf1, radar->cf2);
1053}
1054
1055
1056static void hostapd_event_dfs_nop_finished(struct hostapd_data *hapd,
1057 struct dfs_event *radar)
1058{
1059 wpa_printf(MSG_DEBUG, "DFS NOP finished on %d MHz", radar->freq);
Dmitry Shmidtcce06662013-11-04 18:44:24 -08001060 hostapd_dfs_nop_finished(hapd->iface, radar->freq, radar->ht_enabled,
Dmitry Shmidt051af732013-10-22 13:52:46 -07001061 radar->chan_offset, radar->chan_width,
1062 radar->cf1, radar->cf2);
1063}
1064
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001065
1066static void hostapd_event_dfs_cac_started(struct hostapd_data *hapd,
1067 struct dfs_event *radar)
1068{
1069 wpa_printf(MSG_DEBUG, "DFS offload CAC started on %d MHz", radar->freq);
1070 hostapd_dfs_start_cac(hapd->iface, radar->freq, radar->ht_enabled,
1071 radar->chan_offset, radar->chan_width,
1072 radar->cf1, radar->cf2);
1073}
1074
Dmitry Shmidt051af732013-10-22 13:52:46 -07001075#endif /* NEED_AP_MLME */
1076
1077
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
1079 union wpa_event_data *data)
1080{
1081 struct hostapd_data *hapd = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001082#ifndef CONFIG_NO_STDOUT_DEBUG
1083 int level = MSG_DEBUG;
1084
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001085 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001086 data->rx_mgmt.frame_len >= 24) {
1087 const struct ieee80211_hdr *hdr;
1088 u16 fc;
1089 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
1090 fc = le_to_host16(hdr->frame_control);
1091 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1092 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
1093 level = MSG_EXCESSIVE;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001094 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
1095 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
1096 level = MSG_EXCESSIVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001097 }
1098
1099 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
1100 event_to_string(event), event);
1101#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001102
1103 switch (event) {
1104 case EVENT_MICHAEL_MIC_FAILURE:
1105 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
1106 break;
1107 case EVENT_SCAN_RESULTS:
1108 if (hapd->iface->scan_cb)
1109 hapd->iface->scan_cb(hapd->iface);
1110 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001111 case EVENT_WPS_BUTTON_PUSHED:
1112 hostapd_wps_button_pushed(hapd, NULL);
1113 break;
1114#ifdef NEED_AP_MLME
1115 case EVENT_TX_STATUS:
1116 switch (data->tx_status.type) {
1117 case WLAN_FC_TYPE_MGMT:
1118 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
1119 data->tx_status.data_len,
1120 data->tx_status.stype,
1121 data->tx_status.ack);
1122 break;
1123 case WLAN_FC_TYPE_DATA:
1124 hostapd_tx_status(hapd, data->tx_status.dst,
1125 data->tx_status.data,
1126 data->tx_status.data_len,
1127 data->tx_status.ack);
1128 break;
1129 }
1130 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001131 case EVENT_EAPOL_TX_STATUS:
1132 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
1133 data->eapol_tx_status.data,
1134 data->eapol_tx_status.data_len,
1135 data->eapol_tx_status.ack);
1136 break;
1137 case EVENT_DRIVER_CLIENT_POLL_OK:
1138 hostapd_client_poll_ok(hapd, data->client_poll.addr);
1139 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001140 case EVENT_RX_FROM_UNKNOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001141 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
1142 data->rx_from_unknown.addr,
1143 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001145#endif /* NEED_AP_MLME */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001146 case EVENT_RX_MGMT:
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07001147 if (!data->rx_mgmt.frame)
1148 break;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001149#ifdef NEED_AP_MLME
1150 if (hostapd_mgmt_rx(hapd, &data->rx_mgmt) > 0)
1151 break;
1152#endif /* NEED_AP_MLME */
1153 hostapd_action_rx(hapd, &data->rx_mgmt);
1154 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 case EVENT_RX_PROBE_REQ:
1156 if (data->rx_probe_req.sa == NULL ||
1157 data->rx_probe_req.ie == NULL)
1158 break;
1159 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001160 data->rx_probe_req.da,
1161 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001162 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001163 data->rx_probe_req.ie_len,
1164 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001165 break;
1166 case EVENT_NEW_STA:
1167 hostapd_event_new_sta(hapd, data->new_sta.addr);
1168 break;
1169 case EVENT_EAPOL_RX:
1170 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
1171 data->eapol_rx.data,
1172 data->eapol_rx.data_len);
1173 break;
1174 case EVENT_ASSOC:
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001175 if (!data)
1176 return;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001177 hostapd_notif_assoc(hapd, data->assoc_info.addr,
1178 data->assoc_info.req_ies,
1179 data->assoc_info.req_ies_len,
1180 data->assoc_info.reassoc);
1181 break;
1182 case EVENT_DISASSOC:
1183 if (data)
1184 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
1185 break;
1186 case EVENT_DEAUTH:
1187 if (data)
1188 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
1189 break;
1190 case EVENT_STATION_LOW_ACK:
1191 if (!data)
1192 break;
1193 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
1194 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001195 case EVENT_AUTH:
1196 hostapd_notif_auth(hapd, &data->auth);
1197 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001198 case EVENT_CH_SWITCH:
1199 if (!data)
1200 break;
1201 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
1202 data->ch_switch.ht_enabled,
Dmitry Shmidt04f534e2013-12-09 15:50:16 -08001203 data->ch_switch.ch_offset,
1204 data->ch_switch.ch_width,
1205 data->ch_switch.cf1,
1206 data->ch_switch.cf2);
Dmitry Shmidt04949592012-07-19 12:16:46 -07001207 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001208 case EVENT_CONNECT_FAILED_REASON:
1209 if (!data)
1210 break;
1211 hostapd_event_connect_failed_reason(
1212 hapd, data->connect_failed_reason.addr,
1213 data->connect_failed_reason.code);
1214 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001215 case EVENT_SURVEY:
1216 hostapd_event_get_survey(hapd, &data->survey_results);
1217 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001218#ifdef NEED_AP_MLME
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07001219 case EVENT_INTERFACE_UNAVAILABLE:
1220 hostapd_event_iface_unavailable(hapd);
1221 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001222 case EVENT_DFS_RADAR_DETECTED:
1223 if (!data)
1224 break;
1225 hostapd_event_dfs_radar_detected(hapd, &data->dfs_event);
1226 break;
1227 case EVENT_DFS_CAC_FINISHED:
1228 if (!data)
1229 break;
1230 hostapd_event_dfs_cac_finished(hapd, &data->dfs_event);
1231 break;
1232 case EVENT_DFS_CAC_ABORTED:
1233 if (!data)
1234 break;
1235 hostapd_event_dfs_cac_aborted(hapd, &data->dfs_event);
1236 break;
1237 case EVENT_DFS_NOP_FINISHED:
1238 if (!data)
1239 break;
1240 hostapd_event_dfs_nop_finished(hapd, &data->dfs_event);
1241 break;
1242 case EVENT_CHANNEL_LIST_CHANGED:
1243 /* channel list changed (regulatory?), update channel list */
1244 /* TODO: check this. hostapd_get_hw_features() initializes
1245 * too much stuff. */
1246 /* hostapd_get_hw_features(hapd->iface); */
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001247 hostapd_channel_list_updated(
1248 hapd->iface, data->channel_list_changed.initiator);
Dmitry Shmidt051af732013-10-22 13:52:46 -07001249 break;
Dmitry Shmidt203eadb2015-03-05 14:16:04 -08001250 case EVENT_DFS_CAC_STARTED:
1251 if (!data)
1252 break;
1253 hostapd_event_dfs_cac_started(hapd, &data->dfs_event);
1254 break;
Dmitry Shmidt051af732013-10-22 13:52:46 -07001255#endif /* NEED_AP_MLME */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001256 case EVENT_INTERFACE_ENABLED:
1257 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_ENABLED);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001258 if (hapd->disabled && hapd->started) {
1259 hapd->disabled = 0;
1260 /*
1261 * Try to re-enable interface if the driver stopped it
1262 * when the interface got disabled.
1263 */
1264 wpa_auth_reconfig_group_keys(hapd->wpa_auth);
1265 hapd->reenable_beacon = 1;
1266 ieee802_11_set_beacon(hapd);
1267 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001268 break;
1269 case EVENT_INTERFACE_DISABLED:
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001270 hostapd_free_stas(hapd);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001271 wpa_msg(hapd->msg_ctx, MSG_INFO, INTERFACE_DISABLED);
Dmitry Shmidt7f656022015-02-25 14:36:37 -08001272 hapd->disabled = 1;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001273 break;
1274#ifdef CONFIG_ACS
1275 case EVENT_ACS_CHANNEL_SELECTED:
Dmitry Shmidtdda10c22015-03-24 16:05:01 -07001276 hostapd_acs_channel_selected(hapd,
1277 &data->acs_selected_channels);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001278 break;
1279#endif /* CONFIG_ACS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001280 default:
1281 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
1282 break;
1283 }
1284}
1285
1286#endif /* HOSTAPD */