blob: e3ba224e9de0addfa783613d9aaed04753bf58ea [file] [log] [blame]
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001/*
2 * hostapd / Callback functions for driver wrappers
3 * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
4 *
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"
12#include "radius/radius.h"
13#include "drivers/driver.h"
14#include "common/ieee802_11_defs.h"
15#include "common/ieee802_11_common.h"
Dmitry Shmidtf8623282013-02-20 14:34:59 -080016#include "common/wpa_ctrl.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070017#include "crypto/random.h"
18#include "p2p/p2p.h"
19#include "wps/wps.h"
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070020#include "wnm_ap.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070021#include "hostapd.h"
22#include "ieee802_11.h"
23#include "sta_info.h"
24#include "accounting.h"
25#include "tkip_countermeasures.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070026#include "ieee802_1x.h"
27#include "wpa_auth.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070028#include "wps_hostapd.h"
29#include "ap_drv_ops.h"
30#include "ap_config.h"
Dmitry Shmidt04949592012-07-19 12:16:46 -070031#include "hw_features.h"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070032
33
34int hostapd_notif_assoc(struct hostapd_data *hapd, const u8 *addr,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080035 const u8 *req_ies, size_t req_ies_len, int reassoc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070036{
37 struct sta_info *sta;
38 int new_assoc, res;
39 struct ieee802_11_elems elems;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080040 const u8 *ie;
41 size_t ielen;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070042#ifdef CONFIG_IEEE80211R
43 u8 buf[sizeof(struct ieee80211_mgmt) + 1024];
44 u8 *p = buf;
45#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080046 u16 reason = WLAN_REASON_UNSPECIFIED;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -070047 u16 status = WLAN_STATUS_SUCCESS;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49 if (addr == NULL) {
50 /*
51 * This could potentially happen with unexpected event from the
52 * driver wrapper. This was seen at least in one case where the
53 * driver ended up being set to station mode while hostapd was
54 * running, so better make sure we stop processing such an
55 * event here.
56 */
57 wpa_printf(MSG_DEBUG, "hostapd_notif_assoc: Skip event with "
58 "no address");
59 return -1;
60 }
61 random_add_randomness(addr, ETH_ALEN);
62
63 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
64 HOSTAPD_LEVEL_INFO, "associated");
65
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -080066 ieee802_11_parse_elems(req_ies, req_ies_len, &elems, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070067 if (elems.wps_ie) {
68 ie = elems.wps_ie - 2;
69 ielen = elems.wps_ie_len + 2;
70 wpa_printf(MSG_DEBUG, "STA included WPS IE in (Re)AssocReq");
71 } else if (elems.rsn_ie) {
72 ie = elems.rsn_ie - 2;
73 ielen = elems.rsn_ie_len + 2;
74 wpa_printf(MSG_DEBUG, "STA included RSN IE in (Re)AssocReq");
75 } else if (elems.wpa_ie) {
76 ie = elems.wpa_ie - 2;
77 ielen = elems.wpa_ie_len + 2;
78 wpa_printf(MSG_DEBUG, "STA included WPA IE in (Re)AssocReq");
79 } else {
80 ie = NULL;
81 ielen = 0;
82 wpa_printf(MSG_DEBUG, "STA did not include WPS/RSN/WPA IE in "
83 "(Re)AssocReq");
84 }
85
86 sta = ap_get_sta(hapd, addr);
87 if (sta) {
Dmitry Shmidt34af3062013-07-11 10:46:32 -070088 ap_sta_no_session_timeout(hapd, sta);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070089 accounting_sta_stop(hapd, sta);
Dmitry Shmidt04949592012-07-19 12:16:46 -070090
91 /*
92 * Make sure that the previously registered inactivity timer
93 * will not remove the STA immediately.
94 */
95 sta->timeout_next = STA_NULLFUNC;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070096 } else {
97 sta = ap_sta_add(hapd, addr);
Dmitry Shmidt04949592012-07-19 12:16:46 -070098 if (sta == NULL) {
99 hostapd_drv_sta_disassoc(hapd, addr,
100 WLAN_REASON_DISASSOC_AP_BUSY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700101 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700102 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700103 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800104 sta->flags &= ~(WLAN_STA_WPS | WLAN_STA_MAYBE_WPS | WLAN_STA_WPS2);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700105
106#ifdef CONFIG_P2P
107 if (elems.p2p) {
108 wpabuf_free(sta->p2p_ie);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800109 sta->p2p_ie = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700110 P2P_IE_VENDOR_TYPE);
111 }
112#endif /* CONFIG_P2P */
113
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800114#ifdef CONFIG_HS20
115 wpabuf_free(sta->hs20_ie);
116 if (elems.hs20 && elems.hs20_len > 4) {
117 sta->hs20_ie = wpabuf_alloc_copy(elems.hs20 + 4,
118 elems.hs20_len - 4);
119 } else
120 sta->hs20_ie = NULL;
121#endif /* CONFIG_HS20 */
122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 if (hapd->conf->wpa) {
124 if (ie == NULL || ielen == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800125#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700126 if (hapd->conf->wps_state) {
127 wpa_printf(MSG_DEBUG, "STA did not include "
128 "WPA/RSN IE in (Re)Association "
129 "Request - possible WPS use");
130 sta->flags |= WLAN_STA_MAYBE_WPS;
131 goto skip_wpa_check;
132 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800133#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700134
135 wpa_printf(MSG_DEBUG, "No WPA/RSN IE from STA");
136 return -1;
137 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800138#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700139 if (hapd->conf->wps_state && ie[0] == 0xdd && ie[1] >= 4 &&
140 os_memcmp(ie + 2, "\x00\x50\xf2\x04", 4) == 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800141 struct wpabuf *wps;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700142 sta->flags |= WLAN_STA_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800143 wps = ieee802_11_vendor_ie_concat(ie, ielen,
144 WPS_IE_VENDOR_TYPE);
145 if (wps) {
146 if (wps_is_20(wps)) {
147 wpa_printf(MSG_DEBUG, "WPS: STA "
148 "supports WPS 2.0");
149 sta->flags |= WLAN_STA_WPS2;
150 }
151 wpabuf_free(wps);
152 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700153 goto skip_wpa_check;
154 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800155#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700156
157 if (sta->wpa_sm == NULL)
158 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
159 sta->addr);
160 if (sta->wpa_sm == NULL) {
161 wpa_printf(MSG_ERROR, "Failed to initialize WPA state "
162 "machine");
163 return -1;
164 }
165 res = wpa_validate_wpa_ie(hapd->wpa_auth, sta->wpa_sm,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700166 ie, ielen,
167 elems.mdie, elems.mdie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700168 if (res != WPA_IE_OK) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700169 wpa_printf(MSG_DEBUG, "WPA/RSN information element "
170 "rejected? (res %u)", res);
171 wpa_hexdump(MSG_DEBUG, "IE", ie, ielen);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700172 if (res == WPA_INVALID_GROUP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800173 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700174 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
175 } else if (res == WPA_INVALID_PAIRWISE) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800176 reason = WLAN_REASON_PAIRWISE_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700177 status = WLAN_STATUS_PAIRWISE_CIPHER_NOT_VALID;
178 } else if (res == WPA_INVALID_AKMP) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800179 reason = WLAN_REASON_AKMP_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700180 status = WLAN_STATUS_AKMP_NOT_VALID;
181 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182#ifdef CONFIG_IEEE80211W
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700183 else if (res == WPA_MGMT_FRAME_PROTECTION_VIOLATION) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800184 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700185 status = WLAN_STATUS_INVALID_IE;
186 } else if (res == WPA_INVALID_MGMT_GROUP_CIPHER) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800187 reason = WLAN_REASON_GROUP_CIPHER_NOT_VALID;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700188 status = WLAN_STATUS_GROUP_CIPHER_NOT_VALID;
189 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700190#endif /* CONFIG_IEEE80211W */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700191 else {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800192 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700193 status = WLAN_STATUS_INVALID_IE;
194 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800195 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700196 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700197#ifdef CONFIG_IEEE80211W
198 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
199 sta->sa_query_count > 0)
200 ap_check_sa_query_timeout(hapd, sta);
201 if ((sta->flags & WLAN_STA_MFP) && !sta->sa_query_timed_out &&
202 (sta->auth_alg != WLAN_AUTH_FT)) {
203 /*
204 * STA has already been associated with MFP and SA
205 * Query timeout has not been reached. Reject the
206 * association attempt temporarily and start SA Query,
207 * if one is not pending.
208 */
209
210 if (sta->sa_query_count == 0)
211 ap_sta_start_sa_query(hapd, sta);
212
213#ifdef CONFIG_IEEE80211R
214 status = WLAN_STATUS_ASSOC_REJECTED_TEMPORARILY;
215
216 p = hostapd_eid_assoc_comeback_time(hapd, sta, p);
217
218 hostapd_sta_assoc(hapd, addr, reassoc, status, buf,
219 p - buf);
220#endif /* CONFIG_IEEE80211R */
221 return 0;
222 }
223
224 if (wpa_auth_uses_mfp(sta->wpa_sm))
225 sta->flags |= WLAN_STA_MFP;
226 else
227 sta->flags &= ~WLAN_STA_MFP;
228#endif /* CONFIG_IEEE80211W */
229
230#ifdef CONFIG_IEEE80211R
231 if (sta->auth_alg == WLAN_AUTH_FT) {
232 status = wpa_ft_validate_reassoc(sta->wpa_sm, req_ies,
233 req_ies_len);
234 if (status != WLAN_STATUS_SUCCESS) {
235 if (status == WLAN_STATUS_INVALID_PMKID)
236 reason = WLAN_REASON_INVALID_IE;
237 if (status == WLAN_STATUS_INVALID_MDIE)
238 reason = WLAN_REASON_INVALID_IE;
239 if (status == WLAN_STATUS_INVALID_FTIE)
240 reason = WLAN_REASON_INVALID_IE;
241 goto fail;
242 }
243 }
244#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 } else if (hapd->conf->wps_state) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800246#ifdef CONFIG_WPS
247 struct wpabuf *wps;
248 if (req_ies)
249 wps = ieee802_11_vendor_ie_concat(req_ies, req_ies_len,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700250 WPS_IE_VENDOR_TYPE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800251 else
252 wps = NULL;
253#ifdef CONFIG_WPS_STRICT
254 if (wps && wps_validate_assoc_req(wps) < 0) {
255 reason = WLAN_REASON_INVALID_IE;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700256 status = WLAN_STATUS_INVALID_IE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700257 wpabuf_free(wps);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800258 goto fail;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700259 }
260#endif /* CONFIG_WPS_STRICT */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800261 if (wps) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262 sta->flags |= WLAN_STA_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800263 if (wps_is_20(wps)) {
264 wpa_printf(MSG_DEBUG, "WPS: STA supports "
265 "WPS 2.0");
266 sta->flags |= WLAN_STA_WPS2;
267 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700268 } else
269 sta->flags |= WLAN_STA_MAYBE_WPS;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800270 wpabuf_free(wps);
271#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700272 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800273#ifdef CONFIG_WPS
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700274skip_wpa_check:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800275#endif /* CONFIG_WPS */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700276
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700277#ifdef CONFIG_IEEE80211R
278 p = wpa_sm_write_assoc_resp_ies(sta->wpa_sm, buf, sizeof(buf),
279 sta->auth_alg, req_ies, req_ies_len);
280
281 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
282#else /* CONFIG_IEEE80211R */
283 /* Keep compiler silent about unused variables */
284 if (status) {
285 }
286#endif /* CONFIG_IEEE80211R */
287
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700288 new_assoc = (sta->flags & WLAN_STA_ASSOC) == 0;
289 sta->flags |= WLAN_STA_AUTH | WLAN_STA_ASSOC;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700290
291 if (reassoc && (sta->auth_alg == WLAN_AUTH_FT))
292 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC_FT);
293 else
294 wpa_auth_sm_event(sta->wpa_sm, WPA_ASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295
296 hostapd_new_assoc_sta(hapd, sta, !new_assoc);
297
298 ieee802_1x_notify_port_enabled(sta->eapol_sm, 1);
299
300#ifdef CONFIG_P2P
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800301 if (req_ies) {
302 p2p_group_notif_assoc(hapd->p2p_group, sta->addr,
303 req_ies, req_ies_len);
304 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700305#endif /* CONFIG_P2P */
306
307 return 0;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800308
309fail:
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700310#ifdef CONFIG_IEEE80211R
311 hostapd_sta_assoc(hapd, addr, reassoc, status, buf, p - buf);
312#endif /* CONFIG_IEEE80211R */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800313 hostapd_drv_sta_disassoc(hapd, sta->addr, reason);
314 ap_free_sta(hapd, sta);
315 return -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316}
317
318
319void hostapd_notif_disassoc(struct hostapd_data *hapd, const u8 *addr)
320{
321 struct sta_info *sta;
322
323 if (addr == NULL) {
324 /*
325 * This could potentially happen with unexpected event from the
326 * driver wrapper. This was seen at least in one case where the
327 * driver ended up reporting a station mode event while hostapd
328 * was running, so better make sure we stop processing such an
329 * event here.
330 */
331 wpa_printf(MSG_DEBUG, "hostapd_notif_disassoc: Skip event "
332 "with no address");
333 return;
334 }
335
336 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
337 HOSTAPD_LEVEL_INFO, "disassociated");
338
339 sta = ap_get_sta(hapd, addr);
340 if (sta == NULL) {
341 wpa_printf(MSG_DEBUG, "Disassociation notification for "
342 "unknown STA " MACSTR, MAC2STR(addr));
343 return;
344 }
345
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800346 ap_sta_set_authorized(hapd, sta, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700347 sta->flags &= ~(WLAN_STA_AUTH | WLAN_STA_ASSOC);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700348 wpa_auth_sm_event(sta->wpa_sm, WPA_DISASSOC);
349 sta->acct_terminate_cause = RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST;
350 ieee802_1x_notify_port_enabled(sta->eapol_sm, 0);
351 ap_free_sta(hapd, sta);
352}
353
354
355void hostapd_event_sta_low_ack(struct hostapd_data *hapd, const u8 *addr)
356{
357 struct sta_info *sta = ap_get_sta(hapd, addr);
358
359 if (!sta || !hapd->conf->disassoc_low_ack)
360 return;
361
362 hostapd_logger(hapd, addr, HOSTAPD_MODULE_IEEE80211,
363 HOSTAPD_LEVEL_INFO, "disconnected due to excessive "
364 "missing ACKs");
365 hostapd_drv_sta_disassoc(hapd, addr, WLAN_REASON_DISASSOC_LOW_ACK);
366 if (sta)
367 ap_sta_disassociate(hapd, sta, WLAN_REASON_DISASSOC_LOW_ACK);
368}
369
370
Dmitry Shmidt04949592012-07-19 12:16:46 -0700371void hostapd_event_ch_switch(struct hostapd_data *hapd, int freq, int ht,
372 int offset)
373{
374#ifdef NEED_AP_MLME
375 int channel;
376
377 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
378 HOSTAPD_LEVEL_INFO, "driver had channel switch: "
379 "freq=%d, ht=%d, offset=%d", freq, ht, offset);
380
381 hapd->iface->freq = freq;
382
383 channel = hostapd_hw_get_channel(hapd, freq);
384 if (!channel) {
385 hostapd_logger(hapd, NULL, HOSTAPD_MODULE_IEEE80211,
386 HOSTAPD_LEVEL_WARNING, "driver switched to "
387 "bad channel!");
388 return;
389 }
390
391 hapd->iconf->channel = channel;
392 hapd->iconf->ieee80211n = ht;
393 hapd->iconf->secondary_channel = offset;
394#endif /* NEED_AP_MLME */
395}
396
397
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800398void hostapd_event_connect_failed_reason(struct hostapd_data *hapd,
399 const u8 *addr, int reason_code)
400{
401 switch (reason_code) {
402 case MAX_CLIENT_REACHED:
403 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_MAX_STA MACSTR,
404 MAC2STR(addr));
405 break;
406 case BLOCKED_CLIENT:
407 wpa_msg(hapd->msg_ctx, MSG_INFO, AP_REJECTED_BLOCKED_STA MACSTR,
408 MAC2STR(addr));
409 break;
410 }
411}
412
413
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800414int hostapd_probe_req_rx(struct hostapd_data *hapd, const u8 *sa, const u8 *da,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700415 const u8 *bssid, const u8 *ie, size_t ie_len,
416 int ssi_signal)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700417{
418 size_t i;
419 int ret = 0;
420
421 if (sa == NULL || ie == NULL)
422 return -1;
423
424 random_add_randomness(sa, ETH_ALEN);
425 for (i = 0; hapd->probereq_cb && i < hapd->num_probereq_cb; i++) {
426 if (hapd->probereq_cb[i].cb(hapd->probereq_cb[i].ctx,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700427 sa, da, bssid, ie, ie_len,
428 ssi_signal) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700429 ret = 1;
430 break;
431 }
432 }
433 return ret;
434}
435
436
437#ifdef HOSTAPD
438
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700439#ifdef CONFIG_IEEE80211R
440static void hostapd_notify_auth_ft_finish(void *ctx, const u8 *dst,
441 const u8 *bssid,
442 u16 auth_transaction, u16 status,
443 const u8 *ies, size_t ies_len)
444{
445 struct hostapd_data *hapd = ctx;
446 struct sta_info *sta;
447
448 sta = ap_get_sta(hapd, dst);
449 if (sta == NULL)
450 return;
451
452 hostapd_logger(hapd, dst, HOSTAPD_MODULE_IEEE80211,
453 HOSTAPD_LEVEL_DEBUG, "authentication OK (FT)");
454 sta->flags |= WLAN_STA_AUTH;
455
456 hostapd_sta_auth(hapd, dst, auth_transaction, status, ies, ies_len);
457}
458#endif /* CONFIG_IEEE80211R */
459
460
461static void hostapd_notif_auth(struct hostapd_data *hapd,
462 struct auth_info *rx_auth)
463{
464 struct sta_info *sta;
465 u16 status = WLAN_STATUS_SUCCESS;
466 u8 resp_ies[2 + WLAN_AUTH_CHALLENGE_LEN];
467 size_t resp_ies_len = 0;
468
469 sta = ap_get_sta(hapd, rx_auth->peer);
470 if (!sta) {
471 sta = ap_sta_add(hapd, rx_auth->peer);
472 if (sta == NULL) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700473 status = WLAN_STATUS_AP_UNABLE_TO_HANDLE_NEW_STA;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700474 goto fail;
475 }
476 }
477 sta->flags &= ~WLAN_STA_PREAUTH;
478 ieee802_1x_notify_pre_auth(sta->eapol_sm, 0);
479#ifdef CONFIG_IEEE80211R
480 if (rx_auth->auth_type == WLAN_AUTH_FT && hapd->wpa_auth) {
481 sta->auth_alg = WLAN_AUTH_FT;
482 if (sta->wpa_sm == NULL)
483 sta->wpa_sm = wpa_auth_sta_init(hapd->wpa_auth,
484 sta->addr);
485 if (sta->wpa_sm == NULL) {
486 wpa_printf(MSG_DEBUG, "FT: Failed to initialize WPA "
487 "state machine");
488 status = WLAN_STATUS_UNSPECIFIED_FAILURE;
489 goto fail;
490 }
491 wpa_ft_process_auth(sta->wpa_sm, rx_auth->bssid,
492 rx_auth->auth_transaction, rx_auth->ies,
493 rx_auth->ies_len,
494 hostapd_notify_auth_ft_finish, hapd);
495 return;
496 }
497#endif /* CONFIG_IEEE80211R */
498fail:
499 hostapd_sta_auth(hapd, rx_auth->peer, rx_auth->auth_transaction + 1,
500 status, resp_ies, resp_ies_len);
501}
502
503
504static void hostapd_action_rx(struct hostapd_data *hapd,
505 struct rx_action *action)
506{
507 struct sta_info *sta;
508
509 wpa_printf(MSG_DEBUG, "RX_ACTION cat %d action plen %d",
510 action->category, (int) action->len);
511
512 sta = ap_get_sta(hapd, action->sa);
513 if (sta == NULL) {
514 wpa_printf(MSG_DEBUG, "%s: station not found", __func__);
515 return;
516 }
517#ifdef CONFIG_IEEE80211R
518 if (action->category == WLAN_ACTION_FT) {
519 wpa_printf(MSG_DEBUG, "%s: FT_ACTION length %d",
520 __func__, (int) action->len);
521 wpa_ft_action_rx(sta->wpa_sm, action->data, action->len);
522 }
523#endif /* CONFIG_IEEE80211R */
524#ifdef CONFIG_IEEE80211W
525 if (action->category == WLAN_ACTION_SA_QUERY && action->len >= 4) {
526 wpa_printf(MSG_DEBUG, "%s: SA_QUERY_ACTION length %d",
527 __func__, (int) action->len);
528 ieee802_11_sa_query_action(hapd, action->sa,
529 *(action->data + 1),
530 action->data + 2);
531 }
532#endif /* CONFIG_IEEE80211W */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800533#ifdef CONFIG_WNM
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700534 if (action->category == WLAN_ACTION_WNM) {
535 wpa_printf(MSG_DEBUG, "%s: WNM_ACTION length %d",
536 __func__, (int) action->len);
537 ieee802_11_rx_wnm_action_ap(hapd, action);
538 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800539#endif /* CONFIG_WNM */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700540}
541
542
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700543#ifdef NEED_AP_MLME
544
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700545#define HAPD_BROADCAST ((struct hostapd_data *) -1)
546
547static struct hostapd_data * get_hapd_bssid(struct hostapd_iface *iface,
548 const u8 *bssid)
549{
550 size_t i;
551
552 if (bssid == NULL)
553 return NULL;
554 if (bssid[0] == 0xff && bssid[1] == 0xff && bssid[2] == 0xff &&
555 bssid[3] == 0xff && bssid[4] == 0xff && bssid[5] == 0xff)
556 return HAPD_BROADCAST;
557
558 for (i = 0; i < iface->num_bss; i++) {
559 if (os_memcmp(bssid, iface->bss[i]->own_addr, ETH_ALEN) == 0)
560 return iface->bss[i];
561 }
562
563 return NULL;
564}
565
566
567static void hostapd_rx_from_unknown_sta(struct hostapd_data *hapd,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800568 const u8 *bssid, const u8 *addr,
569 int wds)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700570{
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800571 hapd = get_hapd_bssid(hapd->iface, bssid);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700572 if (hapd == NULL || hapd == HAPD_BROADCAST)
573 return;
574
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800575 ieee802_11_rx_from_unknown(hapd, addr, wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700576}
577
578
579static void hostapd_mgmt_rx(struct hostapd_data *hapd, struct rx_mgmt *rx_mgmt)
580{
581 struct hostapd_iface *iface = hapd->iface;
582 const struct ieee80211_hdr *hdr;
583 const u8 *bssid;
584 struct hostapd_frame_info fi;
585
586 hdr = (const struct ieee80211_hdr *) rx_mgmt->frame;
587 bssid = get_hdr_bssid(hdr, rx_mgmt->frame_len);
588 if (bssid == NULL)
589 return;
590
591 hapd = get_hapd_bssid(iface, bssid);
592 if (hapd == NULL) {
593 u16 fc;
594 fc = le_to_host16(hdr->frame_control);
595
596 /*
597 * Drop frames to unknown BSSIDs except for Beacon frames which
598 * could be used to update neighbor information.
599 */
600 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
601 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
602 hapd = iface->bss[0];
603 else
604 return;
605 }
606
607 os_memset(&fi, 0, sizeof(fi));
608 fi.datarate = rx_mgmt->datarate;
609 fi.ssi_signal = rx_mgmt->ssi_signal;
610
611 if (hapd == HAPD_BROADCAST) {
612 size_t i;
613 for (i = 0; i < iface->num_bss; i++)
614 ieee802_11_mgmt(iface->bss[i], rx_mgmt->frame,
615 rx_mgmt->frame_len, &fi);
616 } else
617 ieee802_11_mgmt(hapd, rx_mgmt->frame, rx_mgmt->frame_len, &fi);
618
619 random_add_randomness(&fi, sizeof(fi));
620}
621
622
Jouni Malinen75ecf522011-06-27 15:19:46 -0700623static void hostapd_rx_action(struct hostapd_data *hapd,
624 struct rx_action *rx_action)
625{
626 struct rx_mgmt rx_mgmt;
627 u8 *buf;
628 struct ieee80211_hdr *hdr;
629
630 wpa_printf(MSG_DEBUG, "EVENT_RX_ACTION DA=" MACSTR " SA=" MACSTR
631 " BSSID=" MACSTR " category=%u",
632 MAC2STR(rx_action->da), MAC2STR(rx_action->sa),
633 MAC2STR(rx_action->bssid), rx_action->category);
634 wpa_hexdump(MSG_MSGDUMP, "Received action frame contents",
635 rx_action->data, rx_action->len);
636
637 buf = os_zalloc(24 + 1 + rx_action->len);
638 if (buf == NULL)
639 return;
640 hdr = (struct ieee80211_hdr *) buf;
641 hdr->frame_control = IEEE80211_FC(WLAN_FC_TYPE_MGMT,
642 WLAN_FC_STYPE_ACTION);
643 if (rx_action->category == WLAN_ACTION_SA_QUERY) {
644 /*
645 * Assume frame was protected; it would have been dropped if
646 * not.
647 */
648 hdr->frame_control |= host_to_le16(WLAN_FC_ISWEP);
649 }
650 os_memcpy(hdr->addr1, rx_action->da, ETH_ALEN);
651 os_memcpy(hdr->addr2, rx_action->sa, ETH_ALEN);
652 os_memcpy(hdr->addr3, rx_action->bssid, ETH_ALEN);
653 buf[24] = rx_action->category;
654 os_memcpy(buf + 24 + 1, rx_action->data, rx_action->len);
655 os_memset(&rx_mgmt, 0, sizeof(rx_mgmt));
656 rx_mgmt.frame = buf;
657 rx_mgmt.frame_len = 24 + 1 + rx_action->len;
658 hostapd_mgmt_rx(hapd, &rx_mgmt);
659 os_free(buf);
660}
661
662
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663static void hostapd_mgmt_tx_cb(struct hostapd_data *hapd, const u8 *buf,
664 size_t len, u16 stype, int ok)
665{
666 struct ieee80211_hdr *hdr;
667 hdr = (struct ieee80211_hdr *) buf;
668 hapd = get_hapd_bssid(hapd->iface, get_hdr_bssid(hdr, len));
669 if (hapd == NULL || hapd == HAPD_BROADCAST)
670 return;
671 ieee802_11_mgmt_cb(hapd, buf, len, stype, ok);
672}
673
674#endif /* NEED_AP_MLME */
675
676
677static int hostapd_event_new_sta(struct hostapd_data *hapd, const u8 *addr)
678{
679 struct sta_info *sta = ap_get_sta(hapd, addr);
680 if (sta)
681 return 0;
682
683 wpa_printf(MSG_DEBUG, "Data frame from unknown STA " MACSTR
684 " - adding a new STA", MAC2STR(addr));
685 sta = ap_sta_add(hapd, addr);
686 if (sta) {
687 hostapd_new_assoc_sta(hapd, sta, 0);
688 } else {
689 wpa_printf(MSG_DEBUG, "Failed to add STA entry for " MACSTR,
690 MAC2STR(addr));
691 return -1;
692 }
693
694 return 0;
695}
696
697
698static void hostapd_event_eapol_rx(struct hostapd_data *hapd, const u8 *src,
699 const u8 *data, size_t data_len)
700{
701 struct hostapd_iface *iface = hapd->iface;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800702 struct sta_info *sta;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700703 size_t j;
704
705 for (j = 0; j < iface->num_bss; j++) {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800706 if ((sta = ap_get_sta(iface->bss[j], src))) {
707 if (sta->flags & WLAN_STA_ASSOC) {
708 hapd = iface->bss[j];
709 break;
710 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 }
712 }
713
714 ieee802_1x_receive(hapd, src, data, data_len);
715}
716
717
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700718static struct hostapd_channel_data * hostapd_get_mode_channel(
719 struct hostapd_iface *iface, unsigned int freq)
720{
721 int i;
722 struct hostapd_channel_data *chan;
723
724 for (i = 0; i < iface->current_mode->num_channels; i++) {
725 chan = &iface->current_mode->channels[i];
726 if (!chan)
727 return NULL;
728 if ((unsigned int) chan->freq == freq)
729 return chan;
730 }
731
732 return NULL;
733}
734
735
736static void hostapd_update_nf(struct hostapd_iface *iface,
737 struct hostapd_channel_data *chan,
738 struct freq_survey *survey)
739{
740 if (!iface->chans_surveyed) {
741 chan->min_nf = survey->nf;
742 iface->lowest_nf = survey->nf;
743 } else {
744 if (dl_list_empty(&chan->survey_list))
745 chan->min_nf = survey->nf;
746 else if (survey->nf < chan->min_nf)
747 chan->min_nf = survey->nf;
748 if (survey->nf < iface->lowest_nf)
749 iface->lowest_nf = survey->nf;
750 }
751}
752
753
754static void hostapd_event_get_survey(struct hostapd_data *hapd,
755 struct survey_results *survey_results)
756{
757 struct hostapd_iface *iface = hapd->iface;
758 struct freq_survey *survey, *tmp;
759 struct hostapd_channel_data *chan;
760
761 if (dl_list_empty(&survey_results->survey_list)) {
762 wpa_printf(MSG_DEBUG, "No survey data received");
763 return;
764 }
765
766 dl_list_for_each_safe(survey, tmp, &survey_results->survey_list,
767 struct freq_survey, list) {
768 chan = hostapd_get_mode_channel(iface, survey->freq);
769 if (!chan)
770 continue;
771 if (chan->flag & HOSTAPD_CHAN_DISABLED)
772 continue;
773
774 dl_list_del(&survey->list);
775 dl_list_add_tail(&chan->survey_list, &survey->list);
776
777 hostapd_update_nf(iface, chan, survey);
778
779 iface->chans_surveyed++;
780 }
781}
782
783
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700784void wpa_supplicant_event(void *ctx, enum wpa_event_type event,
785 union wpa_event_data *data)
786{
787 struct hostapd_data *hapd = ctx;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800788#ifndef CONFIG_NO_STDOUT_DEBUG
789 int level = MSG_DEBUG;
790
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700791 if (event == EVENT_RX_MGMT && data->rx_mgmt.frame &&
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800792 data->rx_mgmt.frame_len >= 24) {
793 const struct ieee80211_hdr *hdr;
794 u16 fc;
795 hdr = (const struct ieee80211_hdr *) data->rx_mgmt.frame;
796 fc = le_to_host16(hdr->frame_control);
797 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
798 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_BEACON)
799 level = MSG_EXCESSIVE;
Dmitry Shmidt8da800a2013-04-24 12:57:01 -0700800 if (WLAN_FC_GET_TYPE(fc) == WLAN_FC_TYPE_MGMT &&
801 WLAN_FC_GET_STYPE(fc) == WLAN_FC_STYPE_PROBE_REQ)
802 level = MSG_EXCESSIVE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800803 }
804
805 wpa_dbg(hapd->msg_ctx, level, "Event %s (%d) received",
806 event_to_string(event), event);
807#endif /* CONFIG_NO_STDOUT_DEBUG */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700808
809 switch (event) {
810 case EVENT_MICHAEL_MIC_FAILURE:
811 michael_mic_failure(hapd, data->michael_mic_failure.src, 1);
812 break;
813 case EVENT_SCAN_RESULTS:
814 if (hapd->iface->scan_cb)
815 hapd->iface->scan_cb(hapd->iface);
816 break;
817#ifdef CONFIG_IEEE80211R
818 case EVENT_FT_RRB_RX:
819 wpa_ft_rrb_rx(hapd->wpa_auth, data->ft_rrb_rx.src,
820 data->ft_rrb_rx.data, data->ft_rrb_rx.data_len);
821 break;
822#endif /* CONFIG_IEEE80211R */
823 case EVENT_WPS_BUTTON_PUSHED:
824 hostapd_wps_button_pushed(hapd, NULL);
825 break;
826#ifdef NEED_AP_MLME
827 case EVENT_TX_STATUS:
828 switch (data->tx_status.type) {
829 case WLAN_FC_TYPE_MGMT:
830 hostapd_mgmt_tx_cb(hapd, data->tx_status.data,
831 data->tx_status.data_len,
832 data->tx_status.stype,
833 data->tx_status.ack);
834 break;
835 case WLAN_FC_TYPE_DATA:
836 hostapd_tx_status(hapd, data->tx_status.dst,
837 data->tx_status.data,
838 data->tx_status.data_len,
839 data->tx_status.ack);
840 break;
841 }
842 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800843 case EVENT_EAPOL_TX_STATUS:
844 hostapd_eapol_tx_status(hapd, data->eapol_tx_status.dst,
845 data->eapol_tx_status.data,
846 data->eapol_tx_status.data_len,
847 data->eapol_tx_status.ack);
848 break;
849 case EVENT_DRIVER_CLIENT_POLL_OK:
850 hostapd_client_poll_ok(hapd, data->client_poll.addr);
851 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 case EVENT_RX_FROM_UNKNOWN:
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800853 hostapd_rx_from_unknown_sta(hapd, data->rx_from_unknown.bssid,
854 data->rx_from_unknown.addr,
855 data->rx_from_unknown.wds);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700856 break;
857 case EVENT_RX_MGMT:
858 hostapd_mgmt_rx(hapd, &data->rx_mgmt);
859 break;
860#endif /* NEED_AP_MLME */
861 case EVENT_RX_PROBE_REQ:
862 if (data->rx_probe_req.sa == NULL ||
863 data->rx_probe_req.ie == NULL)
864 break;
865 hostapd_probe_req_rx(hapd, data->rx_probe_req.sa,
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800866 data->rx_probe_req.da,
867 data->rx_probe_req.bssid,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700868 data->rx_probe_req.ie,
Dmitry Shmidt04949592012-07-19 12:16:46 -0700869 data->rx_probe_req.ie_len,
870 data->rx_probe_req.ssi_signal);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 break;
872 case EVENT_NEW_STA:
873 hostapd_event_new_sta(hapd, data->new_sta.addr);
874 break;
875 case EVENT_EAPOL_RX:
876 hostapd_event_eapol_rx(hapd, data->eapol_rx.src,
877 data->eapol_rx.data,
878 data->eapol_rx.data_len);
879 break;
880 case EVENT_ASSOC:
881 hostapd_notif_assoc(hapd, data->assoc_info.addr,
882 data->assoc_info.req_ies,
883 data->assoc_info.req_ies_len,
884 data->assoc_info.reassoc);
885 break;
886 case EVENT_DISASSOC:
887 if (data)
888 hostapd_notif_disassoc(hapd, data->disassoc_info.addr);
889 break;
890 case EVENT_DEAUTH:
891 if (data)
892 hostapd_notif_disassoc(hapd, data->deauth_info.addr);
893 break;
894 case EVENT_STATION_LOW_ACK:
895 if (!data)
896 break;
897 hostapd_event_sta_low_ack(hapd, data->low_ack.addr);
898 break;
Jouni Malinen75ecf522011-06-27 15:19:46 -0700899 case EVENT_RX_ACTION:
900 if (data->rx_action.da == NULL || data->rx_action.sa == NULL ||
901 data->rx_action.bssid == NULL)
902 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700903#ifdef NEED_AP_MLME
Jouni Malinen75ecf522011-06-27 15:19:46 -0700904 hostapd_rx_action(hapd, &data->rx_action);
Jouni Malinen75ecf522011-06-27 15:19:46 -0700905#endif /* NEED_AP_MLME */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700906 hostapd_action_rx(hapd, &data->rx_action);
907 break;
908 case EVENT_AUTH:
909 hostapd_notif_auth(hapd, &data->auth);
910 break;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700911 case EVENT_CH_SWITCH:
912 if (!data)
913 break;
914 hostapd_event_ch_switch(hapd, data->ch_switch.freq,
915 data->ch_switch.ht_enabled,
916 data->ch_switch.ch_offset);
917 break;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800918 case EVENT_CONNECT_FAILED_REASON:
919 if (!data)
920 break;
921 hostapd_event_connect_failed_reason(
922 hapd, data->connect_failed_reason.addr,
923 data->connect_failed_reason.code);
924 break;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700925 case EVENT_SURVEY:
926 hostapd_event_get_survey(hapd, &data->survey_results);
927 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700928 default:
929 wpa_printf(MSG_DEBUG, "Unknown event %d", event);
930 break;
931 }
932}
933
934#endif /* HOSTAPD */